• 🏆 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!

[Solved] Custom Function Leak?

Status
Not open for further replies.
Level 17
Joined
Mar 21, 2011
Messages
1,597
Hi
I just started messing around with the world editor again and have a simple question.
I created a custom function "CreateFloatingText" which basically just creates a floating text and sets values. Now, as i want to return the texttag, i cannot null it at the end of the function. Instead, i null it in the example trigger. Is that correct?


JASS:
library FloatingText initializer Init


   
    function CreateFloatingText takes string text, player p, real x, real y, real speed, real angle, real duration, real fade, boolean permanent returns texttag
        local texttag t = CreateTextTag()
        call SetTextTagText(t, text, 0.0345)
        call SetTextTagPos(t, x, y, 100)
        if (GetLocalPlayer() != p) then
            call SetTextTagVisibility(t, false)
        endif
        call SetTextTagVelocity(t, (speed*0.071/128)*Cos(angle*bj_DEGTORAD), (speed*0.071/128)*Sin(angle*bj_DEGTORAD))
        call SetTextTagPermanent(t, permanent)
        call SetTextTagLifespan(t, duration)
        call SetTextTagFadepoint(t, fade)
        return t
    endfunction
   


endlibrary

JASS:
local texttag t = CreateFloatingText("123abc", GetTriggerPlayer(), GetCameraTargetPositionX(), GetCameraTargetPositionY(), 80.00, GetRandomReal(1.00, 360.0), 3.00, 2.00, false)
set t = null
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,201
One can also use function parameters to return handles without leaking. Unlike local declared local handle variables, parameter declared local handle variables do correctly decrement reference counter on function return.

JASS:
    function CreateFloatingTextCore takes string text, player p, real x, real y, real speed, real angle, real duration, real fade, boolean permanent, texttag nulltext returns texttag
        set nulltext = CreateTextTag()
        call SetTextTagText(nulltext, text, 0.0345)
        call SetTextTagPos(nulltext, x, y, 100)
        if (GetLocalPlayer() != p) then
            call SetTextTagVisibility(nulltext, false)
        endif
        call SetTextTagVelocity(nulltext, (speed*0.071/128)*Cos(angle*bj_DEGTORAD), (speed*0.071/128)*Sin(angle*bj_DEGTORAD))
        call SetTextTagPermanent(nulltext, permanent)
        call SetTextTagLifespan(nulltext, duration)
        call SetTextTagFadepoint(nulltext, fade)
        return nulltext
    endfunction

call CreateFloatingTextCore(..., null)

    // Maybe a wrapper to mask the parameter?
    function CreateFloatingText takes string text, player p, real x, real y, real speed, real angle, real duration, real fade, boolean permanent returns texttag
        return CreateFloatingTextCore(text, p, x, y, speed, angle, duration, fade, permanent, null)
    endfunction

call CreateFloatingText(...)

I wonder when Blizzard will fix the bug...
 
Level 17
Joined
Mar 21, 2011
Messages
1,597
Ok thanks! That way i can also call the function without saving the output into a variable (besides bj_lastCreatedTextTag)


JASS:
library FloatingText initializer Init


 
    function CreateFloatingText takes string text, player p, real x, real y, real speed, real angle, real duration, real fade, boolean permanent returns texttag
        set bj_lastCreatedTextTag = CreateTextTag()
        call SetTextTagText(bj_lastCreatedTextTag, text, 0.0345)
        call SetTextTagPos(bj_lastCreatedTextTag, x, y, 100)
        if (GetLocalPlayer() != p) then
            call SetTextTagVisibility(bj_lastCreatedTextTag, false)
        endif
        call SetTextTagVelocity(bj_lastCreatedTextTag, (speed*0.071/128)*Cos(angle*bj_DEGTORAD), (speed*0.071/128)*Sin(angle*bj_DEGTORAD))
        call SetTextTagPermanent(bj_lastCreatedTextTag, permanent)
        call SetTextTagLifespan(bj_lastCreatedTextTag, duration)
        call SetTextTagFadepoint(bj_lastCreatedTextTag, fade)
        return t
    endfunction
 


endlibrary

JASS:
call CreateFloatingText("123abc", GetTriggerPlayer(), GetCameraTargetPositionX(), GetCameraTargetPositionY(), 80.00, GetRandomReal(1.00, 360.0), 3.00, 2.00, false)


that should work?
 
Status
Not open for further replies.
Top