• 🏆 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!

Variables for Loops, Groups, etc

Status
Not open for further replies.
Level 5
Joined
May 12, 2008
Messages
77
Hello Hive,

long time ago (when i started mapping) i was warned not to use the same integers for loops too often and i was told to never use Integer A and B because if two triggers which both use the same integer for a loop run at the same time there would be errors. I never really thought about that...
Now i want to clean up messy triggers within one of my maps and there are just too many integer variables. Further i have the feeling that what i stated above is wrong...

So here are my questions:
When i have several triggers, when can i use the same integer variables for loops and when do i have to use different integer variables?
Can i use the same variable in the same trigger multiple times?
Can i use the same variable in different triggers?
Can i use the same variable in trigger A and trigger B if trigger A triggers trigger B?
Does Integer A and B have any advantages or disadvantages compared to custom variables?

How does this work with UnitGroups, Hashtables, Locations, ...
Can i create just one Hashtable to destroy any Locations and UnitGroups and avoid memory leaks this way?
Probably it should work but i dont have any knowledge about jass and in which order triggers are executed if there are multiples that have to be done right now...

Maybe this one question is enough to answer all that stuff in a few words:
When i dont want to save any information for later use (whis is for example just to destroy a unit group or just to get a loop going) and if there are multiple triggers which need the same kind of variable to solve a problem, can i use the same custom variable over and over for this problem?
 
Level 19
Joined
Aug 8, 2007
Messages
2,765
Integer A and B loops are completely fine unless you include waits inside them.

If you're really that worried you can use custom integers for those loops.
Also someone asked this same question recently, so check out this thread.

http://www.hiveworkshop.com/forums/world-editor-help-zone-98/temp-var-243632/

No, integer A shouldn't be used.

If its a custom system, make your own looping variable.
If its your own map, use something like TempInt.

Integer A/B shouldn't be used because it evaluates to

JASS:
    set udg_Backpack[GetForLoopIndexA()] = null

(gui has enough extraneous function calls already)
 
@Arhowk:

Well, that isn't really the reason to not use Integer A/B. That is one reason, but it is relatively negligible.

The real problems come when you are using a loop and execute some code that may fire an event. For example (pseudocode):

  • For loop (Integer A) from 1 to 10 do (actions)
    • Loop - Actions:
      • Unit - Issue "Stop"
  • SomeTrigger
    • Events
      • Unit - A unit is issued an immediate order
    • Conditions
    • Actions
      • For each (Integer A) from 1 to 10 do (actions)
        • Loop - Actions
          • <Some Actions>
Notice that the first trigger will fire the second, and the second uses (Integer A). At that point, (Integer A) will be incremented to 10, and when the first loop returns, (Integer A) will already be 10 (when it should actually be 2).

At some point in time, it became a requirement to use a custom variable for looping to prevent this from happening, even if it is unlikely.
 
Status
Not open for further replies.
Top