HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Local variables always good?

12-24-2006, 04:33 AM#1
Joker
lets say you have a function with 1 action. Would using a local make it any better or worse?
12-24-2006, 04:36 AM#2
wyrmlord
Depends on the function entirely. Local variables are usually used to help make functions MUI.
12-24-2006, 05:06 AM#3
darkwulfv
It would look neater. Thats about all.
But if you do, you'd better clean leaks or your doing nothing but hurting the map.
12-24-2006, 10:17 AM#4
Captain Griffen
If you don't need a variable, don't use a variable.

If you only have one action, then you only need a variable if you have to clean up leaks.

Locals are no faster than globals (though they are both faster than array lookups).
12-24-2006, 11:32 AM#5
SFilip
Define a local only if you want to use it at least twice (or to clean the leaks, as stated above).
For example using a local like this is simply senseless...
Collapse JASS:
function test takes nothing returns nothing
    local string a = "hi, i am a string"
    call BJDebugMsg(a)
endfunction
12-24-2006, 11:53 AM#6
Captain Griffen
This might be useful though:

Collapse JASS:
function X takes nothing returns nothing
    local unit u = GetTriggerUnit()
    call CreateUnit(GetOwningPlayer(u), GetUnitTypeId(u), GetUnitX(u), GetUnitY(u), GetUnitFacing(u))
    set u = null
endfunction

In this case, a local can reduce the number of function calls. If you only use something once, though, a variable isn't needed (unless it needs to be cleaned).
12-24-2006, 12:42 PM#7
Daelin
Quote:
Originally Posted by SFilip
Define a local only if you want to use it at least twice (or to clean the leaks, as stated above).
For example using a local like this is simply senseless...
Collapse JASS:
function test takes nothing returns nothing
    local string a = "hi, i am a string"
    call BJDebugMsg(a)
endfunction

Not necessarily. Sometimes locals are useful for debugging, especially if concatenating long strings. You can split them into several pieces to spot the error much easier. I'm sure there are other situations where variables are useful for debugging.

~Daelin
12-24-2006, 03:20 PM#8
Joker
then would making variables for variables be bad? like this one
Collapse JASS:
function X takes nothing returns nothing
    local unit u = GetTriggerUnit()
    local real ux = GetUnitX(u)
    local real uy = GetUnitY(u)
    call CreateUnit(GetOwningPlayer(u), GetUnitTypeId(u), ux, uy, GetUnitFacing(u))
    set u = null
endfunction
12-24-2006, 03:24 PM#9
wyrmlord
You wouldn't want the two real variables in that case since they are only used once, but keep the unit variable because it is used 3 times.
12-24-2006, 03:29 PM#10
Captain Griffen
They can be useful simply for the sake of clarity, or being able to change stuff easily, but that only really applies in more complex things.
12-26-2006, 11:50 PM#11
Anopob
Coming back to this, I have a question. If you're using a variable only once then it's useless, but then why would
Trigger:
Untitled Trigger 001
Events
Conditions
Collapse Actions
Unit - Create 1 Footman for Player 1 (Red) at (Center of (Playable map area)) facing Default building facing degrees

be a memory leak? Also, would this variable be useless then?

Collapse JASS:
function test takes nothing returns nothing
    local unit target = GetSpellAbilityUnit()
    call SetUnitLifePercentBJ( target, 50.00 )
    set target = null
endfunction

Please help, I'm confused now lol.
12-27-2006, 12:23 AM#12
PipeDream
Study how it's computed. Playable map area returns bj_PLAYABLE_MAP_RECT or some such while center of does GetRectCenter() or some such which creates a location-look them up in Blizzard.j.

Yes, that's useless in general, but with the event callbacks it's good to use variables as many of those for spells aren't thread safe. E.g. they'll get mauled by TriggerSleepAction().
12-27-2006, 05:23 PM#13
Anopob
So you should only use variables when you use waits?
12-27-2006, 05:52 PM#14
Anitarf
Wrong. Read again what PipeDream said, you greatly oversimplified his statement.

It seems some examples are in order.

Collapse JASS:
function test1 takes nothing returns nothing
    local unit target = GetSpellAbilityUnit()
    call SetUnitLifePercentBJ( target, 50.00 )
    set target = null
endfunction

function test2 takes nothing returns nothing
    call SetUnitLifePercentBJ( GetSpellAbilityUnit(), 50.00 )
endfunction
In this case, the functions work the same. Clearly, test2 should take less time to execute.
Collapse JASS:
function test1 takes nothing returns nothing
    local unit target = GetSpellAbilityUnit()
    call PolledWait(5.0)
    call SetUnitLifePercentBJ( target, 50.00 )
    set target = null
endfunction

function test2 takes nothing returns nothing
    call PolledWait(5.0)
    call SetUnitLifePercentBJ( GetSpellAbilityUnit(), 50.00 )
endfunction
In this case, however, test2 can bug because most if not all spell event responses are bugged.
Collapse JASS:
function test1 takes nothing returns nothing
    local unit target = GetTriggerUnit()
    call PolledWait(5.0)
    call SetUnitLifePercentBJ( target, 50.00 )
    set target = null
endfunction

function test2 takes nothing returns nothing
    call PolledWait(5.0)
    call SetUnitLifePercentBJ( GetTriggerUnit(), 50.00 )
endfunction
These two functions can replace the ones given in previous examples and they will both work the same, this is because GetTriggerUnit() is not bugged like the cast event responses.
12-27-2006, 06:54 PM#15
Anopob
Oh okay. Thanks.

EDIT: So in my example the variable would be useless. And in your 3rd example, the test1 function is slower than function 2, though they do the same thing.