| 12-24-2006, 04:33 AM | #1 |
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 |
Depends on the function entirely. Local variables are usually used to help make functions MUI. |
| 12-24-2006, 05:06 AM | #3 |
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 |
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 |
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... 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 |
This might be useful though: 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 | |
Quote:
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 |
then would making variables for variables be bad? like this one 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 |
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 |
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 |
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: be a memory leak? Also, would this variable be useless then? 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 |
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 |
So you should only use variables when you use waits? |
| 12-27-2006, 05:52 PM | #14 |
Wrong. Read again what PipeDream said, you greatly oversimplified his statement. It seems some examples are in order. 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 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 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 |
| 12-27-2006, 06:54 PM | #15 |
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. |
