[Snippet] RegisterAnyUnitEvent

Scripts

14 years
edited 8 years
Description.

A library to reduce the time writing the same shit. I took some code ideas from a similar resource approved here. This one supports all the possibilities in calling the trigger. If user defines the condition and action functions this function will return the respective trigger, so it can be controlled by other instance (just in case).

If the user only sets a condition or action, then it will return [lcode=jass]null[/lcode] because this trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.


Usage:
[btable]For functions without triggersleepaction or waits, it's recommended to use
Code (jass) Select
call RegisterAnyUnitEvent(<Event>, code Function, null)[r]For functions with triggersleepaction or waits, it's recommended to use
Code (jass) Select
call RegisterAnyUnitEvent(<Event>, null, code Function)[r]For triggers which condition and action codes must be separated (it could happen) you set this:
Code (jass) Select
private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction

private function Actions takes nothing returns nothing
    // your stuff...
endfunction

private function init takes nothing returns nothing
    local trigger t = RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
endfunction
[/btable]


Actual code

Code (jass) Select
/*
//************************************************************************//
//                    RegisterAnyUnitEvent by moyack                      //
//************************************************************************//
// Code improved with the idea developed by Magtheridon96 , Bribe, azlier //
//************************************************************************//

A library to reduce the time writing the same shit. I took some code ideas from a similar
resource approved here. This one supports all the possibilities in calling the trigger.
If user defines the condition and action functions this function will return the respective
trigger, so it can be controlled by other instance (just in case).

If the user only sets a condition or action, then it will return null because this
trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.

* For functions without triggersleepaction or waits, it's recommended to use:
    call RegisterAnyUnitEvent(<Event>, code Function, null)

* For functions with triggersleepaction or waits, it's recommended to use:
    call RegisterAnyUnitEvent(<Event>, null, code Function)
*/
globals
    private trigger array ts
endglobals

function RegisterAnyUnitEvent takes playerunitevent e, code cond, code act returns nothing
    local integer i = GetHandleId(e)
    local trigger t
    if act != null then
        set t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, e)
        if cond != null then
            call TriggerAddCondition(t, Filter(cond))
        endif
        call TriggerAddAction(t, act)
        set t = null
    else
        if ts[i] == null then
            set ts[i] = CreateTrigger()
            call TriggerRegisterAnyUnitEventBJ(ts[i], e)
        endif
        call TriggerAddCondition(ts[i], Filter(cond))
    endif
endfunction

endlibrary


Examples:
Examples
Condition only
Code (jass) Select
scope test initializer init

private function Conditions takes nothing returns boolean
    // your code... please DO NOT use waits or triggersleeaction...
    return false // please always set it to false
endfunction

private function init takes nothing returns nothing
    // that's the recommended way, it's faster and nice :)
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, null)
endfunction

endscope

Action Code Only
Code (jass) Select
scope test initializer init

private function Actions takes nothing returns nothing
    // your stuff...
endfunction

private function init takes nothing returns nothing
    // It's valid but not as faster as using conditions. Use it when you have in the code
    // Polledwait(), triggersleepaction() or wait()
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, null, function Actions)
endfunction

endscope

Condition and action separated
Code (jass) Select
scope test initializer init

private function Conditions takes nothing returns boolean
    return GetSpellAbilityId() == 'A000'
endfunction

private function Actions takes nothing returns nothing
    // your stuff...
endfunction

private function init takes nothing returns nothing
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
endfunction

endscope


Optionally I can add textmacros for other kind of events, but I left it in this way because this is the most used by me.
13 years
A small make up to the first post has been made :)