- Joined
- Oct 29, 2007
- Messages
- 1,184
Local or global variables?
Locals have the advantage of making things MUI while globals need to have all these extra variables stuff added and custom value to make MUI.
function forehead takes nothing returns nothing
local integer i // Oh, hey, let's take away 32 bits of memory (or whatever warcraft ints use) to store this variable in.
endfunction //now we're done with it, give the 32 bits back
I don't think there's a difference performance-wise. Locals are certainly easier to work with when you have just one function, but globals are easier when you have several. Locals take time to pass between different functions, whereas with globals, that's not necessary.
Here's the bad (but almost negligable) thing about globals: they never stop taking memory.
When you declare a local, it sets aside memory to use to store its information:
JASS:function forehead takes nothing returns nothing local integer i // Oh, hey, let's take away 32 bits of memory (or whatever warcraft ints use) to store this variable in. endfunction //now we're done with it, give the 32 bits back
With globals, there's no endfunction that magically causes them all to disappear, they're there the entire game (unless you null them [handles only], which means they can't be used anymore). Also, handles such as units use a lot more memory than ints. It's not really worth considering most of the time, but when you have over 100 global variables that are only used in one trigger, it can eat up memory. But, like I said, most people don't have any problems running even the worst Warcraft maps anymore.
Of course, memory leaks with local handles are worse... you have to null those or the game never releases the memory (a unit variable isn't really a unit, it just "points" to the unit, which is actually a combination of reals/ints/bools, etc.) .
function forehead takes nothing returns nothing
local integer i // Oh, hey, let's take away 32 bits of memory (or whatever warcraft ints use) to store this variable in.
endfunction //now we're done with it, give the 32 bits back