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

[General] Does this leak? (local trigger related)

Status
Not open for further replies.
Level 11
Joined
Oct 11, 2012
Messages
711
Hi, I have two questions:
First, does this leak?
JASS:
function Trig_jiance takes nothing returns nothing
    local unit triggeru = udg_Hero[(1+GetPlayerId(GetTriggerPlayer()))]
    local real x = GetUnitX(triggeru)
    local real y = GetUnitY(triggeru)
    local integer i = 0
    local string msgx = R2S(x)
    local string msgy = R2S(y)
    loop
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"X:"+msgx)
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"Y:"+msgy)
        set i = i + 1
        exitwhen i == 7
    endloop
    set triggeru = null
endfunction
function InitTrig_jiance takes nothing returns nothing
    local integer index
    local trigger t = CreateTrigger()
    set index = 0
    loop
        call TriggerRegisterPlayerChatEvent(t, Player(index), "-xy", true)
        set index = index + 1
        exitwhen index == 7
    endloop
    call TriggerAddAction( t, function Trig_jiance)
    set t = null
endfunction

Second, how should I use local trigger? Does it leak? Should I remove use TriggerRemoveAction? I noticed this issue or issues when I found a post on thehelper website, which was posted by PurgeandFire six years ago.. LOL
 
Level 9
Joined
Dec 3, 2010
Messages
162
1) From what I can tell, there's no leak.

2) Local trigger doesn't leak. However, do note that there is a leak when you destroy a trigger that has an action. Thus, you need to use TriggerRemoveAction before destroying a trigger, if you are ever planning to destroy it.

A better solution would be to not use Trigger Actions altogether. Instead, stick to using conditions:
JASS:
function Trig_jiance takes nothing returns boolean // notice it has to return boolean
    local unit triggeru = udg_Hero[(1+GetPlayerId(GetTriggerPlayer()))]
    local real x = GetUnitX(triggeru)
    local real y = GetUnitY(triggeru)
    local integer i = 0
    local string msgx = R2S(x)
    local string msgy = R2S(y)
    loop
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"X:"+msgx)
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"Y:"+msgy)
        set i = i + 1
        exitwhen i == 7
    endloop
    set triggeru = null

    return false // simply return false at the end
endfunction
function InitTrig_jiance takes nothing returns nothing
    local integer index
    local trigger t = CreateTrigger()
    set index = 0
    loop
        call TriggerRegisterPlayerChatEvent(t, Player(index), "-xy", true)
        set index = index + 1
        exitwhen index == 7
    endloop
    call TriggerAddCondition(t, Condition(function Trig_jianc)) // no longer TriggerAddAction
    set t = null
endfunction
Destroying a trigger with conditions doesn't leak IIRC.
 
Level 11
Joined
Oct 11, 2012
Messages
711
1) From what I can tell, there's no leak.

2) Local trigger doesn't leak. However, do note that there is a leak when you destroy a trigger that has an action. Thus, you need to use TriggerRemoveAction before destroying a trigger, if you are ever planning to destroy it.

A better solution would be to not use Trigger Actions altogether. Instead, stick to using conditions:
JASS:
function Trig_jiance takes nothing returns boolean // notice it has to return boolean
    local unit triggeru = udg_Hero[(1+GetPlayerId(GetTriggerPlayer()))]
    local real x = GetUnitX(triggeru)
    local real y = GetUnitY(triggeru)
    local integer i = 0
    local string msgx = R2S(x)
    local string msgy = R2S(y)
    loop
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"X:"+msgx)
        call DisplayTimedTextToPlayer(Player(i),0,0,15,"Y:"+msgy)
        set i = i + 1
        exitwhen i == 7
    endloop
    set triggeru = null

    return false // simply return false at the end
endfunction
function InitTrig_jiance takes nothing returns nothing
    local integer index
    local trigger t = CreateTrigger()
    set index = 0
    loop
        call TriggerRegisterPlayerChatEvent(t, Player(index), "-xy", true)
        set index = index + 1
        exitwhen index == 7
    endloop
    call TriggerAddCondition(t, Condition(function Trig_jianc)) // no longer TriggerAddAction
    set t = null
endfunction
Destroying a trigger with conditions doesn't leak IIRC.

Thanks for the replay. What if I do use action and wanna prevent leak? I got the following from a website, what does the "some variables" mean in it? Does it mean the "name" of the action?

JASS:
call DisableTrigger(GetTriggeringTrigger()) // Disables the trigger 
call DestroyBoolExpr(GetTriggeringTrigger(),some variable) // Destroys boolexpr
call TriggerRemoveAction(GetTriggeringTrigger(),some variable) // Removes action

