Removing a unit from unit array

Status
Not open for further replies.
Level 7
Joined
Jul 29, 2009
Messages
276
i have an array of units and a trigger that puts a unit in it.

i want to have another trigger to free/clear/empty that slot.

i tried "Remove unit UnitArray[Intiger A] From Game"
"UnitArray[Intiger A] = No Unit"
"UnitArray[Intiger A] Custom Value = 0"
and it just isnt working.

(it's importent that the way to remove the unit will support intigers, even if its a custom script).

thx in advence for the helpers!:thumbs_up:
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
This problem sounds so easy why are you asking for help?
If you want them in consequtive order but nonstatic references, then simply overwrite the removed index with the unit in the final slot and reduce the allocated size by 1 for where the final slot is.

If static reference is important, then you need 2 more arrays, one which handles data nonstatically (which you loop though) and purly holds the static references to the other array. The other uses linked list principle to set up a struct and basically allow you to reuse slots which you dereference.

If all you need is nonstatic index reference, then this is so easy lol.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,285
You have your unit array.
When you add stuff to it you take a empty slot after your block of units. To do this you are using a variable which holds the last used index which I will call n.

Lets say our array has 3 entries.
n = 3
array[0] = unit1
array[1] = unit2
array[2] = unit3

Now lets say you want to remove the unit in index 0 from the system. You would set n to n - 1 and then get the value from tthat slot and override that in index 0. Your data will then look like.

n = 2
array[0] = unit3
array[1] = unit2

Thus you will be able to loop through it continously without ever encountering gap or data which you want to remove.

The problem is if you want the units to have a static index, as you then need other index systems to handle them and such. Basically you do the above method but using an integer array of the static index values of the units. You then just need another integer array to set up a link list system so that static indexes can be recycled.

Incase this is not what you want, an array is a block of data which takes an index for an offset. Thus once it has been created it will always be that size (as WC3 does not allow the reduction of arrays and enlarges them in powers of 2). You can not remove slots from an array as it is a block, you can clear them and use them for something else. You can however move orconfigure the data around in them so that it seems that a slot has been removed tou your understanding.
 
Status
Not open for further replies.
Top