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

What was the Bug in the For loop Integer A Function?

Status
Not open for further replies.

Dr Super Good

Spell Reviewer
Level 63
Joined
Jan 18, 2005
Messages
27,192
I have seen some tutorials referencing that there is a bug in the function
If you nest two or more of them it will break because internally they use a global variable to track the state of the loop so uses of the action higher up the virtual machine stack overwrite the state of all lower down uses. This is why both a loop A and loop B are provided since those two can be nested together, as long as only 1 occurrence of both exists on the virtual machine stack at any given time. This also applies with custom loops which use a user specified global variable, however one can limit the scope of exposure of that variable allowing for more control and less risk of nesting.

Now anyone sensible can easily avoid nesting loops which share the same variable state within the same trigger. However what is much less obvious and easy to avoid is nesting occurring in other triggers the loop body might cause to run. For example using a loop A to create units at a variety of locations fed from an array will fire "A unit enters (playable map are)" triggers on every iteration of the loop. These entering unit triggers will complete first before resuming execution with the create unit loop. If this entering unit trigger then also uses loop A it will override the state of loop A causing a major bug when the trigger finishes and execution of the create unit loop resumes. This is where the custom loop action comes into play since one can assign the create unit loop a dedicated global variable and so be certain that no other triggers can interfere with the state of that variable since only the create unit loop uses it.

Of course the programmatically correct solution is to use a local variable. Local variables are unique storage that exists for each function call and last until the function returns. This makes it physically impossible for the state of such loops to interfere with each other, even if a function is recursive (calls itself). GUI does not support local variables...
 
Status
Not open for further replies.
Top