TriggerRegister..., TriggerAddAction, etc.

eliw00d

New Member
Reaction score
3
Say for instance I have a trigger that starts out like this:

JASS:
    private function Actions takes nothing returns nothing
        local trigger t = CreateTrigger()
        local data d = data.create()
        set d.caster = GetTriggerUnit()
        set d.target = GetSpellTargetItem()
        call SetCSData(d.caster,d)
        call TriggerAddAction(t,function Script)
        call TriggerRegisterUnitEvent(t,GetTriggerUnit(),EVENT_UNIT_USE_ITEM)
        set t = null
    endfunction
    
    private function InitReload takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t,Condition(function GetAbility))
        call TriggerAddAction(t,function Actions)            
    endfunction


A few weeks ago I was looking for a way to detect a manipulated item at the same time as a spell was cast. I took what I found as a base and have built around it, but I'm not sure if it is the most effective way to do it (see:full trigger). I also don't fully understand the usage of the Trigger calls.

1) Does it matter what order they are in? i.e. Event, then Condition, then Action vs. Action then Event (as seen above).
2) If I want to detect a manipulated item, does it detect only for the function it is in? Or will it detect other manipulated items, such as from completely separate triggers/scopes?

I'm still learning vJASS and am trying to use Structs as well as I can understand them.
 

PurgeandFire

zxcvmkgdfg
Reaction score
508
2.) What do you mean? It will register the current manipulated item in correspondence to the event. Besides, the other triggers will probably filter out other items (conditional-wise) so not much to worry about...

Yes, the event responses respond to the current event of the trigger.

Number 1, I'll answer after testing it. :D
 

eliw00d

New Member
Reaction score
3
2.) What do you mean? It will register the current manipulated item in correspondence to the event. Besides, the other triggers will probably filter out other items (conditional-wise) so not much to worry about...

Yes, the event responses respond to the current event of the trigger.

Number 1, I'll answer after testing it. :D

so should i add a TriggerAddCondition for the Use Item event? Like so:

JASS:
        call TriggerAddAction(t,function Script)
        call TriggerAddCondition(t,function Script)
        call TriggerRegisterUnitEvent(t,GetTriggerUnit(),EVENT_UNIT_USE_ITEM)


and then add a condition checking the manipulated item against preset options? reason i'm asking is because i swear this trigger is getting weird results when you click on other items while it is executing.
 

PurgeandFire

zxcvmkgdfg
Reaction score
508
How do you use "GetTriggerUnit()" if there is no unit to start with?

Try putting them in order and use TriggerRegisterAnyUnitEventBJ.

If you want to get rid of the BJ, make a dummy function called true like so:
JASS:
function True takes nothing returns boolean
    return true
endfunction


Then use this:


Something like that, it might not be exact though since it is freehand.

As long as your condition isn't screwed up (and as long as it filters the right stuff) it will be fine.

