• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[JASS] Very basic question regarding handles

Status
Not open for further replies.
Level 4
Joined
Jan 19, 2008
Messages
69
This is probably an incredibly stupid question.. but at what point should I null handles for max efficiency? I'd like to use my script as a reference.

It seemed natural to me that the handles should be null'ed only once they have been declared - as shown below:
JASS:
function SH_OL_conditions takes nothing returns nothing
    local trigger trig
    local unit caster
    local integer trigid
    local integer stats
    local effect sfx

    if GetSpellAbilityId() == SH_OL_spellid() then
        set caster = GetTriggerUnit()
        set stats = SH_OL_stats(SH_OL_getlevel(caster))
        set sfx = AddSpecialEffectTarget("Abilities\\Spells\\Orc\\Purge\\PurgeBuffTarget.mdl", caster, "origin")
        call SetHeroInt(caster, (GetHeroInt(caster, false)) + stats, true)
        call SetHeroAgi(caster, (GetHeroAgi(caster, false)) + stats, true)
        set trig = CreateTrigger()
        set trigid = GetHandleId (trig)
        call TriggerRegisterTimerEvent(trig, 0.2, true)
        call SaveTriggerActionHandle(udg_SH_hashtable , trigid, 0, TriggerAddAction(trig, function SH_OL_periodic))
        call SaveUnitHandle(udg_SH_hashtable, trigid, 1, caster)
        call SaveReal(udg_SH_hashtable, trigid, 2, 0.)
        call SaveReal(udg_SH_hashtable, trigid, 3, SH_OL_duration())
        call SaveInteger(udg_SH_hashtable, trigid, 4, stats)
        call SaveEffectHandle(udg_SH_hashtable, trigid, 5, sfx)
        set trig = null 
        set caster = null
        set sfx = null
    endif

endfunction

But looking at other people's code I tend to see this:
JASS:
function SH_OL_conditions takes nothing returns nothing
    local trigger trig
    local unit caster
    local integer trigid
    local integer stats
    local effect sfx

    if GetSpellAbilityId() == SH_OL_spellid() then
        set caster = GetTriggerUnit()
        set stats = SH_OL_stats(SH_OL_getlevel(caster))
        set sfx = AddSpecialEffectTarget("Abilities\\Spells\\Orc\\Purge\\PurgeBuffTarget.mdl", caster, "origin")
        call SetHeroInt(caster, (GetHeroInt(caster, false)) + stats, true)
        call SetHeroAgi(caster, (GetHeroAgi(caster, false)) + stats, true)
        set trig = CreateTrigger()
        set trigid = GetHandleId (trig)
        call TriggerRegisterTimerEvent(trig, 0.2, true)
        call SaveTriggerActionHandle(udg_SH_hashtable , trigid, 0, TriggerAddAction(trig, function SH_OL_periodic))
        call SaveUnitHandle(udg_SH_hashtable, trigid, 1, caster)
        call SaveReal(udg_SH_hashtable, trigid, 2, 0.)
        call SaveReal(udg_SH_hashtable, trigid, 3, SH_OL_duration())
        call SaveInteger(udg_SH_hashtable, trigid, 4, stats)
        call SaveEffectHandle(udg_SH_hashtable, trigid, 5, sfx)
    endif

    set trig = null 
    set caster = null
    set sfx = null

endfunction
 
Level 20
Joined
Jul 14, 2011
Messages
3,213
Wait... the variable may be created without a value, but still created. Wouldn't they leak if the "if" conditions aren't met?

And the value these variables get inside the "if" doesn't remain on the variable outside/after the "if"? It does, it has to.

I always tough on using these variables in all the trigger (even the same for different stuff) and nullying at the end just those that aren't nulled during the trigger (like first of group unit)

Does this means that local variables are harmless until you assign them a value?
 
Level 5
Joined
May 6, 2013
Messages
125
Handle Variables don't leak. The Handles those Variables are assigned to leak because the internal refernce counter of wc is too dumb to decrease itself when a local gets out of scope. No Handle assigned, no leak.
 
Status
Not open for further replies.
Top