Home   Help Search Login Register  

Author Topic: deleting entire arrays of pre-placed units  (Read 1163 times)

0 Members and 1 Guest are viewing this topic.

KyleEasterly

  • Guest
deleting entire arrays of pre-placed units
« on: 27 Jan 2004, 06:07:03 »
Hi, another stupid question, I looked around here and tried some different stuff out, but can't get this working, heres the problem:

I'm making this rather large co-op mission designed for play on a LAN with a dedicated server. The west spawn at the airfield at the far east end of Tonal island, and work their way west until the entire island is cleared. The enemies are only grouped around the dwellings and bases, so it shouldn't be too hard to complete it.

The only problem I could forsee is if 2 hours into this, someone on the LAN crashed, we could either restart the WHOLE thing from scratch or just go on without them, which wouldn't be too fun =(

So I figured I would divide the island by the 14 larger map columns (the single letter ones, starting at D and going to Q) and added a parameter "Clear To:" then all the columns. I have the parameter working, and I have the markers (14 large 675x8100 default red-transparent markers, slightly overlapping) and triggers (same size, set for repeatedly when resistance is not present) set up and working as far as I know. The only real difficult bit of scripting is the actual clearing of the units when the clear to zone is selected. For now I am only testing with zones D and E, and have placed a single enemy unit down the airfield facing away from me, but within sight of my player. In his init field I have the following:
unitsInZoneD = unitsInZoneD + units this
figuring for each unit I place in whichever zone I'll add them to an array with alll the other units in the zone, then when it is time to delete them, do a little "deleteVehicle _x" forEach unitsInZoneD and be done with it. WRONG!

My test setup:
Myself as player, on init:
Code: [Select]
[] exec "init.sqs"

resistance government HD soldier within sight of me, on init:
Code: [Select]
unitsInZoneD = unitsInZoneD + units this

file description.ext:
Code: [Select]
respawn=3;
respawndelay=5;
titleParam1 = "Clear To:";
valuesParam1[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14};
defValueParam1 = 1;
textsParam1[] = {"D","E","F","G","H","I","J","K","L","M","N","O","P","Q"};

file init.sqs:
Code: [Select]
? param1 == 1 : goto "clearZoneD"
? param1 == 2 : goto "clearZoneE"
? param1 == 3 : goto "clearZoneF"
? param1 == 4 : goto "clearZoneG"
? param1 == 5 : goto "clearZoneH"
? param1 == 6 : goto "clearZoneI"
? param1 == 7 : goto "clearZoneJ"
? param1 == 8 : goto "clearZoneK"
? param1 == 9 : goto "clearZoneL"
? param1 == 10 : goto "clearZoneM"
? param1 == 11 : goto "clearZoneN"
? param1 == 12 : goto "clearZoneO"
? param1 == 13 : goto "clearZoneP"
? param1 == 14 : goto "clearZoneQ"

#clearZoneE
[unitsInZoneE] exec "removeunits.sqs"
[2, 1, 0] exec "setzone.sqs"

#clearZoneD
[unitsInZoneD] exec "removeunits.sqs"
[1, 1, 0] exec "setzone.sqs"

#mainLoop

? getMarkerColor "markD" == "ColorGreenAlpha" : [unitsInZoneD] exec "removeunits.sqs"
? getMarkerColor "markE" == "ColorGreenAlpha" : [unitsInZoneE] exec "removeunits.sqs"

~2
goto "mainLoop"

file setzone.sqs:
Code: [Select]
_zone = _this select 0
; zone table:
; D E F G H I J K L M  N  O  P  Q
; 1 2 3 4 5 6 7 8 9 10 11 12 13 14
_state = _this select 1
; 0 is enemy-occupied, 1 is cleared
_report = _this select 2
; 0 is 'silent mode', 1 hints the change

? _zone == 1 : goto "zoneD"
? _zone == 2 : goto "zoneE"
? _zone == 3 : goto "zoneF"
? _zone == 4 : goto "zoneG"
? _zone == 5 : goto "zoneH"
? _zone == 6 : goto "zoneI"
? _zone == 7 : goto "zoneJ"
? _zone == 8 : goto "zoneK"
? _zone == 9 : goto "zoneL"
? _zone == 10 : goto "zoneM"
? _zone == 11 : goto "zoneN"
? _zone == 12 : goto "zoneO"
? _zone == 13 : goto "zoneP"
? _zone == 14 : goto "zoneQ"

