• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

Index Value

Status
Not open for further replies.
Level 33
Joined
Mar 27, 2008
Messages
8,035
I have a test map which when a Player select a unit, it gives them an index value.
This trigger will first check whether the unit has been indexed or not (Unit Group).
If yes, it will loop through the index size and check which index does the unit holds.
If no, it will give the unit an index value and add it to Unit Group.

So, in order to register units and see its index, you gotta follow these step;
1. Select a unit
2. Re-select the selected unit (clicking/drag-select/etc) and then you will see its index value


There are 5 units in the test map, everything works great, but you gotta do this step in order for it to successfully gives correct index value to a unit;
1. Select 1st unit
2. Select 2nd unit
3. Select 3rd unit
4. Select 4th unit
5. Select 5th unit

After you have done that, only then you can check their index value correctly


However, if you do like this;
1. Select 1st unit
2. Re-select 1st unit (or easier way, double-click any 1st unit)
3. Select 2nd unit and so on
4. Index value for 2nd unit and so on will get f**cked up


What I meant by the value is messed up is that the value for 2nd unit and so on will start from 11111, I mean why the index does not raised to 2 ?

This trigger works, if and only if, you followed the first step, register all units first without checking their index value, only then it gives the correct value.

If you register 1st unit or any unit and check their index value without registering all index value for next unit(s), the next unit(s)'s index value will get messed up.

How to solve this, but the most important question, why does it turn to 11111 instead of 2 (for the next index value).

  • Melee Initialization
    • Events
      • Player - Player 1 (Red) Selects a unit
    • Conditions
    • Actions
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • ((Triggering unit) is in IndexGroup) Equal to True
        • Then - Actions
          • For each (Integer Index) from 1 to Index, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • (Triggering unit) Equal to Unit[Index]
                • Then - Actions
                  • Game - Display to (All players) the text: (This unit has index value of + (String(Index)))
                • Else - Actions
        • Else - Actions
          • Set Index = (Index + 1)
          • Game - Display to (All players) the text: (String(Index))
          • Set Unit[Index] = (Triggering unit)
          • Unit Group - Add Unit[Index] to IndexGroup
 

Attachments

  • Checking Index.w3x
    12.8 KB · Views: 38
Level 12
Joined
Aug 12, 2008
Messages
350
I think the fault is at the "For each (Integer Index) from 1 to Index, ..."
Anyway, I fixed it by remaking the trigger.

  • Index Registration
    • Events
      • Player - Player 1 (Red) Selects a unit
    • Conditions
    • Actions
      • Set tempUnit = (Triggering unit)
      • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
        • If - Conditions
          • (tempUnit is in IndexGroup) Equal to False
        • Then - Actions
          • Set Index = (Index + 1)
          • Set Unit[Index] = (Triggering unit)
          • Game - Display to (All players) the text: (String(Index))
          • Unit Group - Add Unit[Index] to IndexGroup
        • Else - Actions
          • For each (Integer tempInteger) from 1 to Index, do (Actions)
            • Loop - Actions
              • If (All Conditions are True) then do (Then Actions) else do (Else Actions)
                • If - Conditions
                  • tempUnit Equal to Unit[tempInteger]
                • Then - Actions
                  • Game - Display to (All players) the text: (This unit has index value of + (String(tempInteger)))
                • Else - Actions
Edit:
  • Set Unit[Index] = (Triggering Unit)
should be (sorry, my mistake)
  • Set Unit[Index] = tempUnit
 

Attachments

  • Checking Index.w3x
    13 KB · Views: 41
Level 33
Joined
Mar 27, 2008
Messages
8,035
Yeah, finding index value by this way uses O(n) searches which is bad if you have like 100 units that has been indexed.

Skip remaining actions when you've found the index :)
Ah yeah, don't want the trigger to still search index when you have found one eh ?
Unit[50] has found, but max index size is 100, it would be a waste of 50 times more looping occur, better stop the remaining operation.
 
Level 23
Joined
Apr 16, 2012
Messages
4,041
Spartipilo, skip remaining actions is good if you dont have anything else in the trigger after the loop as it stops the thread
also I think(nothing confirmed) that simple return takes less time to run then setting variable to some value
I think you know it anyways :D
 
Level 33
Joined
Mar 27, 2008
Messages
8,035
I don't trust "Skip remaining actions" I like setting bj_forLoopAIndex = to the max value so the loop stops.
Hey I'd rather use this method, never thought about this.
Because some trigger demands the loop to be at the top while leaving important actions below it that needs to be executed.
If we skip remaining actions, those below actions won't get fired at all.
If we set integer counter to max, it will end that loop and still fire below actions.

Spartipilo, skip remaining actions is good if you dont have anything else in the trigger after the loop as it stops the thread
also I think(nothing confirmed) that simple return takes less time to run then setting variable to some value
I think you know it anyways :D
Yeah, it depends on the situation too.
If no further actions, we should use Skip remaining actions
If there is, setting integer counter to max is the way.
 
Status
Not open for further replies.
Top