[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 null 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
1
call RegisterAnyUnitEvent(<Event>, code Function, null)
[r]For functions with triggersleepaction or waits, it's recommended to use
Code (jass) Select
1
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
1
private function Conditions takes nothing returns boolean
2
    return GetSpellAbilityId() == 'A000'
3
endfunction
4
5
private function Actions takes nothing returns nothing
6
    // your stuff...
7
endfunction
8
9
private function init takes nothing returns nothing
10
    local trigger t = RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
11
endfunction
[/btable]


Actual code

Code (jass) Select
1
/*
2
//************************************************************************//
3
//                    RegisterAnyUnitEvent by moyack                      //
4
//************************************************************************//
5
// Code improved with the idea developed by Magtheridon96 , Bribe, azlier //
6
//************************************************************************//
7
8
A library to reduce the time writing the same shit. I took some code ideas from a similar 
9
resource approved here. This one supports all the possibilities in calling the trigger.
10
If user defines the condition and action functions this function will return the respective 
11
trigger, so it can be controlled by other instance (just in case).
12
13
If the user only sets a condition or action, then it will return null because this
14
trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.
15
16
 * For functions without triggersleepaction or waits, it's recommended to use:
17
    call RegisterAnyUnitEvent(<Event>, code Function, null)
18
19
 * For functions with triggersleepaction or waits, it's recommended to use:
20
    call RegisterAnyUnitEvent(<Event>, null, code Function)
21
*/
22
globals
23
    private trigger array ts
24
endglobals
25
26
function RegisterAnyUnitEvent takes playerunitevent e, code cond, code act returns nothing
27
    local integer i = GetHandleId(e)
28
    local trigger t
29
    if act != null then
30
        set t = CreateTrigger()
31
        call TriggerRegisterAnyUnitEventBJ(t, e)
32
        if cond != null then
33
            call TriggerAddCondition(t, Filter(cond))
34
        endif
35
        call TriggerAddAction(t, act)
36
        set t = null
37
    else
38
        if ts[i] == null then
39
            set ts[i] = CreateTrigger()
40
            call TriggerRegisterAnyUnitEventBJ(ts[i], e)
41
        endif
42
        call TriggerAddCondition(ts[i], Filter(cond))
43
    endif
44
endfunction
45
46
endlibrary


Examples:
Examples
Condition only
Code (jass) Select
1
scope test initializer init
2
3
private function Conditions takes nothing returns boolean
4
    // your code... please DO NOT use waits or triggersleeaction...
5
    return false // please always set it to false
6
endfunction
7
8
private function init takes nothing returns nothing
9
    // that's the recommended way, it's faster and nice :)
10
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, null)
11
endfunction
12
13
endscope

Action Code Only
Code (jass) Select
1
scope test initializer init
2
3
private function Actions takes nothing returns nothing
4
    // your stuff...
5
endfunction
6
7
private function init takes nothing returns nothing
8
    // It's valid but not as faster as using conditions. Use it when you have in the code
9
    // Polledwait(), triggersleepaction() or wait()
10
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, null, function Actions)
11
endfunction
12
13
endscope

Condition and action separated
Code (jass) Select
1
scope test initializer init
2
3
private function Conditions takes nothing returns boolean
4
    return GetSpellAbilityId() == 'A000'
5
endfunction
6
7
private function Actions takes nothing returns nothing
8
    // your stuff...
9
endfunction
10
11
private function init takes nothing returns nothing
12
    call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
13
endfunction
14
15
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 :)