And your example code is weird btw, you use function Script in both and it takes filterfunc. :p (I know it is just an example, I'm just joking)
 

eliw00d

New Member
Reaction score
3
GetTriggerUnit() gets the casting unit from the other event. the use item event is nested, i guess you could say.
 

PurgeandFire

zxcvmkgdfg
Reaction score
508
Ok, then ignore my last post. What weird stuff happen after the execution?

The conditions probably don't filter out correctly, post the whole code here. :thup:
 

eliw00d

New Member
Reaction score
3
Ok, then ignore my last post. What weird stuff happen after the execution?

The conditions probably don't filter out correctly, post the whole code here. :thup:

there's a link to it in the first post. it's a very different way of approaching it, i'm sure, but i am new to vJASS. i had to use a lot of DestroyTrigger() calls to eliminate a lot of the behavior. the main problem i'm having is that if i disable the Script function, it disables other triggers outside the scope. i'm trying to find out why.

edit: actually, here:

JASS:
scope FirearmReload   
    private struct data
        unit caster
        item target
        item source
        integer a 
        integer b 
        integer c
        method onDestroy takes nothing returns nothing
            set .caster = null
            set .target = null
            set .source = null
            set .a = 0
            set .b = 0
            set .c = 0
        endmethod
    endstruct 
    
    private function Conditions takes nothing returns boolean
        return GetSpellAbilityId() == 'A003'
    endfunction
    
    private function Script takes nothing returns nothing
        local data d = GetCSData(GetTriggerUnit()) 
        local integer max = 0
        local integer dif = 0
        
        set d.source = GetManipulatedItem()
        set d.a = GetItemCharges(d.target)
        set d.b = GetItemCharges(d.source)
        set d.c = GetItemUserData(d.target)
        set dif = d.a - 1
        
        if GetAttachedBoolean(d.caster,"active") then
            if GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' and d.b != 0 then
                call SetItemCharges(d.source,d.b+1)
            elseif GetItemTypeId(d.source) == 'I001' or GetItemTypeId(d.source) == 'I003' then    
                call UnitAddItemByIdSwapped(GetItemTypeId(d.source),d.caster)
            endif
            call d.destroy()
            call DestroyTrigger(GetTriggeringTrigger()) 
            return
        elseif not GetAttachedBoolean(d.caster,"active") then
            if GetItemTypeId(d.target) == 'I002' then
                if GetItemTypeId(d.source) == 'I003' then
                    set max = 30
                elseif GetItemTypeId(d.source) == 'I001' then
                    set max = 100
                elseif GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' and d.b != 0 then
                    set max = d.b + 1
                endif
                if d.a < (max + 1) and d.a > 0 then
                    call AttachBoolean(d.caster,"active",true)
                    call DisableTrigger(GetTriggeringTrigger())
                    call SetItemCharges(d.target,1)
                    call PlaySoundOnUnitBJ(gg_snd_HK416MAGOUT,100,d.caster)
                    if d.c == 1 then
                        call UnitAddItemByIdSwapped('I006',d.caster)
                        call SetItemCharges(GetLastCreatedItem(),dif)
                    elseif d.c == 0 then
                        if dif == 30 then
                            call UnitAddItemByIdSwapped('I003',d.caster)
                        else
                            call UnitAddItemByIdSwapped('I00E',d.caster)
                            call SetItemCharges(GetLastCreatedItem(),dif)
                        endif 
                    endif
                    if GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' then
                        call RemoveItem(d.source)
                    endif   
                    call TriggerSleepAction(2.80)
                    call SetItemCharges(d.target,1+max)
                    set max = GetItemCharges(d.target)
                    if max == 101 or max == 100 then
                        call SetItemUserData(d.target,1)
                    elseif max == 31 or max == 30 then
                        call SetItemUserData(d.target,0)
                    endif
                    call PlaySoundOnUnitBJ(gg_snd_HK416MAGIN,100,d.caster)
                    call TriggerSleepAction(1.00)
                    call EnableTrigger(GetTriggeringTrigger())
                    call AttachBoolean(d.caster,"active",false)
                    call d.destroy()
                    call DestroyTrigger(GetTriggeringTrigger())
                    return
                elseif d.a == 0 then
                    call AttachBoolean(d.caster,"active",true)
                    call SetItemCharges(d.target,0)
                    call PlaySoundOnUnitBJ(gg_snd_HK416MAGOUT,100,d.caster)
                    if d.c == 1 then
                        call UnitAddItemByIdSwapped('I006',d.caster)
                        call SetItemCharges(GetLastCreatedItem(),dif)
                    elseif d.c == 0 then
                        if dif == 30 then
                            call UnitAddItemByIdSwapped('I003',d.caster)
                        else
                            call UnitAddItemByIdSwapped('I00E',d.caster)
                            call SetItemCharges(GetLastCreatedItem(),dif)
                        endif 
                    endif    
                    if GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' then
                        call RemoveItem(d.source)
                    endif
                    call TriggerSleepAction(2.80)
                    call SetItemCharges(d.target,0+max)
                    set max = GetItemCharges(d.target)
                    if max == 101 or max == 100 then
                        call SetItemUserData(d.target,1)
                    elseif max == 31 or max == 30 then
                        call SetItemUserData(d.target,0)
                    endif
                    call PlaySoundOnUnitBJ(gg_snd_HK416MAGIN,100,d.caster)
                    call TriggerSleepAction(0.20)
                    call PlaySoundOnUnitBJ(gg_snd_HK416CHARGE,100,d.caster)
                    call TriggerSleepAction(1.00)
                    call AttachBoolean(d.caster,"active",false)
                    call d.destroy()
                    call DestroyTrigger(GetTriggeringTrigger())
                    return
                else
                    if GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' and d.b != 0 then
                        call SetItemCharges(d.source,d.b+1)
                    elseif GetItemTypeId(d.source) == 'I001' or GetItemTypeId(d.source) == 'I003' then    
                        call UnitAddItemByIdSwapped(GetItemTypeId(d.source),d.caster)
                    endif
                    call d.destroy()
                    call DestroyTrigger(GetTriggeringTrigger()) 
                    return
                endif
            elseif GetItemTypeId(d.target) == 'I007' then               
                if d.a < 5 and d.b > 0 then
                    call AttachBoolean(d.caster,"active",true)        
                    loop
                        set d.a = d.a + 1
                        set d.b = d.b - 1
                        call SetItemCharges(d.target,d.a)
                        call SetItemCharges(d.source,d.b)
                        call PlaySoundOnUnitBJ(gg_snd_HK416MAGIN,100,d.caster)
                        exitwhen d.a == 5 or d.b == 0
                        call TriggerSleepAction(1.00)
                    endloop
                    if d.b == 0 then
                        call RemoveItem(d.source)
                    endif
                    call TriggerSleepAction(1.00)
                    call AttachBoolean(d.caster,"active",false)
                    call d.destroy()
                    call DestroyTrigger(GetTriggeringTrigger())
                    return
                elseif d.a == 5 and d.b > 0 then
                    call SetItemCharges(d.source,d.b)
                    call d.destroy()
                    call DestroyTrigger(GetTriggeringTrigger())
                    return
                endif
            else
                if GetItemTypeId(d.source) == 'I00E' or GetItemTypeId(d.source) == 'I006' or GetItemTypeId(d.source) == 'I008' and d.b != 0 then
                    call SetItemCharges(d.source,d.b+1)
                elseif GetItemTypeId(d.source) == 'I001' or GetItemTypeId(d.source) == 'I003' then    
                    call UnitAddItemByIdSwapped(GetItemTypeId(d.source),d.caster)
                endif
                call d.destroy()
                call DestroyTrigger(GetTriggeringTrigger()) 
                return
            endif 
        endif
    endfunction
    
    private function Actions takes nothing returns nothing
        local trigger t = CreateTrigger()
        local data d = data.create()
        set d.caster = GetTriggerUnit()
        set d.target = GetSpellTargetItem()
        call SetCSData(d.caster,d)
        call TriggerAddAction(t,function Script)
        call TriggerRegisterUnitEvent(t,GetTriggerUnit(),EVENT_UNIT_USE_ITEM)
        set t = null
    endfunction
    
    public function InitTrig takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t,Condition(function Conditions))
        call TriggerAddAction(t,function Actions)            
    endfunction      
