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

What is more efficient way about add actions/conditions to a trigger?

Status
Not open for further replies.
Level 24
Joined
Jun 26, 2020
Messages
1,850
If I create a trigger I can normally do:
vJASS:
function Actions takes nothing returns nothing
    //bla bla
endfunction

function Conditions takes nothing returns boolean
    return bool
endfunction

function Init takes nothing returns nothing
    set t=CreateTrigger
    call TriggerRegisterEvent(t,<Whatever>)
    call TriggerAddCondition(t,Condition(function Conditions))
    call TriggerAddAction(t,function Actions)
endfunction
But we can also just do
vJASS:
function Actions takes nothing returns nothing
    if bool then
        //bla bla
    endif
endfunction

function Init takes nothing returns nothing
    set t=CreateTrigger
    call TriggerRegisterEvent(t,<Whatever>)
    call TriggerAddAction(t,function Actions)
endfunction
Or
vJASS:
function Conditions takes nothing returns nothing
    if bool then
        //bla bla
    endif
endfunction

function Init takes nothing returns nothing
    set t=CreateTrigger
    call TriggerRegisterEvent(t,<Whatever>)
    call TriggerAddCondition(t,Condition(function Conditions)) //Yes the condition can accept a function that returns nothing
endfunction
But if the actions need local variables, to save steps we can use the first way:
vJASS:
function Actions takes nothing returns nothing
    local type x
    //bla bla
endfunction

function Conditions takes nothing returns boolean
    return bool
endfunction

function Init takes nothing returns nothing
    set t=CreateTrigger
    call TriggerRegisterEvent(t,<Whatever>)
    call TriggerAddCondition(t,Condition(function Conditions))
    call TriggerAddAction(t,function Actions)
endfunction
Or is more efficient this?:

vJASS:
function Actions takes nothing returns nothing
    local type x
    //bla bla
endfunction

function Conditions takes nothing returns nothing
    if bool then
       call Actions()
   endif
endfunction

function Init takes nothing returns nothing
    set t=CreateTrigger
    call TriggerRegisterEvent(t,<Whatever>)
    call TriggerAddAction(t,function Conditions)
endfunction
What do you think?
 

Wrda

Spell Reviewer
Level 25
Joined
Nov 18, 2012
Messages
1,870
You overthink too much, that's what I think.
If you're a speed freak, people will only use TriggerAddCondition because it is supposedly faster.
Otherwise, most people will just use TriggerAddAction.
I don't get your point on "But if the actions need local variables, to save steps we can use the first way:". You can make local variables in ANY functions, are you trying to save bytes?
Also, in your last code, wtf is the point of having a TriggerAction that runs another function? How did you even consider that efficient?
 
Level 24
Joined
Jun 26, 2020
Messages
1,850
You overthink too much, that's what I think.
If you're a speed freak, people will only use TriggerAddCondition because it is supposedly faster.
Otherwise, most people will just use TriggerAddAction.
I don't get your point on "But if the actions need local variables, to save steps we can use the first way:". You can make local variables in ANY functions, are you trying to save bytes?
Also, in your last code, wtf is the point of having a TriggerAction that runs another function? How did you even consider that efficient?
Well, that's because I'm starting to use RegisterNativeEvent, and sometimes I "had" to do that, so I'm here to ask if is better or not, I mean declare local variables is extra memory or steps that I can avoid or is irrelevant? like this 2 examples:
vJASS:
function Actions takes nothing returns nothing
    local type x //Extra step every time I need to check
    if bool then
       //bla bla
    endif
endfunction

function Init takes nothing returns nothing
    call RegisterEvent(<Whatever>,function Actions)
endfunction

vJASS:
function Actions takes nothing returns nothing
    local type x
    //bla bla
endfunction

function Conditions takes nothing returns nothing
    //I check without doing that step
    if bool then
       call Actions()
    endif
endfunction

function Init takes nothing returns nothing
    call RegisterEvent(<Whatever>,function Conditions)
endfunction
If I use Lua I don't have this doubt because I can just do:
Lua:
function Init()
    RegisterEvent(<Whatever>,function()
        if not bool then
            return
        end
        local x
        --bla bla
    end)
end
 
Last edited:

Wrda

Spell Reviewer
Level 25
Joined
Nov 18, 2012
Messages
1,870
You're not checking anything at moment, you're just declaring a local variable. Whether it takes more memory, in terms of map file, the 2nd one will take more. If you mean in game, I don't know, however, they're both irrelevant. What you do should avoid is calling a function to do your stuff when was already called and could do it perfectly there. In order words, don't the 2nd one.
 
Status
Not open for further replies.
Top