HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

Nullifing variables passed to other functions

07-29-2006, 10:43 PM#1
The_AwaKening
This code was made by Blade.dk and placed in my custom script. Maybe I'm wrong, but shouldn't whichUnit be nullified at the end of this function? I'm asking because I have a lot of my own functions in my custom script as well that I am trying to optimize.
Collapse JASS:
function SetUnitMaxState takes unit whichUnit, unitstate whichUnitState, integer newVal returns boolean
    local integer c = newVal-R2I(GetUnitState(whichUnit, whichUnitState))
    local integer i = MaxStateModifierId(whichUnitState)
    if i == 0 then
        return false
    endif
    if c > 0 then
        loop
            exitwhen c == 0
            call UnitAddAbility(whichUnit, i)
            if c >= 100 then
                set c = c - 100
                call SetUnitAbilityLevel(whichUnit, i, 4)
            elseif c >= 10 then
                set c = c - 10
                call SetUnitAbilityLevel(whichUnit, i, 3)
            else
                set c = c - 1
                call SetUnitAbilityLevel(whichUnit, i, 2)
            endif
            call UnitRemoveAbility(whichUnit, i)
        endloop
    elseif c < 0 then
        set c = -c
        loop
            exitwhen c == 0
            call UnitAddAbility(whichUnit, i)
            if c >= 100 then
                set c = c - 100
                call SetUnitAbilityLevel(whichUnit, i, 7)
            elseif c >= 10 then
                set c = c - 10
                call SetUnitAbilityLevel(whichUnit, i, 6)
            else
                set c = c - 1
                call SetUnitAbilityLevel(whichUnit, i, 5)
            endif
            call UnitRemoveAbility(whichUnit, i)
        endloop
    endif
    return t
endfunction
07-29-2006, 10:46 PM#2
Alevice
Parameters do not leak IIRC. A new handle is not being created at the function.
07-29-2006, 10:48 PM#3
The)TideHunter(
Yea, only worry about locals, they are the only thing that is the only thing that can cause problems.
07-29-2006, 10:56 PM#4
The_AwaKening
Ok, good to know, but for all the optimizations I've done and I have nullified them already, that shouldn't cause a problem either? If so, I have to go back and remove all of them.
07-29-2006, 10:58 PM#5
The)TideHunter(
I doubt it, but i wouldent take the risk, im still not 100% sure what nulling can do, i just know it prevents a minor leak and recylcles the index (sometimes as we are disscussing).
If i was you, i would remove the nulling, its a waste of bytes and dosent help atall =/.
07-29-2006, 11:06 PM#6
Captain Griffen
Actually nulling shouldn't recycle the index (as far as we know) and only does so for timer variables. Nulling destroyers the pointer to a local variable that extends a type of handle (not local handle variables), and should not affect the object itself, or it's index number.

Anyway, passed variables do not need to be nulled, only after you declare a local variable of a type that extends a handle:

Collapse JASS:
type event              extends     handle  // a reference to an event registration
type player             extends     handle  // a single player reference
type widget             extends     handle  // an interactive game object with life
type unit               extends     widget  // a single unit reference
type destructable       extends     widget
type item               extends     widget
type ability            extends     handle
type buff               extends     ability
type force              extends     handle
type group              extends     handle
type trigger            extends     handle
type triggercondition   extends     handle
type triggeraction      extends     handle
type timer              extends     handle
type location           extends     handle
type region             extends     handle
type rect               extends     handle
type boolexpr           extends     handle
type sound              extends     handle
type conditionfunc      extends     boolexpr
type filterfunc         extends     boolexpr
type unitpool           extends     handle
type itempool           extends     handle
type race               extends     handle
type alliancetype       extends     handle
type racepreference     extends     handle
type gamestate          extends     handle
type igamestate         extends     gamestate
type fgamestate         extends     gamestate
type playerstate        extends     handle
type playerscore        extends     handle
type playergameresult   extends     handle
type unitstate          extends     handle
type aidifficulty       extends     handle

type eventid            extends     handle
type gameevent          extends     eventid
type playerevent        extends     eventid
type playerunitevent    extends     eventid
type unitevent          extends     eventid
type limitop            extends     eventid
type widgetevent        extends     eventid
type dialogevent        extends     eventid
type unittype           extends     handle

type gamespeed          extends     handle
type gamedifficulty     extends     handle
type gametype           extends     handle
type mapflag            extends     handle
type mapvisibility      extends     handle
type mapsetting         extends     handle
type mapdensity         extends     handle
type mapcontrol         extends     handle
type playerslotstate    extends     handle
type volumegroup        extends     handle
type camerafield        extends     handle
type camerasetup        extends     handle
type playercolor        extends     handle
type placement          extends     handle
type startlocprio       extends     handle
type raritycontrol      extends     handle
type blendmode          extends     handle
type texmapflags        extends     handle
type effect             extends     handle
type effecttype         extends     handle
type weathereffect      extends     handle
type terraindeformation extends     handle
type fogstate           extends     handle
type fogmodifier        extends     handle
type dialog             extends     handle
type button             extends     handle
type quest              extends     handle
type questitem          extends     handle
type defeatcondition    extends     handle
type timerdialog        extends     handle
type leaderboard        extends     handle
type multiboard         extends     handle
type multiboarditem     extends     handle
type trackable          extends     handle
type gamecache          extends     handle
type version            extends     handle
type itemtype           extends     handle
type texttag            extends     handle
type attacktype         extends     handle
type damagetype         extends     handle
type weapontype         extends     handle
type soundtype          extends     handle
type lightning          extends     handle
type pathingtype        extends     handle
type image              extends     handle
type ubersplat          extends     handle
07-29-2006, 11:15 PM#7
The_AwaKening
Brings me to another leak I need cleaned up.
Collapse JASS:
function SoundLeak takes nothing returns nothing
    call StartSound( gg_snd_Hint )
endfunction
The above leaks I'm assuming. Would the below function be the correct way to fix it?
Collapse JASS:
function SoundLeak takes nothing returns nothing
 local sound hint=gg_snd_Hint
    call StartSound( hint )
    call KillSoundWhenDone( hint )
 set hint=null
endfunction

If I need to do more with the code, please show me.
07-29-2006, 11:20 PM#8
The)TideHunter(
Nope, gg_snd_Hint is a global, its fine as it is.
If you wanted to KillSoundWhenDone, just use the global again, using a local twice is only like using a global twice.

Collapse JASS:
function SoundLeak takes nothing returns nothing
    call StartSound(gg_snd_Hint)
    call KillSoundWhenDone(gg_snd_Hint)
endfunction
07-29-2006, 11:24 PM#9
The_AwaKening
OK, that makes sense. Thanks
07-30-2006, 10:28 AM#10
Captain Griffen
Actually that wouldn't be a local sound. That would just be a local pointer to a global sound, I believe. You'd have to create a new sound to make it local.

KillSoundWhenDone would destroy the global after it was done, so you wouldn't be able to use it again.

Global sounds cannot overlap with each other, as you cannot play the same variable twice simultaneously. For that, you'd need to use dynamically created sounds, rather than ones created at start up by the sound editor.