[Snippet] RegisterPlayerUnitEvent

Scripts

14 years
edited 8 years
I'm attempting to start up some activity over here by posting/spamming resources :P.

This system was made to replace that cumbersome GTrigger by Jesus4Lyf.
Special thanks to Bribe, azlier and BBQ. (The most awesome people I know /o/)

Code (jass) Select
1
/**************************************************************
2
*
3
*   RegisterPlayerUnitEvent
4
*   v5.1.0.1
5
*   By Magtheridon96
6
*
7
*   I would like to give a special thanks to Bribe, azlier
8
*   and BBQ for improving this library. For modularity, it only 
9
*   supports player unit events.
10
*
11
*   Functions passed to RegisterPlayerUnitEvent must either
12
*   return a boolean (false) or nothing. (Which is a Pro)
13
*
14
*   Warning:
15
*   --------
16
*
17
*       - Don't use TriggerSleepAction inside registered code.
18
*       - Don't destroy a trigger unless you really know what you're doing.
19
*
20
*   API:
21
*   ----
22
*
23
*       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
24
*           - Registers code that will execute when an event fires.
25
*       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
26
*           - Registers code that will execute when an event fires for a certain player.
27
*       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
28
*           - Returns the trigger corresponding to ALL functions of a playerunitevent.
29
*
30
**************************************************************/
31
library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
32
    globals
33
        private trigger array t
34
    endglobals
35
    
36
    function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
37
        local integer i = GetHandleId(p)
38
        local integer k = 15
39
        if t[i] == null then
40
            set t[i] = CreateTrigger()
41
            loop
42
                call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
43
                exitwhen k == 0
44
                set k = k - 1
45
            endloop
46
        endif
47
        call TriggerAddCondition(t[i], Filter(c))
48
    endfunction
49
    
50
    function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
51
        local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
52
        if t[i] == null then
53
            set t[i] = CreateTrigger()
54
            call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
55
        endif
56
        call TriggerAddCondition(t[i], Filter(c))
57
    endfunction
58
    
59
    function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
60
        return t[GetHandleId(p)]
61
    endfunction
62
endlibrary


Here's a vanilla Jass version:

Code (jass) Select
1
//**************************************************************
2
//*
3
//*   RegisterPlayerUnitEvent (Vanilla Jass)
4
//*   v5.1.0.1
5
//*   By Magtheridon96
6
//*
7
//*   I would like to give a special thanks to Bribe, azlier
8
//*   and BBQ for improving this library. For modularity, it only 
9
//*   supports player unit events.
10
//*
11
//*   Functions passed to RegisterPlayerUnitEvent must either
12
//*   return a boolean (false) or nothing. (Which is a Pro)
13
//*
14
//*   Implementation:
15
//*   ---------------
16
//*
17
//*       - Copy all this script into a new trigger called "RegisterPlayerUnitEvent Jass"
18
//*       - Create a trigger array variable called RPUE.
19
//*       - Done.
20
//*
21
//*   Warning:
22
//*   --------
23
//*
24
//*       - Don't use TriggerSleepAction inside registered code.
25
//*       - Don't destroy a trigger unless you really know what you're doing.
26
//*
27
//*   API:
28
//*   ----
29
//*
30
//*       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
31
//*           - Registers code that will execute when an event fires.
32
//*       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
33
//*           - Registers code that will execute when an event fires for a certain player.
34
//*       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
35
//*           - Returns the trigger corresponding to ALL functions of a playerunitevent.
36
//*
37
//**************************************************************
38
function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
39
    local integer i = GetHandleId(p)
40
    local integer k = 15
41
    if udg_RPUE[i] == null then
42
        set udg_RPUE[i] = CreateTrigger()
43
        loop
44
            call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], Player(k), p, null)
45
            exitwhen k == 0
46
            set k = k - 1
47
        endloop
48
    endif
49
    call TriggerAddCondition(udg_RPUE[i], Filter(c))
50
endfunction
51
52
function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
53
    local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
54
    if udg_RPUE[i] == null then
55
        set udg_RPUE[i] = CreateTrigger()
56
        call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], pl, p, null)
57
    endif
58
    call TriggerAddCondition(udg_RPUE[i], Filter(c))
59
endfunction
60
61
function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
62
    return udg_RPUE[GetHandleId(p)]
63
endfunction
64
    
65
function InitTrig_RegisterPlayerUnitEvent_Jass takes nothing returns nothing
66
endfunction


Feel free to comment.
14 years
When I use this:
1
RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT,function bla,bla)


everything is working properly

But when I use this:
1
RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function bla,bla)


spell/skill does not work.

Does anyone know what the problem is?!?
14 years
edited 14 years
Quote from: moyack on July 14, 2012, 06:08:34 AM
Could you provide us with the spell code?

Okay, no problem.

When I try to use this:
Code (jass) Select
1
function SkillHeatActions takes nothing returns boolean
2
    // bla,bla
3
    return true
4
endfunction
5
6
function StartTrigger_Skill_Heat takes nothing returns nothing
7
    local trigger=CreateTrigger()
