HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

What's the point in using LOCAL variables?

02-22-2003, 05:05 PM#1
FyreDaug
Does it really make that much difference in memory usage and gameplay speed? Or is it just used because it's more organized?
02-22-2003, 05:08 PM#2
Dorelian
im not very good at triggs but id say theyre used to make stuff more organized and easyer.
02-22-2003, 06:41 PM#3
DaKaN
locals are said to respond faster than globals, its best to use locals for doing loops and minor calculations than making a varible definition (that way you dont have to keep typing udg_varible every danm time also =)
02-22-2003, 06:53 PM#4
Guest
Locals are actually very useful. They are used when you only need that variable for THAT function. Local variables disappear when the function that uses them is done. Global variables hang around for ever, so they take up more memory.

Another difference with locals is that you don't need to create a global for every instance the function is run. For example, let's say you had a function that used local variable "x". Everytime the function is run, it would create a new instance of "x" that can only be used or changed in THAT instance of the function. If that variable were a global however, the value of that variable could be changed from any outside influence or even a different instance of the same function.

From an organization standpoint, locals can also be very useful. For one thing, globals have to be declared at the beginning of the war3map.j file. That's why you have to create them in the WE variable list. Locals, on the other hand, only have to be declared at the beginning of a FUNCTION.

Also, since local variables only exist within a given function, the variable name of a local only needs to be unique to that given FUNCTION. This can be very helpful when you find you have the same variable usage in a number of different functions.

I wouldn't say that locals are "better" than globals. Afterall, in many cases, you may need to store a variable for later use or maybe you even WANT to be able to change the variable from a different function.

OK. That should answer your question...but is it worth adding to the tutorial in my signature?
02-22-2003, 07:17 PM#5
FyreDaug
So let me get this straight, tell me if I understand this right: (Not done in any language but basic english)

Function
Declare variable X
Set X = 5
Loop until X is 10
Message (Val.X)
X=X+1
End Function

Would show up as:
5
6
7
8
9

So each time this is run, it RE-creates X and then re-runs the loop again and it elimitates the variable after the function is done?

If it doesn't keep it's value throughout the game and is 'reset' everytime, what's the point? (Again I ask)

You won't be able to use it for leaderboards or anything, since the value is wiped out anyways.

Besides saving memory, what can I solve?
02-22-2003, 08:07 PM#6
Aiursrage2k
1) Portability
For example I have my own save cache function, it creates many local variables. If I were to use all globals I would need to declare many different variables (over 20) each time its used in a map, but using locals I can get it down to 4.
02-22-2003, 08:15 PM#7
Guest
Quote:
Originally posted by sid67
Locals are actually very useful. They are used when you only need that variable for THAT function. Local variables disappear when the function that uses them is done. Global variables hang around for ever, so they take up more memory.

Do we know for sure that's how JASS works?
Quote:
From an organization standpoint, locals can also be very useful. For one thing, globals have to be declared at the beginning of the war3map.j file. That's why you have to create them in the WE variable list. Locals, on the other hand, only have to be declared at the beginning of a FUNCTION.

No declaring variables at arbitrary locations? No fun :(

Quote:
I wouldn't say that locals are "better" than globals. Afterall, in many cases, you may need to store a variable for later use or maybe you even WANT to be able to change the variable from a different function.
Can't you just pass it by reference? Or is there no such thing in JASS? Is everything passed by value?


Also, why does everyone use x for loop control? Is there something wrong with using the conventional i? emote_confused
02-22-2003, 09:43 PM#8
ChronOmega
it doesnt matter wether you use x or i or even ~ if you want ti is just what you want, thats all
02-22-2003, 10:00 PM#9
Guest
Ok, just thought it was either some quirk of JASS or some other convention
02-22-2003, 10:09 PM#10
FyreDaug
its just a variable, name/letter doesn't matter, aslong as you keep the reference the same.
02-23-2003, 01:36 AM#11
Guest
Quote:
FyreDaug: If it doesn't keep it's value throughout the game and is 'reset' everytime, what's the point? (Again I ask)

The point is that sometimes this is EXACTLY what you want to do. And you are also missing the point that local variables are immune to outside influence in EVERY instance that function runs. For example, take a function that did the following:

Code:
function MeaninglessTest takes nothing returns nothing
   set udg_GlobalX = false
   TriggerSleepAction( 5 )     // A five second "Wait"
   set udg_GlobalX = true
endfunction

This function sets your GlobalX to false, then 5 seconds later sets it to true. Well if you ran this function every 3 seconds, your going to have the functions fighting over the value of GlobalX.

On the other hand, if you used a local like this:

Code:
function MeaninglessTest takes nothing returns nothing
   local boolean x = false
   TriggerSleepAction( 5 )     // A five second "Wait"
   set x = true
endfunction

Then the local variable "x" will never get influenced from any outside influence, including a different instance of the same function. Keep in mind that these are both only example functions and that the local example has no practical use.

Quote:
Parkan: Also, why does everyone use x for loop control? Is there something wrong with using the conventional i?

I only use "x" because everyone is used to using "x" as a variable in basic math class. No reason not to use "i" at all. I really suggest you look at Magnus99's reference manual, Parkan. You obviously have programming experience. JASS should be a cinch for you.
02-23-2003, 01:41 AM#12
FyreDaug
Okay sid, I'm understanding better (I also have programming expirience in VB, but I didn't pay much attention.... damn), can you give a good reference to something using a local variable that is much better used as a local than global please?

Thanks alot.
02-23-2003, 01:45 AM#13
Guest
A counting loop. Let's say that you had a "wait" action in your counting loop. If you had multiple instances of that function running, then there are multiple loops running. When the 1st loop reached it's exit condition, ALL instances of that loop would exit. A local however, would be immune to that outside influence.
02-23-2003, 01:50 AM#14
FyreDaug
Is that similar to the "For integer A to B do" in WE?
02-23-2003, 03:56 AM#15
Guest
Quote:
Originally posted by FyreDaug
Is that similar to the "For integer A to B do" in WE?


An actual loop in JASS, as I understand it, can have any break condition, while a for construct will run B - A times no matter what. In fact, I strongly suspect internally the for construct is actually a loop with an "i == B" break condition.