I'd really like to know what the benefit is of a local variable over a global one as a global variable allows you to use it in a new trigger if you so desire. Any scenarios where a local variable would be better than a global?
Local variables are great when you plan to have a trigger run several times at once.
Let's say you make a new spell and call it "Timed Death". You trigger it's effect: It's supposed to wait 5 seconds, then kill the targeted unit.
Not very hard to program using a global variable.
But if you use the spell twice in a row without waiting for the 5 seconds delay, then your first instance of that spell gets messed up and accidently kills the target of the second instance.
With local variables you can make sure it won't mess up.
I was doing a search on Google for some more detailed specifications on global variables in SC2 and this came up first so I thought—seeing as it probably gets a lot of views—I'd add a few things to it for people's edification.
If local variables are anything like they are in other programs, then their main benefit is their local scope—they are limited in scope to the part of code they are defined in (i.e. the particular function). In this way you can have multiple variables of the same name that don't interfere with each other if their respective scopes are different. This is the "why" behind what s3rius explained above.
Another advantage is that (in many languages), local variables are automatically deleted and memory reallocated after they are used. This clears the programmer of what can sometimes be hefty memory management duties when dealing with scores of large global variables, like arrays/tables.
I'd really like to know what the benefit is of a local variable over a global one as a global variable allows you to use it in a new trigger if you so desire. Any scenarios where a local variable would be better than a global?
Local variables are great when you plan to have a trigger run several times at once.
Let's say you make a new spell and call it "Timed Death". You trigger it's effect: It's supposed to wait 5 seconds, then kill the targeted unit. Not very hard to program using a global variable. But if you use the spell twice in a row without waiting for the 5 seconds delay, then your first instance of that spell gets messed up and accidently kills the target of the second instance.
With local variables you can make sure it won't mess up.
@s3rius: Go
Aha I see, that's a clear scenario of when the local beats global. Thanks!
I was doing a search on Google for some more detailed specifications on global variables in SC2 and this came up first so I thought—seeing as it probably gets a lot of views—I'd add a few things to it for people's edification.
If local variables are anything like they are in other programs, then their main benefit is their local scope—they are limited in scope to the part of code they are defined in (i.e. the particular function). In this way you can have multiple variables of the same name that don't interfere with each other if their respective scopes are different. This is the "why" behind what s3rius explained above.
Another advantage is that (in many languages), local variables are automatically deleted and memory reallocated after they are used. This clears the programmer of what can sometimes be hefty memory management duties when dealing with scores of large global variables, like arrays/tables.