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

This trigger seems to run twice per cast o.O

Status
Not open for further replies.
Level 13
Joined
Jan 2, 2016
Messages
973
JASS:
scope SEF initializer InitTrig_SEF

    globals
        boolean array SB
        boolean array EB
        boolean array FB
        unit array SU
        unit array EU
        unit array FU
        unit array SEF_U
        real array SEF_X
        real array SEF_Y
        timer array SEF_T
        integer array SEF_C
        integer array TC
    endglobals
    
    private function PeriodicCheck takes nothing returns nothing
        local timer t = GetExpiredTimer()
        local integer id = GetHandleId(t)
        local unit c = LoadUnitHandle(udg_Table, id, 'cast')
        local unit u = LoadUnitHandle(udg_Table, id, 'targ')
        local integer i = GetPlayerId(GetOwningPlayer(c))
        set TC[i] = TC[i] + 1
        if TC[i] >= 20 or IsUnitDeadBJ(c) or IsUnitDeadBJ(u) then
            call DestroyLightning(LoadLightningHandle(udg_Table, id, 'ligh'))
        endif
        set t = null
        set c = null
        set u = null
    endfunction
    
    function SEF_Init takes unit u, unit c returns nothing
        local timer t = GetFreeTimer()
        local real x = GetUnitX(u)
        local real y = GetUnitY(u)
        local real xc = GetUnitX(c)
        local real yc = GetUnitY(c)
        local integer id = GetHandleId(t)
        call SaveLightningHandle(udg_Table, id, 'ligh', AddLightning("SPLK", true, x, y, xc, yc))
        call SaveUnitHandle(udg_Table, id, 'cast', c)
        call SaveUnitHandle(udg_Table, id, 'targ', u)
        call SetUnitFacing( u, bj_RADTODEG*Atan2(yc-y,xc-x))
        call PauseUnit( u , true )
        call TimerStart( t, 0.10, true, function PeriodicCheck)
    endfunction

    function SEF_Check takes nothing returns boolean
        local unit c = GetTriggerUnit()
        local unit t = GetSpellTargetUnit()
        local integer utid = GetUnitTypeId(t)
        local player p = GetOwningPlayer(c)
        local integer i = GetPlayerId(p)
call BJDebugMsg("ran")
        if GetSpellAbilityId() == 'A02J' then
            if utid  == 'odoc' then
call ShowB(FB[i])
                if not FB[i] then
                    set FU[i] = t
                    set FB[i] = true
                    call SEF_Init(t, c)
                else
                    call DisplayTextToPlayer(p, 0, 0, "You already have a chosen caster for the Fire part of the spell.")
                endif
            elseif utid == 'oshm' then
                if not SB[i] then
                    set SU[i] = t
                    set SB[i] = true
                    call SEF_Init(t, c)
                else
                    call DisplayTextToPlayer(p, 0, 0, "You already have a chosen caster for the Storm part of the spell.")
                endif
            elseif utid == 'ospw' then
                if not EB[i] then
                    set EU[i] = t
                    set EB[i] = true
                    call SEF_Init(t, c)
                else
                    call DisplayTextToPlayer(p, 0, 0, "You already have a chosen caster for the Earth part of the spell.")
                endif
            endif
        endif
        set c = null
        set t = null
        set p = null
        return false
    endfunction

//function Trig_TEST_Actions takes nothing returns nothing
//    call UnitRemoveAbilityBJ( 'A02J', gg_unit_o000_0181 )
//    call UnitAddAbilityBJ( 'A02J', gg_unit_o000_0181 )
//endfunction

//==============================================================================
    function InitTrig_SEF takes nothing returns nothing
        local integer i = 0
        set gg_trg_SEF = CreateTrigger(  )
        call TriggerRegisterAnyUnitEventBJ( gg_trg_SEF, EVENT_PLAYER_UNIT_SPELL_EFFECT )
        call TriggerAddCondition( gg_trg_SEF, Condition( function SEF_Check ) )
        loop
            exitwhen i > 11
            set SB[i] = false
            set EB[i] = false
            set FB[i] = false
            set i = i + 1
        endloop
    endfunction
    
endscope

When I cast it - I get "ran", "false", "ran", "true".
Any idea why is this happening? x_x
The spell is based on Channel.

EDIT: I must be cursed.... Every time I post an issue with my triggers - I discover what's wrong by myself after few minutes.
Apparently it was getting initialized 2-ce, since I had "initializer InitTrig_SEF" in the top row.
This is the 1-st trigger in which I'm using a scope (and an initializer).

So now I wanna ask, what are initializers used for if people can just use "InitTrig_" in their scopes?
 
Choosing whether to use an initializer or InitTrig_ is mostly a matter of preference. I prefer initializers because they feel more explicit/readable, and I can name the function whatever I want (although I typically will just name it "Init").

There are also subtle differences in which initializer is ran first, but that only matters when you have libraries that require other libraries to initialize before you. So for now, you can just choose whichever method you like. :)
 
Status
Not open for further replies.
Top