set TriggerActionVar=null // Nulls trigger action
set BoolExprVar=null // Nulls the boolexpr

call DestroyTrigger(GetTriggeringTrigger()) // Destroy
 
Level 9
Joined
Dec 3, 2010
Messages
162
What if I do use action and wanna prevent leak?

Is there a specific reason why you need to use action?

TriggerRemoveAction - the 2nd parameter is of type triggeraction. This variable is returned when you call the TriggerAddAction native.

JASS:
globals
    triggeraction Action
endglobals

function Example takes nothing returns nothing
    // removes the action you store earlier on
    call TriggerRemoveAction(yourTrigger, Action)
endfunction

function Init takes nothing returns nothing
    // stores the triggeraction returned from TriggerAddAction
    set Action = TriggerAddAction(yourTrigger, function yourFunction)
endfunction
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Are you going to use dynamic triggers? Judging from the first post, I think you're not going to. Using dynamic triggers for this kind of application is overkill.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Is there a specific reason why you need to use action?

TriggerRemoveAction - the 2nd parameter is of type triggeraction. This variable is returned when you call the TriggerAddAction native.

JASS:
globals
    triggeraction Action
endglobals

function Example takes nothing returns nothing
    // removes the action you store earlier on
    call TriggerRemoveAction(yourTrigger, Action)
endfunction

function Init takes nothing returns nothing
    // stores the triggeraction returned from TriggerAddAction
    set Action = TriggerAddAction(yourTrigger, function yourFunction)
endfunction

Alright, I will use condition only from now on. LOL.
One last question: can I disable the local trigger "t" just as disabling a global trigger?
JASS:
function Init takes nothing returns nothing
    local t = CreateTrigger()
    call DisableTrigger(t)
    ..........
endfunction

There's no leak but there is a very very small leak about the strings but it cant removed so nvm xD.

Alright, LOL. Thanks
 
Last edited:
Level 22
Joined
Sep 24, 2005
Messages
4,821
Yes you can, the only difference with a local and a global variable is it's scope. Are you using dynamic triggers? I mean for this?

EDIT:
Hey your script is wrong, t is a timer not a trigger.
 
Level 22
Joined
Sep 24, 2005
Messages
4,821
Dynamic triggers are using triggers and then destroying them, for example, you assign a damage event to a unit, the only available damage event in jass is attachable to a unit and not generic so you need to assign and destroy the said event for the said unit, and since there is no way to remove events from triggers, you destroy triggers when a unit is removed from the game and create them when you introduce new units to the game.

EDIT: It's not gonna hurt to use Trigger Actions for those kinds of application, GUI Triggers use them with no problems.
 
Level 9
Joined
Dec 3, 2010
Messages
162
Alright, I will use condition only from now on. LOL.
One last question: can I disable the local trigger "t" just as disabling a global trigger?
JASS:
function Init takes nothing returns nothing
    local t = CreateTrigger()
    call DisableTrigger(t)
    ..........
endfunction



Alright, LOL. Thanks

Should be local trigger t = CreateTrigger() :p

Well yes, you can disable the trigger as long as you have a reference to it. In your case, I would imagine the only way to retrieve the trigger would be by storing it somewhere or GetTriggeringTrigger() (this is only if you're trying to disable the trigger outside of the current scope).

Ultimately from what I see on your first post, TriggerRemoveAction wouldn't be needed at all since you're not destroying triggers.
 
Level 11
Joined
Oct 11, 2012
Messages
711
Dynamic triggers are using triggers and then destroying them, for example, you assign a damage event to a unit, the only available damage event in jass is attachable to a unit and not generic so you need to assign and destroy the said event for the said unit, and since there is no way to remove events from triggers, you destroy triggers when a unit is removed from the game and create them when you introduce new units to the game.

EDIT: It's not gonna hurt to use Trigger Actions for those kinds of application, GUI Triggers use them with no problems.

Should be local trigger t = CreateTrigger() :p

Well yes, you can disable the trigger as long as you have a reference to it. In your case, I would imagine the only way to retrieve the trigger would be by storing it somewhere or GetTriggeringTrigger() (this is only if you're trying to disable the trigger outside of the current scope).

Ultimately from what I see on your first post, TriggerRemoveAction wouldn't be needed at all since you're not destroying triggers.

To destroy trigger you have to remove the actions or use only condition.
Anyway using only conditions is cooooool ;)

Big thanks to all of you and +Rep!
 
Status
Not open for further replies.
Top