#zoneD
? _state == 1 : "markD" setMarkerColor "ColorGreenAlpha"
? _state == 1 : if (_report == 1) then {hint "Zone D clear."}
? _state == 0 : "markD" setMarkerColor "ColorRedAlpha"
? _state == 0 : if (_report == 1) then {hint "Enemies detected in Zone D."}
exit

#zoneE
? _state == 1 : "markE" setMarkerColor "ColorGreenAlpha"
? _state == 1 : if (_report == 1) then {hint "Zone E clear."}
? _state == 0 : "markE" setMarkerColor "ColorRedAlpha"
? _state == 0 : if (_report == 1) then {hint "Enemies detected in Zone E."}
exit

file removeunits.sqs:
Code: [Select]
; removeunits.sqs
; takes in an array of units and destroys them all

"deleteVehicle _x" forEach _this select 0
exit

When I test it, I first manually delete Invasion of Tonal.bas_i1.pbo to ensure I'm not running an old version of the mission, then I launch OFP, open the mission editor, export to MP missions with the same name, exit the editor, host a local server, set Clear To to E, and play. I know the scripts are working period because the markers change from the default transparent red to transparent green, signifying that they are clear (I have not yet put triggers down that would set enemy-containing zone D to red again, waiting until i get this working).

The actual problem comes when I start the mission, and that enemy guy is STILL THERE. No errors are displayed or anything.

And if you see any ways I can further optimize this process, please let me know, this is the first time I've ever scripted something except for some camera stuff a year ago.

Thanks,

-Kyle

Offline Welshmanizer

  • Members
  • *
Re:deleting entire arrays of pre-placed units
« Reply #1 on: 27 Jan 2004, 22:48:19 »
Without looking at your code in too much detail, I believe the problem is that you have not defined an initial value for unitsInZoneD.

You are adding an array (units this) to a null variable of unknown type (unitsInZoneD). This won't work. You can only add an array to an array. Therefore you need to define unitsInZoneD as an empty array before you start adding things to it.

Put this at the top of your init.sqs:

Code: [Select]
unitsInZoneD = []
You will need to do this for each zone variable.

 ;D
« Last Edit: 27 Jan 2004, 22:52:31 by Welshmanizer »

KyleEasterly

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #2 on: 28 Jan 2004, 04:30:09 »
aha, I figured it would be something simple like that... =/

well that seems to be working now, HOWEVER, when it actually tries to deleteVehicle the guy(s), this error appears:
'deleteVehicle _x|#|': Error deletevehicle: Type Array, expected Object

I'll experiment with the command some and search around for specific examples of how to use it, but if anyone knows whats going on and just happens to be reading this, posting here would be ok too =D

Thanks,

and special thanks to Welshmanizer

-Kyle

edit:

Well, now It's pretty much doing the same thing. I even got rid of the forEach and did a loop:

Code: [Select]
; removeunits.sqs
; takes in an array of units and destroys them all
_zonearr = _this select 0

_i = 0
#startLoop
_theguy = _zonearr select _i
hint format ["deleting %1", _theguy]
deleteVehicle _theguy
?(_i < count _zonearr) : goto "startLoop"

exit

so in theory I could [unitsInZoneD] exec "removeunits.sqs" and have it work.... wrong...

when the hint pops up (i disabled all the other hints in setzone.sqs so i could see this one) it says:
deleting scalar bool array string 0xfcffffef

oops!

I even gave the guy in zone d a name (guyd)... this is really weird...

and its not the _zonearr = this select 0 line, because I replaced _zonearr in the loop with unitsInZoneD and it gave the same exact result.

if I change removeunits.sqs to only have deleteVehicle guyd, it deletes him, but I don't want to have to have a line of code for every single unit i place...

by this I have determined that there MUST be something wrong with my array assignment, I'll mess around with that then edit this post again
« Last Edit: 28 Jan 2004, 05:18:12 by KyleEasterly »

Unnamed

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #3 on: 28 Jan 2004, 05:37:14 »
You need to increment _i in your loop, you are constantly displaying element 0, even after you deleted it. Common mistake, for me anyway :)

KyleEasterly

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #4 on: 28 Jan 2004, 06:18:23 »
ah, thanks, but its not even deleting him at all... he would be element 0 right?

I tried this: instead of just doing 'unitsInZoneD = []' at beginning of init i made it 'unitsInZoneD = [guyd]' and removed the unitsInZoneD = unitsInZoneD + units this from his init, and tried that. it didnt change anything... so I'm still not getting something here.

