[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
/**************************************************************
*
*   RegisterPlayerUnitEvent
*   v5.1.0.1
*   By Magtheridon96
*
*   I would like to give a special thanks to Bribe, azlier
*   and BBQ for improving this library. For modularity, it only
*   supports player unit events.
*
*   Functions passed to RegisterPlayerUnitEvent must either
*   return a boolean (false) or nothing. (Which is a Pro)
*
*   Warning:
*   --------
*
*       - Don't use TriggerSleepAction inside registered code.
*       - Don't destroy a trigger unless you really know what you're doing.
*
*   API:
*   ----
*
*       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
*           - Registers code that will execute when an event fires.
*       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
*           - Registers code that will execute when an event fires for a certain player.
*       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
*           - Returns the trigger corresponding to ALL functions of a playerunitevent.
*
**************************************************************/
library RegisterPlayerUnitEvent // Special Thanks to Bribe and azlier
    globals
        private trigger array t
    endglobals
   
    function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
        local integer i = GetHandleId(p)
        local integer k = 15
        if t[i] == null then
            set t[i] = CreateTrigger()
            loop
                call TriggerRegisterPlayerUnitEvent(t[i], Player(k), p, null)
                exitwhen k == 0
                set k = k - 1
            endloop
        endif
        call TriggerAddCondition(t[i], Filter(c))
    endfunction
   
    function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
        local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
        if t[i] == null then
            set t[i] = CreateTrigger()
            call TriggerRegisterPlayerUnitEvent(t[i], pl, p, null)
        endif
        call TriggerAddCondition(t[i], Filter(c))
    endfunction
   
    function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
        return t[GetHandleId(p)]
    endfunction
endlibrary


Here's a vanilla Jass version:

Code (jass) Select
//**************************************************************
//*
//*   RegisterPlayerUnitEvent (Vanilla Jass)
//*   v5.1.0.1
//*   By Magtheridon96
//*
//*   I would like to give a special thanks to Bribe, azlier
//*   and BBQ for improving this library. For modularity, it only
//*   supports player unit events.
//*
//*   Functions passed to RegisterPlayerUnitEvent must either
//*   return a boolean (false) or nothing. (Which is a Pro)
//*
//*   Implementation:
//*   ---------------
//*
//*       - Copy all this script into a new trigger called "RegisterPlayerUnitEvent Jass"
//*       - Create a trigger array variable called RPUE.
//*       - Done.
//*
//*   Warning:
//*   --------
//*
//*       - Don't use TriggerSleepAction inside registered code.
//*       - Don't destroy a trigger unless you really know what you're doing.
//*
//*   API:
//*   ----
//*
//*       - function RegisterPlayerUnitEvent takes playerunitevent whichEvent, code whichFunction returns nothing
//*           - Registers code that will execute when an event fires.
//*       - function RegisterPlayerUnitEventForPlayer takes playerunitevent whichEvent, code whichFunction, player whichPlayer returns nothing
//*           - Registers code that will execute when an event fires for a certain player.
//*       - function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
//*           - Returns the trigger corresponding to ALL functions of a playerunitevent.
//*
//**************************************************************
function RegisterPlayerUnitEvent takes playerunitevent p, code c returns nothing
    local integer i = GetHandleId(p)
    local integer k = 15
    if udg_RPUE[i] == null then
        set udg_RPUE[i] = CreateTrigger()
        loop
            call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], Player(k), p, null)
            exitwhen k == 0
            set k = k - 1
        endloop
    endif
    call TriggerAddCondition(udg_RPUE[i], Filter(c))
endfunction

function RegisterPlayerUnitEventForPlayer takes playerunitevent p, code c, player pl returns nothing
    local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
    if udg_RPUE[i] == null then
        set udg_RPUE[i] = CreateTrigger()
        call TriggerRegisterPlayerUnitEvent(udg_RPUE[i], pl, p, null)
    endif
    call TriggerAddCondition(udg_RPUE[i], Filter(c))
endfunction

function GetPlayerUnitEventTrigger takes playerunitevent p returns trigger
    return udg_RPUE[GetHandleId(p)]
endfunction
   
function InitTrig_RegisterPlayerUnitEvent_Jass takes nothing returns nothing
endfunction


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

everything is working properly

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

spell/skill does not work.

Does anyone know what the problem is?!?
14 years
Quote from: zerocool on July 14, 2012, 02:29:27 AM
When I use this:
Code (jass) Select
RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT,function bla,bla)

everything is working properly

But when I use this:
Code (jass) Select
RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function bla,bla)

spell/skill does not work.

Does anyone know what the problem is?!?
Could you provide us with the spell code?
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
function SkillHeatActions takes nothing returns boolean
    // bla,bla
    return true
endfunction

function StartTrigger_Skill_Heat takes nothing returns nothing
    local trigger=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
    call Preload(H_ModelPath())
    set t=null
endfunction


Everything works perfectly,and when I try this
Code (jass) Select
function SkillHeatActions takes nothing returns boolean
   // bla,bla
    return true
endfunction

function StartTrigger_Skill_Heat takes nothing returns nothing
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
    call Preload(H_ModelPath())
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
function SkillHeatActions takes nothing returns boolean
    // bla,bla
    return true
endfunction

function StartTrigger_Skill_Heat takes nothing returns nothing
    local trigger=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
    call Preload(H_ModelPath())
    set t=null
endfunction


Everything works perfectly,and when I try this
Code (jass) Select
function SkillHeatActions takes nothing returns boolean
   // bla,bla
    return true
endfunction

function StartTrigger_Skill_Heat takes nothing returns nothing
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
    call Preload(H_ModelPath())
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
function StartTrigger_Skill_Heat takes nothing returns nothing
    local trigger=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
    call TriggerAddCondition(t,Condition(function SkillHeatActions))
    call Preload(H_ModelPath())
    set t=null
endfunction


and this will not work

Code (jass) Select
function StartTrigger_Skill_Heat takes nothing returns nothing
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function SkillHeatActions)
    call Preload(H_ModelPath())
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

function InitTrig_Skill_Heat takes nothing returns nothing
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,function HeatMainAct,null)
endfunction


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

RegisterAnyUnitEvent Action:
Code (jass) Select

function InitTrig_Skill_Heat takes nothing returns nothing
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,null,function HeatAddDamageAct)
endfunction


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

RegisterAnyUnitEvent Condition/Action:
Code (jass) Select

function InitTrig_Skill_Heat takes nothing returns nothing
    call RegisterAnyUnitEvent(EVENT_PLAYER_HERO_SKILL,function RegisterSkillHeat,function HeatMainAct)
endfunction


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

TriggerRegisterAnyUnitEventBJ:
Code (jass) Select

function InitTrig_Skill_Heat takes nothing returns nothing
    local trigger=CreateTrigger()
    call TriggerRegisterAnyUnitEventBJ(t,EVENT_PLAYER_HERO_SKILL)
    call TriggerAddCondition(t,Condition(function HeatMainAct))
    set t=null
endfunction


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

RegisterPlayerUnitEvent:
Code (jass) Select

function InitTrig_Skill_Heat takes nothing returns nothing
    call RegisterPlayerUnitEvent(EVENT_PLAYER_HERO_SKILL,function HeatMainAct)
endfunction


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.
13 years
Oh yay, I don't have to do any fixes :D
13 years
Updated! :D
NOW MICROSECONDS FASTER.