• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

Indexing the picked units

Status
Not open for further replies.
Level 11
Joined
Jan 23, 2015
Messages
788
Hey, guys, I think I can find a way to do this, but I need to find one fast, and my brain got jammed again, as usual, so:
How do I put every picked unit (from a unit group with unknown number - but that can be fixed if needed) into an indexed variable, with a different index for every one of them? I'm sure you know what I mean -_-

p.s. gee, I could barely write this thread -_-..
 
Level 11
Joined
Jan 23, 2015
Messages
788
Have you got any idea of how to make a system like this:

Let's say I pick more units and index them all, then once a unit dies, a slot frees up, so I can index other unit with the index the dead one used..

Like:

Footman1 = Unit[21]
Footman2 = Unit[22]
Footman3 = Unit[23]
When Footman2 dies, I pick another group of units and set Unit[22] as some of the picked unit, but leave Footman1 and Footman3 as Unit[21] and Unit[23] cause they are still alive..
Perhaps this can be done through adding the units in a unit group and indexing them every 1 second checking if they are dead or alive, but the index will change every time I index them..
 
"Custom Value" is an unused integer only units have that you can define and access via trigger on a per-unit base (kind of like "unit point value", only that is has no effect on gameplay or the score screen. However, every unit has only one "Custom value", which is why it can only be used for one task per map.

This spawned the idea of creating a "unit indexer", which uses the custom value of units to assign a unique index for every unit for use in triggered spells and abilities in combination with array variables, as arrays only take integers as array index.

Sounds familiar? Correct. Because this is basicly exactly what you are trying to do.


This data structure is called a "stack" in object orientated programming: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

In pseudo code, it works like this (shamelessly editing IcemanBo's answer):

Trigger 1 (this one assigns an index for every unit on the map when the game starts):
Map Initialization:
Set MaxIndex = 0
PickAllUnitsInMapAndDoActions
---- Set MaxIndex = MaxIndex + 1
---- Set Unit[MaxIndex] = PickedUnit
---- Set Custom Value of Picked Unit = MaxIndex

Trigger 2 (this one cleans up dead units by swapping the last entry of the list with the one that was killed):
A unit dies:
Set Unit[Custom Value of Dying Unit] = Unit[MaxIndex]
Set Unit[MaxIndex] = No Unit
Set MaxIndex = MaxIndex - 1

Trigger 3 (this one automaticly adds newly created units):
A unit enters map rect:
---- Set MaxIndex = MaxIndex + 1
---- Set Unit[MaxIndex] = PickedUnit


However, it is highly recommended to use an existing Unit Indexer instead ... it will do all this automaticly for you and will also catch several corner cases you ignore with the implementation above.
 
Level 11
Joined
Jan 23, 2015
Messages
788
"Custom Value" is an unused integer only units have that you can define and access via trigger on a per-unit base (kind of like "unit point value", only that is has no effect on gameplay or the score screen. However, every unit has only one "Custom value", which is why it can only be used for one task per map.

This spawned the idea of creating a "unit indexer", which uses the custom value of units to assign a unique index for every unit for use in triggered spells and abilities in combination with array variables, as arrays only take integers as array index.

Sounds familiar? Correct. Because this is basicly exactly what you are trying to do.


This data structure is called a "stack" in object orientated programming: https://en.wikipedia.org/wiki/Stack_(abstract_data_type)

In pseudo code, it works like this (shamelessly editing IcemanBo's answer):

Trigger 1 (this one assigns an index for every unit on the map when the game starts):
Map Initialization:
Set MaxIndex = 0
PickAllUnitsInMapAndDoActions
---- Set MaxIndex = MaxIndex + 1
---- Set Unit[MaxIndex] = PickedUnit
---- Set Custom Value of Picked Unit = MaxIndex

Trigger 2 (this one cleans up dead units by swapping the last entry of the list with the one that was killed):
A unit dies:
Set Unit[Custom Value of Dying Unit] = Unit[MaxIndex]
Set Unit[MaxIndex] = No Unit
Set MaxIndex = MaxIndex - 1

Trigger 3 (this one automaticly adds newly created units):
A unit enters map rect:
---- Set MaxIndex = MaxIndex + 1
---- Set Unit[MaxIndex] = PickedUnit


However, it is highly recommended to use an existing Unit Indexer instead ... it will do all this automaticly for you and will also catch several corner cases you ignore with the implementation above.

Okay, so this uses the raises the integer MaxIndex by 1 for every unit, so the first picked unit is indexed on number 1, while for example the eight on number 8, then it sets the custom value of that unit so it will be fixed and will use that index till it dies, then when the unit dies, that index (slot) frees up for another unit right? But how can that be made since we change the index for the dying unit to MaxIndex, there's another unit with that index and will conflict when subtracting 1..

Like:

Unit[1] Footman
Unit[2] Rifleman
Unit[3] Peasant

The integers above are the MaxIndex indexes..

Unit[2] dies, then we set this unit's index to MaxIndex which is 3, which means, it becomes Unit[3], then both Unit[2] and Unit[3] are a same unit, in this case, they are the Peasant, so we make the Peasant = No Unit instead, or together with the Rifleman.... Is it like that, or I'm missing some part.. (it's really hard to understand something that is made by someone other, I can barely understand some trigger I made on my own but forgot what it actually does.. lol)
 
Status
Not open for further replies.
Top