I'll try incrementing the counter, but I doubt that will fix any of my current problems (would have been later though) - its not even touching the first guy in the array.

edit:

ok, it works after I increment the variable... now to test a few other things... BUT this is still with the unit to array assignment in init.sqs... meaning I have to manually add each unit, when id like to do it automatically, so if anyone has any other ideas besides the one i used in my first post, please let me know

edit:

just found out about thislist... will try placing some more triggers and messing with that...

resistance
present
once

activation:
unitsInZoneWHATEVER = thislist

=D I hope this does it and I can stop bugging you guys
« Last Edit: 28 Jan 2004, 06:42:07 by KyleEasterly »

Unnamed

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #5 on: 28 Jan 2004, 06:44:30 »
Put a delay in your loop, it's looping so fast you dont have chance to see the guy being displayed, before you delete him:

Code: [Select]
#startLoop
_theguy = _zonearr select _i
hint format ["deleting %1", _theguy]

~2
_i=_i+1

deleteVehicle _theguy
?(_i < count _zonearr) : goto "startLoop"

When you call:

Code: [Select]
deleteVehicle _theguy
All your doing is deleting the Vehicle, element 0 is still there and so is the variable _theguy. They just point to an object thats has already been deleted, so you get your original error.
« Last Edit: 28 Jan 2004, 06:45:47 by Unnamed »

KyleEasterly

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #6 on: 28 Jan 2004, 08:32:20 »
ah, so if i do deleteVehicle (_zonearr select _i) then it should work?

and remember that part is working now... kinda hard to notice my edits though, i just have a hard time adding them to the array. right now i have to set them in init.sqs instead of in the units' init fields

here is what i have and it works perfectly (but will be a pain to add deletable units)

in removeunits.sqs:
Code: [Select]
; removeunits.sqs
; takes in an array of units and destroys them all
_zonearr = _this select 0

_i = 0
#startLoop
_theguy = _zonearr select _i
deleteVehicle _theguy
_i = _i + 1
?(_i < count _zonearr) : goto "startLoop"

exit

at the beginning of init.sqs:
Code: [Select]
unitsInZoneD = [guyd_1, guyd_2, guyd_3, guyd_4, guyd_5, guyd_6, guyd_7, guyd_8]
unitsInZoneE = []
unitsInZoneF = []
unitsInZoneG = []
unitsInZoneH = []
unitsInZoneI = []
unitsInZoneJ = []
unitsInZoneK = []
unitsInZoneL = []
unitsInZoneM = []
unitsInZoneN = []
unitsInZoneO = []
unitsInZoneP = []

I suppose I could delete them by group... maybe the foreach command would work that way...

Offline Welshmanizer

  • Members
  • *
Re:deleting entire arrays of pre-placed units
« Reply #7 on: 28 Jan 2004, 15:05:45 »
Your original code for the removal was right ("deleteVehicle _x" forEach _this select 0). It is your execution of this script that is wrong.

You used [unitsInZoneD] exec "removeunits.sqs" - this is wrong. unitsInZoneD is already an array so you don't need to add the brackets. In effect you are executing it like this:

[ [guyd_1, guyd_2, guyd_3] ] exec "removeunits.sqs"

so your _this select 0 will select the whole array of soldiers.

Instead, exec it like so:

unitsInZoneD exec "removeunits.sqs"

 8)

KyleEasterly

  • Guest
Re:deleting entire arrays of pre-placed units
« Reply #8 on: 28 Jan 2004, 20:45:47 »
aieeeeeeee... that crossed my mind a few times after I had converted it to the loop-method...


however I still cannot seem to add units to an array when they are initialized... what i need to be able to do is add whatever unit to the array by using its init field, with the same line of code between all the units belonging to the same array
unitsInZoneD = unitsInZoneD + units this
did not work for me, I currently have to name each unit, then put those units in the array at the beginning of init.sqs. this works perfectly but is a major pain in the neck to add more units.

Offline Welshmanizer

  • Members
  • *
Re:deleting entire arrays of pre-placed units
« Reply #9 on: 28 Jan 2004, 21:24:11 »
Oh yeah, you need to put the unitsInZoneD = [] first in the init field of the first unit you created. The units' init fields are processed before the init.sqs, and out of these units the first one you created will be processed first. Don't put it in the init.sqs because it will 'overwrite' the array, resetting it to empty.

Also, if you have more than one unit in a group, you only need unitsInZoneD = unitsInZoneD + units this in the leader's init field. Otherwise, you'll get multiple entries of the same unit.