Bugged event trigger

Darius34

New Member
Reaction score
30
Code:
function Trig_Spirit_Call_Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A003'
endfunction

function SpiritCallEnd takes nothing returns nothing
    local trigger t = GetTriggeringTrigger()
    local unit caster = GetHandleUnit(t,"caster")
    local unit summoned = GetHandleUnit(t,"summoned")
    local integer i = 1
    local integer j = 6
    
    call DisplayTextToForce(GetPlayersAll(),"Summoned has died.")
    
    call PauseUnitBJ(false,caster)
    call SelectUnitForPlayerSingle( caster, GetOwningPlayer(caster) )
    call SetHeroXP( caster, GetHeroXP(summoned), false )
    loop
        exitwhen i > j
        call UnitAddItemByIdSwapped( GetItemTypeId(UnitItemInSlotBJ(summoned, i)), caster )
        set i = i + 1
    endloop
    call DisplayTextToForce(GetPlayersAll(),"Items transferred back.")
    set caster = null
    set summoned = null
endfunction

function Trig_Spirit_Call_Actions takes nothing returns nothing
    local unit caster = GetTriggerUnit()
    local location targetpt = GetSpellTargetLoc()
    local integer level = GetUnitAbilityLevel(caster,'A003')
    local unit summoned
    local trigger t
    local integer i = 1
    local integer j = 6
    local real facing = GetUnitFacing(caster)

    call PauseUnitBJ(true,caster)
    if level==1 then
        call CreateNUnitsAtLoc(1,'H002',GetOwningPlayer(caster),targetpt,facing)
    elseif level==2 then
        call CreateNUnitsAtLoc(1,'H003',GetOwningPlayer(caster),targetpt,facing)
    elseif level==3 then
        call CreateNUnitsAtLoc(1,'H001',GetOwningPlayer(caster),targetpt,facing)
    endif
    set summoned = GetLastCreatedUnit()
    call DisplayTextToForce(GetPlayersAll(),"Summoned has been created.")
    call SelectUnitForPlayerSingle( summoned, GetOwningPlayer(caster) )
    call UnitApplyTimedLifeBJ( 18, 'BTLF', summoned )
    call SetHeroXP( summoned, GetHeroXP(caster), false )
    loop
        exitwhen i > j
        call UnitAddItemByIdSwapped( GetItemTypeId(UnitItemInSlotBJ(caster, i)), summoned )
        set i = i + 1
    endloop
    call DisplayTextToForce(GetPlayersAll(),"Items transferred.")
    
    call TriggerRegisterUnitEvent(t,summoned,EVENT_UNIT_DEATH)
    call TriggerAddAction(t,function SpiritCallEnd)
    call SetHandleHandle(t, "caster",caster)
    call SetHandleHandle(t, "summoned", summoned)
    call PolledWait(18.)
    call DisplayTextToForce(GetPlayersAll(),"Summoned has died (supposedly).")
    
    call ShowUnit(summoned,false)
    call SpiritCallEnd()
    call RemoveUnit(summoned)
    
    call FlushHandleLocals(t)
    call DestroyTrigger(t)
    set caster = null
    call RemoveLocation(targetpt)
    set summoned = null
    call DisplayTextToForce(GetPlayersAll(),"Done.")    
endfunction

//===========================================================================
function InitTrig_Spirit_Call takes nothing returns nothing
    set gg_trg_Spirit_Call = CreateTrigger(  )
    call TriggerRegisterAnyUnitEventBJ( gg_trg_Spirit_Call, EVENT_PLAYER_UNIT_SPELL_EFFECT )
    call TriggerAddCondition( gg_trg_Spirit_Call, Condition( function Trig_Spirit_Call_Conditions ) )
    call TriggerAddAction( gg_trg_Spirit_Call, function Trig_Spirit_Call_Actions )
endfunction
This is my trigger. A Unit Dies event does not fire at all. Basically it creates another hero unit at the target point, pauses the caster and after 18 seconds, kills the unit (hence the expiration timer) and returns control to the caster. If the summoned unit dies before the 18 second timer, control will be transferred back anyway.

Now, I have no idea why the event of the trigger t does not fire. I am using Handle Vars, am I doing something wrong there? Also, my code might not be very efficient or 100% leak free, so please correct me if you notice any such errors. Thanks.
 
D

dadads

Guest
Check these:
- Is the "Trigger Enabled" checkbox checked already?
- Have you initialized the game cache somewhere inside your map?
- Is the spell really 'A003' ?
 

Darius34

New Member
Reaction score
30
Have you initialized the game cache somewhere inside your map?
No, I haven't. But I don't remember doing that the last time I used Handle Vars, and it worked. I'll check again.

As for the rest, yes, I've checked those. The spell works all the way to the point where it creates the summoned unit. Then, when the summoned unit expires and dies, nothing more happens, i.e. the dying event does not fire.

I even tried killing the unit manually with a trigger. Didn't work.
 

SFilip

Gone but not forgotten
Reaction score
634
> But I don't remember doing that the last time I used Handle Vars, and it worked.
Game Cache has to be initialized, it can't work without it.
Did you change the LocalVars function so that it returns a global variable? Have you initialized that local variable?
 
L

leetslay

Guest
You need to create the trigger before you can use it.
as it is now your trigger var doesn't point to anything.
your code says "local trigger t"
you have to put "set t = CreateTrigger()"
somewhere after that and before you use it.
 

Darius34

New Member
Reaction score
30
@leetslay
Thanks a lot! The spell works fine now. I completely forgot about that part. :)

I'm sorry SFlip, but I don't understand what you're saying at all. :D I'm very new to the concept Handle Vars uses, and I have no idea how it worked without the game cache. Still, it did work, and it worked the previous time too. Could you explain, please? I'd like to take the chance to learn more.
___________

EDIT: Oh yes, and this may be more of a semantic question, but I thought I'd ask anyway.

When the spell begins the caster is paused and a summoned hero is created. It is set to the level of the caster hero and gains all the caster hero's items. Thus when the spell ends, I transfer the items and experience back from the summoned hero to the caster hero, in the event of the summoned one gaining additionals.

This is where my problem lies: the original caster hero ends up with two of every item it started with. Is it possible for me to check the items of both efficiently and remove all the duplicates, or to prevent the summoned hero from acquiring items? (So that I wouldn't have to transfer at all.) Thanks again, in advance.
 

SFilip

Gone but not forgotten
Reaction score
634
Well in the official handle variables system (found here) you have this little function...
Code:
function LocalVars takes nothing returns gamecache
    // Replace InitGameCache("jasslocalvars.w3v") with a global variable!!
    return InitGameCache("jasslocalvars.w3v")
endfunction
Its used by almost all the functions to access the game cache and it works sort of fine the way it is...
Yet it initializes the gamecache again on every use which is both slow and causes a leak.
Because of this you need to create a global variable of type gamecache (called cache for example) and replace that function with this.
Code:
function LocalVars takes nothing returns gamecache
    return udg_cache
endfunction
And call this on map initialization to initialize the actual gamecache:
set udg_cache = InitGameCache("jasslocalvars.w3v")
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      • Ghan
        Administrator - Servers are fun

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top