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

[Trigger] Group Loop & Var

Status
Not open for further replies.
Level 19
Joined
Aug 8, 2007
Messages
2,765
??!?!?!?!?!?!?!?!?!?!?!

http://en.wikipedia.org/wiki/Thread_(computing)

for unit a in (group that contains 5 units) will do
unit a -> entire loop
unit b -> entire loop
unit c -> entire loop
...


Set MyPickedUnit = No unit <--- Was told to put this here but no given reason to why other than the value gets a null, the specified location is what I want to know not why it saves space

that line isnt needed
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
Does this loop pick through all units individually or all at once? I would assume it's looping hella fast but I would still like to make sure respectively.
Like everything in JASS, it happens "instantly" as far as the game state is concerned. However it will pick them in an unspecified sequence due to how computers work. Since WC3 is only "single threaded" you do not have to worry about race conditions, each loop iteration will execute completely before the next can begin.

Set MyPickedUnit = (Picked unit) <--- Wouldn't this overwrite?
Yes, each time around the loop it will overwrite the contents of that variable. If the variable is being used only within the loop this is fine and even a good way to speed up the code however if you use the contents after loop completion you will be using an unspecified unit which is generally not a good thing.

Set MyPickedUnit = No unit <--- Was told to put this here but no given reason to why other than the value gets a null, the specified location is what I want to know not why it saves space
It is needed to allow the unit handle index to be recycled once (if) the unit is removed. If the variable is used a lot it makes no difference and is even bad for performance as it will be recycled sooner or later once a new value is assigned anyway. Only null unit variables if the variable is not used often or will never be used again for the rest of the session.
 
Level 12
Joined
Mar 17, 2007
Messages
412
Yes, each time around the loop it will overwrite the contents of that variable.

Regarding the overwrite, the best example I can offer is shown below I hope you can understand it.


For example lets say we have a loop factor of 3 with ( [x] = [MyPickedUnit] )

(Is questionable - making sure I'm not missing something)
Action - Loop
[x]
Looping
[x] [x] <--- 1 extra block
Looping
[x] [x] [x] <--- 2 extra blocks

set [x] = Null <--- Recycles only 1 block? What of the other 2?

---or---

(I'm assuming this is the correct way)
Action - Loop
[x]
Looping
[x] <--- Overwritten x2?
Looping
[x] <--- Overwritten x3?

set [x] = Null <--- Recycles the 1 block (Clears it completely)


What I don't understand of these examples is why the variable gets "nulled" outside of the loop & not inside the loop, when inside the loop I don't know if it's creating a infinite loop, I don't know if it's going back to the first [x] it picked, I don't know if it's even picking any [x] at all to be honest. With that being said I'm blinded by seeing how the null outside the loop works other than clearing it to prevent lag.
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Recycles only 1 block? What of the other 2?
There was only 1 (memory) block, and it is being re-used every time the group callback (the loop) is ran.

What I don't understand of these examples is why the variable gets "nulled" outside of the loop & not inside the loop, when inside the loop I don't know if it's creating a infinite loop, I don't know if it's going back to the first [x] it picked, I don't know if it's even picking any [x] at all to be honest. With that being said I'm blinded by seeing how the null outside the loop works other than clearing it to prevent lag.

As the value within the (memory) block is being overwritten, there is no need to set it to null within the loop (code) block. Doing so will waste processing time.

Think of the memory block as a ring and the data as the digits of your hand, the ring can only be at one finger at a time. Setting the memory block to null within the loop is like removing a ring from one finger, placing it on top the desk, picking it up, and finally, putting it onto a new finger. Setting it to null only at the end of the loop will be like removing the ring on one finger and then putting it on another.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,198
The variable is itself a block of memory. Since it is global it will be allocated at map initialization by the JASS system and will exist until the end of session. It will always take up exactly the same amount of memory which is always 32 bits.

The necessity to "null" at the end is to allow the handle index used by a handle type object to be re-allocated to a new handle type object. Each allocated handle has a reference counter keeping track of the number of times it is referenced by the JASS system. As long as references exist to it the handle cannot be re-used for a new object even if the object it pointed to has long since been removed. The key action of nulling is to remove the reference to the handle and not to assign it a meaningful value, as such assigning it to a different handle value is equally as effective.

Depending on the variable use you might not even need to "null" at all. If practically every trigger uses it as a register (a global used in a local-like way) then the handle it references will be changed so often that even if the referenced object gets removed the amount of time it will block the handle id is minimal.

There are only two cases where you should need to null handles.

The first is if the handle variable performs a reference change highly irregularly, possibly even never. This could be a member in an indexing system where the index might never be re-allocated. It could also be global variables used only for a single once-off cinematic that will never be changed after the cinematic plays. In both cases you want to null to allow the handle id to be able to be recycled at a later time.

The second case is before function return for local handle variables declared with the keyword "local". An error in the JASS system means that such local variables do not automatically remove reference at function return allowing any handle ID they were referencing to become un-reclaimable (the reference counter leaks counts). This does not affect local handle variables declared as arguments, as those do correctly remove reference at function return. This only really affect people scripting in pure JASS since WC3's GUI system does not support local variables.
 
Status
Not open for further replies.
Top