endscope
and here is the other script that is stopping:

JASS:
scope FirearmEquip 
    private struct data
        unit caster
        item source
        integer a 
        method onDestroy takes nothing returns nothing
            set .caster = null
            set .source = null
            set .a = 0
        endmethod
    endstruct
         
    private function Conditions takes nothing returns boolean
        return GetSpellAbilityId() == 'A001'
    endfunction

    private function Script takes nothing returns nothing
        local data d = GetCSData(GetTriggerUnit())
        local integer a = 0        
                   
        set d.source = GetManipulatedItem()
        set a = GetItemCharges(d.source)
    
        if GetUnitAbilityLevel(d.caster,'A00V') == 1 then
            call SetUnitAbilityLevel(d.caster,'A00V',2)
            if a != 0 then
                call SetItemCharges(d.source,a+1)
            endif
            call d.destroy()
            call DestroyTrigger(GetTriggeringTrigger())
            return
        else
            call SetUnitAbilityLevel(d.caster,'A00V',1)
            if a != 0 then
                call SetItemCharges(d.source,a+1)
            endif
            call d.destroy()
            call DestroyTrigger(GetTriggeringTrigger())
            return
        endif
    endfunction

    private function Actions takes nothing returns nothing
        local trigger t = CreateTrigger()
        local data d = data.create()
        set d.caster = GetTriggerUnit()
        call SetCSData(d.caster,d)
        call TriggerAddAction(t,function Script)
        call TriggerRegisterUnitEvent(t,GetTriggerUnit(),EVENT_UNIT_USE_ITEM)
        set t = null
    endfunction

    public function InitTrig takes nothing returns nothing
        local trigger t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition(t, Condition( function Conditions ) )
        call TriggerAddAction(t, function Actions )
    endfunction
endscope
is it because they both have the same nested event but no condition? also, any feedback on the code in any way would be appreciated. i'm a novice programmer as well as a newbie to vJASS. i need all the help i can get, and criticism.
 

Switch33

New Member
Reaction score
12
JASS:
 (In the second script)
private function Script takes nothing "returns nothing"
        local data d = GetCSData(GetTriggerUnit())
        local integer a = 0        
                   
        set d.source = GetManipulatedItem()
        set a = GetItemCharges(d.source)
    
        if GetUnitAbilityLevel(d.caster,'A00V') == 1 then
            call SetUnitAbilityLevel(d.caster,'A00V',2)
            if a != 0 then
                call SetItemCharges(d.source,a+1)
            endif
            call d.destroy()
            call DestroyTrigger(GetTriggeringTrigger())
            "return" <------------------------------
        else
            call SetUnitAbilityLevel(d.caster,'A00V',1)
            if a != 0 then
                call SetItemCharges(d.source,a+1)
            endif
            call d.destroy()
            call DestroyTrigger(GetTriggeringTrigger())
            "return" <----------------------------
        endif
    endfunction


That could be a problem..?

Also in the first if you do something like call DestroyTrigger(GetTriggeringTrigger()) it will destroy the last trigger that ran that function. You might want to set the other trigger to a struct to be able to use it later and destroy it, but then again.. why would you want to make a trigger that creates another trigger and destroys itself besides just dmg detection systems?

If your trying to avoid using RegisterAnyUnitEventBJ() it's rather pointless.. theres no reason to avoid it. It's not horribly bad and is used by nearly everyone. (It also allows for easier MUI in most cases.)
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top