8
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
9
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
10
    call Preload(H_ModelPath())
11
    set t=null
12
endfunction


Everything works perfectly,and when I try this
Code (jass) Select
1
function SkillHeatActions takes nothing returns boolean
2
   // bla,bla
3
    return true
4
endfunction
5
6
function StartTrigger_Skill_Heat takes nothing returns nothing
7
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
8
    call Preload(H_ModelPath())
9
endfunction


simply will not work...???




14 years
Quote from: zerocool on July 14, 2012, 09:20:59 AM
Okay, no problem.

When I try to use this:
Code (jass) Select
1
function SkillHeatActions takes nothing returns boolean
2
    // bla,bla
3
    return true
4
endfunction
5
6
function StartTrigger_Skill_Heat takes nothing returns nothing
7
    local trigger=CreateTrigger()
8
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
9
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
10
    call Preload(H_ModelPath())
11
    set t=null
12
endfunction


Everything works perfectly,and when I try this
Code (jass) Select
1
function SkillHeatActions takes nothing returns boolean
2
   // bla,bla
3
    return true
4
endfunction
5
6
function StartTrigger_Skill_Heat takes nothing returns nothing
7
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
8
    call Preload(H_ModelPath())
9
endfunction


simply will not work...???

IS this function inside a scope? if so, try changing it into a library with the respective requirements
14 years
Quote from: moyack on July 14, 2012, 10:25:57 AM
IS this function inside a scope? if so, try changing it into a library with the respective requirements

I think the problem is not what you said
For Example:When I use the event EVENT_PLAYER_UNIT_SPELL_EFFECT everything is working well,but when I want to use the event EVENT_PLAYER_HERO_SKILL then there is a problem,spell/skill will not work.

Which means that only that specific event causes error.

Because it is logical that, if this works

Code (jass) Select
1
function StartTrigger_Skill_Heat takes nothing returns nothing
2
    local trigger=CreateTrigger()
3
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
4
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
5
    call Preload(H_ModelPath())
6
    set t=null
7
endfunction


and this will not work

Code (jass) Select
1
function StartTrigger_Skill_Heat takes nothing returns nothing
2
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
3
    call Preload(H_ModelPath())
4
endfunction


that there is a conflict with the system
I think this event[EVENT_PLAYER_HERO_SKILL] has a conflict with the system
14 years
Hmmm.... have you debugged it??? I mean if this even start at all?? I hope you're not using TriggerSleepAction() or PolledWait() in the condition because it breaks the thread.
14 years
Quote from: moyack on July 14, 2012, 04:39:42 PM
Hmmm.... have you debugged it??? I mean if this even start at all?? I hope you're not using TriggerSleepAction() or PolledWait() in the condition because it breaks the thread.

I never use TriggerSleepAction and PolledWait func
14 years
I have tested your system for some time, and the results are as follows:

RegisterAnyUnitEvent Condition:
Code (jass) Select
1
2
function InitTrig_Skill_Heat takes nothing returns nothing
3
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,function HeatMainAct,null)
4
endfunction
5


1.Impression:Works perfectly.
2.Opinion:Very stable.
3.Errors:No errors found.

RegisterAnyUnitEvent Action:
Code (jass) Select
1
2
function InitTrig_Skill_Heat takes nothing returns nothing
3
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,null,function HeatAddDamageAct)
4
endfunction
5


1.Impression:Works perfectly.
2.Opinion:Very stable.
3.Errors:No errors found.

RegisterAnyUnitEvent Condition/Action:
Code (jass) Select
1
2
function InitTrig_Skill_Heat takes nothing returns nothing
3
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,function RegisterSkillHeat,function HeatMainAct)
4
endfunction
5


1.Impression:Works perfectly.
2.Opinion:Very stable.
3.Errors:No errors found.

TriggerRegisterAnyUnitEventBJ:
Code (jass) Select
1
2
function InitTrig_Skill_Heat takes nothing returns nothing
3
    local trigger=CreateTrigger()
4
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
5
    call TriggerAddCondition(t,Condition(function HeatMainAct))
6
    set t=null
7
endfunction
8


1.Impression:Works perfectly.
2.Opinion:Stable.
3.Errors:No errors found.

RegisterPlayerUnitEvent:
Code (jass) Select
1
2
function InitTrig_Skill_Heat takes nothing returns nothing
3
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function HeatMainAct)
4
endfunction
5


1.Impression:Not working properly.
2.Opinion:Very unstable,especially with "EVENT_PLAYER_HERO_SKILL".
3.Errors:Can cause errors.
4.Note: I still have not found the reason why this event["EVENT_PLAYER_HERO_SKILL"] in some cases,cause problems.
14 years
I need to get some time to test this event, but, meanwhile try to use the functions inside a library. I had that kind of issue in my project Power of Corruption and the problem was that I was using scopes, changing them into libraries solved the issues.
14 years
I figured what's the problem yeahhhhhhhhh!!!
Problem solved.
14 years
Oh yay, I don't have to do any fixes :D
13 years
Updated! :D
NOW MICROSECONDS FASTER.