1. Updated Resource Submission Rules: All model & skin resource submissions must now include an in-game screenshot. This is to help speed up the moderation process and to show how the model and/or texture looks like from the in-game camera.
    Dismiss Notice
  2. DID YOU KNOW - That you can unlock new rank icons by posting on the forums or winning contests? Click here to customize your rank or read our User Rank Policy to see a list of ranks that you can unlock. Have you won a contest and still havn't received your rank award? Then please contact the administration.
    Dismiss Notice
  3. Travel to distant realms and encounter scenes unknown to the common folk. The Greatest of Adventures is upon us with the 8th Cinematic Contest. Join in on a fun ride.
    Dismiss Notice
  4. The 18th Icon Contest is ON! Choose any ingame unit and give him/her Hero abilities. Good luck to all.
    Dismiss Notice
  5. The Secrets of Warcraft 3 have revealed interesting works. The RESULTS for Abelhawk's Mini-Mapping Contest #15 have come out!
    Dismiss Notice
  6. Contestants are to create a scene set in the Stone Age. Come and see what you can come up with. We wish you the best of luck!
    Dismiss Notice
  7. Colour outside the lines! Techtree Contest #13 is a go. The contest is optionally paired.
    Dismiss Notice
  8. Night Rider gained several songs for his journey. The poll for the 12th Music Contest has started. Check it out!
    Dismiss Notice
  9. Greetings cerebrates, our Swarm needs new spawners that will have numerous children. Join the HIVE's 31st Modeling Contest - Spawners and Spawned! The contest is optionally paired.
    Dismiss Notice
  10. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

[Snippet] RegisterEvent pack

Discussion in 'JASS Resources' started by Bannar, Apr 4, 2014.

  1. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    A non-trigger registering for native event types.
    Instead of creating multiple small threads I've decided to upload this as a pack.

    Back in time when Maggy released RPUE I've written few additional, small snippets to provide similar functionality for other, main native event types. Main event types include: playerunitevent, playerevent, unitevent and gameevent.

    Still, after all those years RPUE could use an update, mainly because of awkward unsychronized trigger handles for "any" and "target (player)" cases. Whatmore, you can not retrieve trigger handle which was created for specific player and it creates unnesseccary amount of triggers instead of sticking to rule: one trigger per event per player/index.

    Thats why, before implementing other "RegisterEvents" I've created small lib to solve mentioned issues. Notice that there is only one unique trigger array instead of one per event type.

    RegisterNativeEvent:
    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterNativeEvent v1.1.1.3
    *       by Bannar
    *
    *    Storage of trigger handles for native events.
    *
    ******************************************************************************
    *
    *    Optional requirements:
    *
    *       Table by Bribe
    *          hiveworkshop.com/threads/snippet-new-table.188084/
    *
    ******************************************************************************
    *
    *    Important:
    *
    *       Avoid using TriggerSleepAction within functions registered.
    *       Destroy native event trigger on your own responsibility.
    *
    ******************************************************************************
    *
    *    Core:
    *
    *       function IsNativeEventRegistered takes integer whichIndex, integer whichEvent returns boolean
    *          Whether index whichIndex has already been attached to event whichEvent.
    *
    *       function RegisterNativeEventTrigger takes integer whichIndex, integer eventId returns boolean
    *          Registers whichIndex within whichEvent scope and assigns new trigger handle for it.
    *
    *       function GetIndexNativeEventTrigger takes integer whichIndex, integer whichEvent returns trigger
    *          Retrieves trigger handle for event whichEvent specific to provided index whichIndex.
    *
    *       function GetNativeEventTrigger takes integer whichEvent returns trigger
    *          Retrieves trigger handle for event whichEvent.
    *
    *
    *    Custom events:
    *
    *       function CreateNativeEvent takes nothing returns integer
    *          Returns unique id for new event and registers it with RegisterNativeEvent.
    *
    *       function RegisterIndexNativeEvent takes integer whichIndex, integer whichEvent, code func returns nothing
    *          Registers new event handler func for event whichEvent specific to index whichIndex.
    *
    *       function RegisterNativeEvent takes integer whichEvent, code func returns nothing
    *          Registers new event handler func for specified event whichEvent.
    *
    *****************************************************************************/

    library RegisterNativeEvent uses optional Table

    globals
        private integer eventIndex = 500 // 0-499 reserved for Blizzard native events
    endglobals

    private module NativeEventInit
        private static method onInit takes nothing returns nothing
    static if LIBRARY_Table then
            set table = TableArray[0x2000]
    endif
        endmethod
    endmodule

    private struct NativeEvent extends array
    static if LIBRARY_Table then
        static TableArray table
    else
        static hashtable table = InitHashtable()
    endif
        implement NativeEventInit
    endstruct

    function IsNativeEventRegistered takes integer whichIndex, integer whichEvent returns boolean
    static if LIBRARY_Table then
        return NativeEvent.table[whichEvent].trigger.has(whichIndex)
    else
        return HaveSavedHandle(NativeEvent.table, whichEvent, whichIndex)
    endif
    endfunction

    function RegisterNativeEventTrigger takes integer whichIndex, integer whichEvent returns boolean
        if not IsNativeEventRegistered(whichIndex, whichEvent) then
    static if LIBRARY_Table then
            set NativeEvent.table[whichEvent].trigger[whichIndex] = CreateTrigger()
    else
            call SaveTriggerHandle(NativeEvent.table, whichEvent, whichIndex, CreateTrigger())
    endif
            return true
        endif
        return false
    endfunction

    function GetIndexNativeEventTrigger takes integer whichIndex, integer whichEvent returns trigger
    static if LIBRARY_Table then
        return NativeEvent.table[whichEvent].trigger[whichIndex]
    else
        return LoadTriggerHandle(NativeEvent.table, whichEvent, whichIndex)
    endif
    endfunction

    function GetNativeEventTrigger takes integer whichEvent returns trigger
        return GetIndexNativeEventTrigger(bj_MAX_PLAYER_SLOTS, whichEvent)
    endfunction

    function CreateNativeEvent takes nothing returns integer
        local integer eventId = eventIndex
        call RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId)
        set eventIndex = eventIndex + 1
        return eventId
    endfunction

    function RegisterIndexNativeEvent takes integer whichIndex, integer whichEvent, code func returns nothing
        call RegisterNativeEventTrigger(whichIndex, whichEvent)
        call TriggerAddCondition(GetIndexNativeEventTrigger(whichIndex, whichEvent), Condition(func))
    endfunction

    function RegisterNativeEvent takes integer whichEvent, code func returns nothing
        call RegisterIndexNativeEvent(bj_MAX_PLAYER_SLOTS, whichEvent, func)
    endfunction

    endlibrary


    And now, the functionality for player, game, unit and playerunit native event types.

    RegisterPlayerUnitEvent:
    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterPlayerUnitEvent v1.0.3.2
    *       by Bannar
    *
    *    Register version of TriggerRegisterPlayerUnitEvent.
    *
    *    Special thanks to Magtheridon96, Bribe, azlier and BBQ for the original library version.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/threads/snippet-registerevent-pack.250266/
    *
    ******************************************************************************
    *
    *    Functions:
    *
    *       function GetAnyPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
    *          Retrieves trigger handle for playerunitevent whichEvent.
    *
    *       function GetPlayerUnitEventTrigger takes player whichPlayer, playerunitevent whichEvent returns trigger
    *          Retrieves trigger handle for playerunitevent whichEvent specific to player whichPlayer.
    *
    *       function RegisterAnyPlayerUnitEvent takes playerunitevent whichEvent, code func returns nothing
    *          Registers generic playerunitevent whichEvent adding code func as callback.
    *
    *       function RegisterPlayerUnitEvent takes player whichPlayer, playerunitevent whichEvent, code func returns nothing
    *          Registers playerunitevent whichEvent for player whichPlayer adding code func as callback.
    *
    *****************************************************************************/

    library RegisterPlayerUnitEvent requires RegisterNativeEvent

    function GetAnyPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function GetPlayerUnitEventTrigger takes player whichPlayer, playerunitevent whichEvent returns trigger
        return GetIndexNativeEventTrigger(GetPlayerId(whichPlayer), GetHandleId(whichEvent))
    endfunction

    function RegisterAnyPlayerUnitEvent takes playerunitevent whichEvent, code func returns nothing
        local integer eventId = GetHandleId(whichEvent)
        local integer index = 0
        local trigger t = null

        if RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId) then
            set t = GetNativeEventTrigger(eventId)
            loop
                call TriggerRegisterPlayerUnitEvent(t, Player(index), whichEvent, null)
                set index = index + 1
                exitwhen index == bj_MAX_PLAYER_SLOTS
            endloop
            set t = null
        endif

        call RegisterNativeEvent(eventId, func)
    endfunction

    function RegisterPlayerUnitEvent takes player whichPlayer, playerunitevent whichEvent, code func returns nothing
        local integer playerId = GetPlayerId(whichPlayer)
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(playerId, eventId) then
            call TriggerRegisterPlayerUnitEvent(GetIndexNativeEventTrigger(playerId, eventId), whichPlayer, whichEvent, null)
        endif

        call RegisterIndexNativeEvent(playerId, eventId, func)
    endfunction

    endlibrary

    RegisterPlayerEvent:

    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterPlayerEvent v1.0.2.2
    *       by Bannar
    *
    *    Register version of TriggerRegisterPlayerEvent.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/threads/snippet-registerevent-pack.250266/
    *
    ******************************************************************************
    *
    *    Functions:
    *
    *       function GetAnyPlayerEventTrigger takes playerevent whichEvent returns trigger
    *          Retrieves trigger handle for playerevent whichEvent.
    *
    *       function GetPlayerEventTrigger takes player whichPlayer, playerevent whichEvent returns trigger
    *          Retrieves trigger handle for playerevent whichEvent specific to player whichPlayer.
    *
    *       function RegisterAnyPlayerEvent takes playerevent whichEvent, code func returns nothing
    *          Registers generic playerevent whichEvent adding code func as callback.
    *
    *       function RegisterPlayerEvent takes player whichPlayer, playerevent whichEvent, code func returns nothing
    *          Registers playerevent whichEvent for player whichPlayer adding code func as callback.
    *
    *****************************************************************************/

    library RegisterPlayerEvent requires RegisterNativeEvent

    function GetAnyPlayerEventTrigger takes playerevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function GetPlayerEventTrigger takes player whichPlayer, playerevent whichEvent returns trigger
        return GetIndexNativeEventTrigger(GetPlayerId(whichPlayer), GetHandleId(whichEvent))
    endfunction

    function RegisterAnyPlayerEvent takes playerevent whichEvent, code func returns nothing
        local integer eventId = GetHandleId(whichEvent)
        local integer index = 0
        local trigger t = null

        if RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId) then
            set t = GetNativeEventTrigger(eventId)
            loop
                call TriggerRegisterPlayerEvent(t, Player(index), whichEvent)
                set index = index + 1
                exitwhen index == bj_MAX_PLAYER_SLOTS
            endloop
            set t = null
        endif

        call RegisterNativeEvent(eventId, func)
    endfunction

    function RegisterPlayerEvent takes player whichPlayer, playerevent whichEvent, code func returns nothing
        local integer playerId = GetPlayerId(whichPlayer)
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(playerId, eventId) then
            call TriggerRegisterPlayerEvent(GetIndexNativeEventTrigger(playerId, eventId), whichPlayer, whichEvent)
        endif

        call RegisterIndexNativeEvent(playerId, eventId, func)
    endfunction

    endlibrary

    RegisterGameEvent:

    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterGameEvent v1.0.0.6
    *       by Bannar
    *
    *    Register version of TriggerRegisterGameEvent.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/threads/snippet-registerevent-pack.250266/
    *
    ******************************************************************************
    *
    *    Functions:
    *
    *       function GetGameEventTrigger takes gameevent whichEvent returns trigger
    *          Retrieves trigger handle for gameevent whichEvent.
    *
    *       function RegisterGameEvent takes gameevent whichEvent, code func returns nothing
    *          Registers generic gameevent whichEvent adding code func as callback.
    *
    *****************************************************************************/

    library RegisterGameEvent requires RegisterNativeEvent

    function GetGameEventTrigger takes gameevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function RegisterGameEvent takes gameevent whichEvent, code func returns nothing
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId) then
            call TriggerRegisterGameEvent(GetNativeEventTrigger(eventId), whichEvent)
        endif

        call RegisterNativeEvent(eventId, func)
    endfunction

    endlibrary

    RegisterUnitEvent:

    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterUnitEvent v1.0.1.2
    *       by Bannar
    *
    *    Register version of TriggerRegisterUnitEvent.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/threads/snippet-registerevent-pack.250266/
    *
    ******************************************************************************
    *
    *    Functions:
    *
    *       function GetUnitEventTrigger takes unitevent whichEvent returns trigger
    *          Retrieves trigger handle for unitevent whichEvent.
    *
    *       function RegisterUnitEvent takes unit whichUnit, unitevent whichEvent, code func returns nothing
    *          Registers unitevent whichEvent for unit whichUnit adding code func as callback.
    *
    *****************************************************************************/

    library RegisterUnitEvent requires RegisterNativeEvent

    function GetUnitEventTrigger takes unitevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function RegisterUnitEvent takes unit whichUnit, unitevent whichEvent, code func returns nothing
        local integer unitId = GetHandleId(whichUnit)
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(unitId, eventId) then
            call TriggerRegisterUnitEvent(GetIndexNativeEventTrigger(unitId, eventId), whichUnit, whichEvent)
        endif

        call RegisterIndexNativeEvent(unitId, eventId, func)
    endfunction

    endlibrary

    Reference event-id values for RegisterNativeEvent:
    Code (vJASS):
    globals
        /**
         *  For use with RegisterGameEvent
         */

        constant integer gameevent_VICTORY         = 0
        constant integer gameevent_END_LEVEL       = 1
        constant integer gameevent_VARIABLE_LIMIT  = 2
        constant integer gameevent_STATE_LIMIT     = 3
        constant integer gameevent_TIMER_EXPIRED   = 4
        constant integer gameevent_ENTER_REGION    = 5
        constant integer gameevent_LEAVE_REGION    = 6
        constant integer gameevent_TRACKABLE_HIT   = 7
        constant integer gameevent_TRACKABLE_TRACK = 8
        constant integer gameevent_SHOW_SKILL      = 9
        constant integer gameevent_BUILD_SUBMENU   = 10
        /**
         *  For use with RegisterPlayerEvent
         */

        constant integer playerevent_STATE_LIMIT      = 11
        constant integer playerevent_ALLIANCE_CHANGED = 12
        constant integer playerevent_DEFEAT           = 13
        constant integer playerevent_VICTORY          = 14
        constant integer playerevent_LEAVE            = 15
        constant integer playerevent_CHAT             = 16
        constant integer playerevent_END_CINEMATIC    = 17
        /**
         *  For use with RegisterPlayerUnitEvent
         */

        constant integer playerunitevent_ATTACKED            = 18
        constant integer playerunitevent_RESCUED             = 19
        constant integer playerunitevent_DEATH               = 20
        constant integer playerunitevent_DECAY               = 21
        constant integer playerunitevent_DETECTED            = 22
        constant integer playerunitevent_HIDDEN              = 23
        constant integer playerunitevent_SELECTED            = 24
        constant integer playerunitevent_DESELECTED          = 25
        constant integer playerunitevent_CONSTRUCT_START     = 26
        constant integer playerunitevent_CONSTRUCT_CANCEL    = 27
        constant integer playerunitevent_CONSTRUCT_FINISH    = 28
        constant integer playerunitevent_UPGRADE_START       = 29
        constant integer playerunitevent_UPGRADE_CANCEL      = 30
        constant integer playerunitevent_UPGRADE_FINISH      = 31
        constant integer playerunitevent_TRAIN_START         = 32
        constant integer playerunitevent_TRAIN_CANCEL        = 33
        constant integer playerunitevent_TRAIN_FINISH        = 34
        constant integer playerunitevent_RESEARCH_START      = 35
        constant integer playerunitevent_RESEARCH_CANCEL     = 36
        constant integer playerunitevent_RESEARCH_FINISH     = 37
        constant integer playerunitevent_ISSUED_ORDER        = 38
        constant integer playerunitevent_ISSUED_POINT_ORDER  = 39
        constant integer playerunitevent_ISSUED_TARGET_ORDER = 40
        constant integer playerunitevent_ISSUED_UNIT_ORDER   = 40 // for compat
        constant integer playerunitevent_HERO_LEVEL          = 41
        constant integer playerunitevent_HERO_SKILL          = 42
        constant integer playerunitevent_HERO_REVIVABLE      = 43
        constant integer playerunitevent_HERO_REVIVE_START   = 44
        constant integer playerunitevent_HERO_REVIVE_CANCEL  = 45
        constant integer playerunitevent_HERO_REVIVE_FINISH  = 46
        constant integer playerunitevent_SUMMON              = 47
        constant integer playerunitevent_DROP_ITEM           = 48
        constant integer playerunitevent_PICKUP_ITEM         = 49
        constant integer playerunitevent_USE_ITEM            = 50
        constant integer playerunitevent_LOADED              = 51
        /**
         *  For use with RegisterUnitEvent
         */

        constant integer unitevent_DAMAGED             = 52
        constant integer unitevent_DEATH               = 53
        constant integer unitevent_DECAY               = 54
        constant integer unitevent_DETECTED            = 55
        constant integer unitevent_HIDDEN              = 56
        constant integer unitevent_SELECTED            = 57
        constant integer unitevent_DESELECTED          = 58
        constant integer unitevent_STATE_LIMIT         = 59
        constant integer unitevent_ACQUIRED_TARGET     = 60
        constant integer unitevent_TARGET_IN_RANGE     = 61
        constant integer unitevent_ATTACKED            = 62
        constant integer unitevent_RESCUED             = 63
        constant integer unitevent_CONSTRUCT_CANCEL    = 64
        constant integer unitevent_CONSTRUCT_FINISH    = 65
        constant integer unitevent_UPGRADE_START       = 66
        constant integer unitevent_UPGRADE_CANCEL      = 67
        constant integer unitevent_UPGRADE_FINISH      = 68
        constant integer unitevent_TRAIN_START         = 69
        constant integer unitevent_TRAIN_CANCEL        = 70
        constant integer unitevent_TRAIN_FINISH        = 71
        constant integer unitevent_RESEARCH_START      = 72
        constant integer unitevent_RESEARCH_CANCEL     = 73
        constant integer unitevent_RESEARCH_FINISH     = 74
        constant integer unitevent_ISSUED_ORDER        = 75
        constant integer unitevent_ISSUED_POINT_ORDER  = 76
        constant integer unitevent_ISSUED_TARGET_ORDER = 77
        constant integer unitevent_HERO_LEVEL          = 78
        constant integer unitevent_HERO_SKILL          = 79
        constant integer unitevent_HERO_REVIVABLE      = 80
        constant integer unitevent_HERO_REVIVE_START   = 81
        constant integer unitevent_HERO_REVIVE_CANCEL  = 82
        constant integer unitevent_HERO_REVIVE_FINISH  = 83
        constant integer unitevent_SUMMON              = 84
        constant integer unitevent_DROP_ITEM           = 85
        constant integer unitevent_PICKUP_ITEM         = 86
        constant integer unitevent_USE_ITEM            = 87
        constant integer unitevent_LOADED              = 88
        /**
         *  For use with RegisterGameEvent
         */

        constant integer gameevent_LOADED                 = 256
        constant integer gameevent_TOURNAMENT_FINISH_SOON = 257
        constant integer gameevent_TOURNAMENT_FINISH_NOW  = 258
        constant integer gameevent_SAVE                   = 259
        /**
         *  For use with RegisterPlayerEvent
         */

        constant integer playerevent_ARROW_LEFT_DOWN  = 261
        constant integer playerevent_ARROW_LEFT_UP    = 262
        constant integer playerevent_ARROW_RIGHT_DOWN = 263
        constant integer playerevent_ARROW_RIGHT_UP   = 264
        constant integer playerevent_ARROW_DOWN_DOWN  = 265
        constant integer playerevent_ARROW_DOWN_UP    = 266
        constant integer playerevent_ARROW_UP_DOWN    = 267
        constant integer playerevent_ARROW_UP_UP      = 268
        constant integer playerevent_MOUSE_DOWN       = 269
        constant integer playerevent_MOUSE_UP         = 270
        constant integer playerevent_MOUSE_MOVE       = 271
        /**
         * For use with RegisterPlayerUnitEvent
         */

        constant integer playerunitevent_SELL          = 272
        constant integer playerunitevent_CHANGE_OWNER  = 273
        constant integer playerunitevent_SELL_ITEM     = 274
        constant integer playerunitevent_SPELL_CHANNEL = 275
        constant integer playerunitevent_SPELL_CAST    = 276
        constant integer playerunitevent_SPELL_EFFECT  = 277
        constant integer playerunitevent_SPELL_FINISH  = 278
        constant integer playerunitevent_SPELL_ENDCAST = 279
        constant integer playerunitevent_PAWN_ITEM     = 280
        /**
         *  For use with RegisterUnitEvent
         */

        constant integer unitevent_SELL          = 289
        constant integer unitevent_CHANGE_OWNER  = 290
        constant integer unitevent_SELL_ITEM     = 291
        constant integer unitevent_SPELL_CHANNEL = 292
        constant integer unitevent_SPELL_CAST    = 293
        constant integer unitevent_SPELL_EFFECT  = 294
        constant integer unitevent_SPELL_FINISH  = 295
        constant integer unitevent_SPELL_ENDCAST = 296
        constant integer unitevent_PAWN_ITEM     = 297
    endglobals

    Reference to obsolete snippets

    RegisterPlayerUnitEvent:
    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterPlayerUnitEvent v1.0.2.1
    *       by Bannar
    *
    *    Register version of TriggerRegisterPlayerUnitEvent.
    *
    *    Special thanks to Magtheridon96, Bribe, azlier and BBQ for the original library version.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/forums/submissions-414/snippet-registerevent-pack-250266/
    *
    ******************************************************************************
    *
    *    constant boolean RPUE_VERSION_NEW
    *       Defines API style. Choose between compatibility with standard RPUE or Blizzard alike interface
    *
    *
    *    Functions:
    *
    *       function Register(Any)PlayerUnitEvent takes playerunitevent whichEvent, code cb returns nothing
    *          registers generic playerunitevent whichEvent adding code cb as callback
    *
    *       function RegisterPlayerUnitEvent(ForPlayer) takes player whichPlayer, playerunitevent whichEvent, code cb returns nothing
    *          registers playerunitevent whichEvent for player whichPlayer adding code cb as callback
    *
    *       function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
    *          retrieves trigger handle for playerunitevent whichEvent
    *
    *       function GetPlayerUnitEventTriggerForPlayer takes player whichPlayer, playerunitevent whichEvent returns trigger
    *          retrieves trigger handle for playerunitevent whichEvent specific to player whichPlayer
    *
    *****************************************************************************/

    library RegisterPlayerUnitEvent requires RegisterNativeEvent

    globals
        constant boolean RPUE_VERSION_NEW = false
    endglobals

    //! textmacro_once DEFINE_REGISTER_PLAYER_UNIT_EVENT takes GENERIC, SPECIFIC
    function Register$GENERIC$PlayerUnitEvent takes playerunitevent whichEvent, code cb returns nothing
        local integer eventId = GetHandleId(whichEvent)
        local integer index = 0
        local trigger t = null

        if RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId) then
            set t = GetNativeEventTrigger(eventId)
            loop
                call TriggerRegisterPlayerUnitEvent(t, Player(index), whichEvent, null)
                set index = index + 1
                exitwhen index == bj_MAX_PLAYER_SLOTS
            endloop
            set t = null
        endif

        call TriggerAddCondition(GetNativeEventTrigger(eventId), Condition(cb))
    endfunction

    function RegisterPlayerUnitEvent$SPECIFIC$ returns nothing
        local integer playerId = GetPlayerId(whichPlayer)
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(playerId, eventId) then
            call TriggerRegisterPlayerUnitEvent(GetIndexNativeEventTrigger(playerId, eventId), whichPlayer, whichEvent, null)
        endif

        call TriggerAddCondition(GetIndexNativeEventTrigger(playerId, eventId), Condition(cb))
    endfunction
    //! endtextmacro

    static if RPUE_VERSION_NEW then
        //! runtextmacro DEFINE_REGISTER_PLAYER_UNIT_EVENT("Any", " takes player whichPlayer, playerunitevent whichEvent, code cb")
    else
        //! runtextmacro DEFINE_REGISTER_PLAYER_UNIT_EVENT("", "ForPlayer takes playerunitevent whichEvent, code cb, player whichPlayer")
    endif

    function GetPlayerUnitEventTrigger takes playerunitevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function GetPlayerUnitEventTriggerForPlayer takes player whichPlayer, playerunitevent whichEvent returns trigger
        return GetIndexNativeEventTrigger(GetPlayerId(whichPlayer), GetHandleId(whichEvent))
    endfunction

    endlibrary

    RegisterPlayerEvent:
    Code (vJASS):
    /*****************************************************************************
    *
    *    RegisterPlayerEvent v1.0.1.1
    *       by Bannar
    *
    *    Register version of TriggerRegisterPlayerEvent.
    *
    ******************************************************************************
    *
    *    Requirements:
    *
    *       RegisterNativeEvent by Bannar
    *          hiveworkshop.com/forums/submissions-414/snippet-registerevent-pack-250266/
    *
    ******************************************************************************
    *
    *    Functions:
    *
    *       function RegisterAnyPlayerEvent takes playerevent whichEvent, code cb returns nothing
    *          registers generic playerevent whichEvent adding code cb as callback
    *
    *       function RegisterPlayerEvent takes player whichPlayer, playerevent whichEvent, code cb returns nothing
    *          registers playerevent whichEvent for player whichPlayer adding code cb as callback
    *
    *       function GetPlayerEventTrigger takes playerevent whichEvent returns trigger
    *          retrieves trigger handle for playerevent whichEvent
    *
    *       function GetPlayerEventTriggerForPlayer takes player whichPlayer, playerevent whichEvent returns trigger
    *          retrieves trigger handle for playerevent whichEvent specific to player whichPlayer
    *
    *****************************************************************************/

    library RegisterPlayerEvent requires RegisterNativeEvent

    function RegisterAnyPlayerEvent takes playerevent whichEvent, code cb returns nothing
        local integer eventId = GetHandleId(whichEvent)
        local integer index = 0
        local trigger t = null

        if RegisterNativeEventTrigger(bj_MAX_PLAYER_SLOTS, eventId) then
            set t = GetNativeEventTrigger(eventId)
            loop
                call TriggerRegisterPlayerEvent(t, Player(index), whichEvent)
                set index = index + 1
                exitwhen index == bj_MAX_PLAYER_SLOTS
            endloop
            set t = null
        endif

        call TriggerAddCondition(GetNativeEventTrigger(eventId), Condition(cb))
    endfunction

    function RegisterPlayerEvent takes player whichPlayer, playerevent whichEvent, code cb returns nothing
        local integer playerId = GetPlayerId(whichPlayer)
        local integer eventId = GetHandleId(whichEvent)

        if RegisterNativeEventTrigger(playerId, eventId) then
            call TriggerRegisterPlayerEvent(GetIndexNativeEventTrigger(playerId, eventId), whichPlayer, whichEvent)
        endif

        call TriggerAddCondition(GetIndexNativeEventTrigger(playerId, eventId), Condition(cb))
    endfunction

    function GetPlayerEventTrigger takes playerevent whichEvent returns trigger
        return GetNativeEventTrigger(GetHandleId(whichEvent))
    endfunction

    function GetPlayerEventTriggerForPlayer takes player whichPlayer, playerevent whichEvent returns trigger
        return GetIndexNativeEventTrigger(GetPlayerId(whichPlayer), GetHandleId(whichEvent))
    endfunction

    endlibrary
     
    Last edited: Jun 14, 2018
  2. PurgeandFire

    PurgeandFire

    Code Moderator

    Joined:
    Nov 11, 2006
    Messages:
    7,427
    Resources:
    18
    Icons:
    1
    Spells:
    4
    Tutorials:
    9
    JASS:
    4
    Resources:
    18
    Seems like a cool idea. Atm there are some issues. Try this code out:
    Code (vJASS):
    library A initializer Init requires RegisterPlayerEvent
        private function OnEsc takes nothing returns boolean
            call BJDebugMsg("Hello World!")
            return false
        endfunction
       
        private function OnChat takes nothing returns boolean
            call BJDebugMsg("Chat!")
            return false
        endfunction

        private function Init takes nothing returns nothing
            call RegisterAnyPlayerEvent(EVENT_PLAYER_END_CINEMATIC, function OnEsc)
            call RegisterAnyPlayerEvent(EVENT_PLAYER_CHAT, function OnChat)
        endfunction
    endlibrary


    It didn't work for me. It seemed to work after I added a second event registry, but then it ended up having weird bugs (e.g. if I put the same register 3 times, it would fire the actions 6 times).
     
  3. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    Thanks for spotting this out. Its because of multiple updates done just before uploading script to accomodate it for hive :p

    Was checking for false during register process instead of true within Register<>Event libs.

    Should be fixed now, at least I hope so.

    Edit: should I add hidden tags for each of those libs?
     
  4. Bribe

    Bribe

    Joined:
    Sep 26, 2009
    Messages:
    8,006
    Resources:
    25
    Maps:
    3
    Spells:
    10
    Tutorials:
    3
    JASS:
    9
    Resources:
    25
    Hidden tags would be good.
     
  5. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    Added RegisterPlayerUnitEvent. It contains whats missing within Maggys RPUE.
    Separate triggers for each player per playerunitevent are not created. Instead, there is one trigger per event as it should be. Creates less handles.
    Allows one to retrieve any trigger handle which is in use due to reason mentioned above.

    This script is not ment to have the same api as Maggys. It follows standard warcraft3 naming convention: Register/RegisterAny.
    Since all of my previous scripts follow that rule, making an exception would be pointless.

    I've been using all of those through my codes and found no problems or errors. Retrieving triggers is so much needed, escpecially when every event is registered via such lib and you want to disable/enable trigger temporarily, preventing events from taking place. Functionality > less functionality.
     
  6. Ruke

    Ruke

    Joined:
    Sep 19, 2011
    Messages:
    517
    Resources:
    7
    Tools:
    1
    Spells:
    5
    Wurst:
    1
    Resources:
    7
    is it really that bad to have several triggers with the same event that you did this?
     
  7. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    The native library existed for some time now. And this version of RPUE was done couple weeks later after maggy uploaded his, since the missing things always prevented me from using this "modificated version". And for the sake of completeness, its good to have this one too here.

    And Ruke, those "things" were reported in Maggies thread several times already, it's been years now. There is no reason not to paste this here. Whatmore, when using registerevents from this libs, it was pain in ass to couple naming convention with original RPUG.

    Fact that those several triggers were created was not the worse part. I've had troubles with retrieval of triggers many times, because original RPUG wouldnt allow me to. Also, this "RegisterEvent" comes as a pack. The main lib is core for every other since those are just unique wrappers. Win win.
     
  8. Ruke

    Ruke

    Joined:
    Sep 19, 2011
    Messages:
    517
    Resources:
    7
    Tools:
    1
    Spells:
    5
    Wurst:
    1
    Resources:
    7
    read the hole thread, the only i could find was "This doesn't change trigger's run performance, yes it slowers registration process a little but the point of snippet is reducing handle count anyway."...
     
  9. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    GetHandleId(GetTriggeringTrigger())
     
  10. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    @edo494 perfect example of.. what everyone knows. And now, will GetTriggeringTrigger retrieve handle in other triggers execution? Dont be foolish. Sometimes you need to get access to handle from the "outside".

    @Ruke, which thread? RPUE? Dude, some pages I learnt by heart.
    If u speak of reduction of trigger handles - this one is even better. Plus, once again, you can get every trigger u want.

    Whatever man, you only use if u want to. It's your own choice. However, there is nothing wrong with uploading different version, is there?
     
  11. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    no, you said you cant, I show you can. And more to the point, whats the point of manipulating the executing trigger? if you want to remove it at certain point, you can just as much use your own trigger. There is 0 cases where you would use RegisterXXXEvent with trigger retrieval that I can think of.

    I dont quite understand how this works internally, I didnt take the time to look into it very good, and it is a bit bigger than Mags one, but it has the other events, which is very good in my opinion
     
  12. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    @edo494 It wasn't about just showing "how can we retrive trigger handle". Read the topic again. I was in need of retrival many times - you need you use. If you don't, whats the point of denying the need?

    This pack was uploaded some time ago. And it's not RPUE as you both arguing about, in fact RPUE is one of extensions pack brings. Registering Player/Game/Unit in such form wasn't even uploaded on hive. And the purpose of RegisterNativeEvent is to archive everything within same core.

    Now, you say Maggies is shorter, true, yet when you were to use additional one, lets say for Player, then this pack would be a better choice without a doubt.

    EDIT: To clarify: You can read form posts above why I've uploaded RPUE, yet ITS NOT RPUE per se. This one is a "pack" with kind of "core" as a storage.
     
  13. Ruke

    Ruke

    Joined:
    Sep 19, 2011
    Messages:
    517
    Resources:
    7
    Tools:
    1
    Spells:
    5
    Wurst:
    1
    Resources:
    7
    bannar, i don't even use vjass anymore, but i do wanna know if this is really worth it, because yes, you keep trigger handlers a little bit lower, but does this make any difference?
     
  14. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    Nope? RPUE made no difference in performance, nor does this one.

    This offers just 2 points, which has been mentioned milion times:
    -less handles (even lower amount than in popular RPUE)
    -posibility to retrieve any trigger handle

    There is non:
    Code (vJASS):
    local integer i = 16 * GetHandleId(p) + GetPlayerId(pl)
    :)

    RegisterNativeEvent does the job. The rest of scripts are just wrappers to make everything clean, and to bring proper naming convention as stated.
     
  15. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    well yea lower handle count is nice and dandy and all, but you know, we are not living in 2003 where every 200 bytes object matters, nor is anyone running Warcraft 3 on embedded system like Arduino, where the memory is very limited
     
  16. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    And ^this should prevent me from uploading something I've been using for a while, and has enough quality in my opinion to share with hive users?

    Guys with such atitude, none should upload anything in case most guys viewing jass section got their own, modified codes and it's unlikely that they will change their habits.
     
  17. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    no, this is still good enough resource for the additional Register functions, but I wouldnt necessarily point out the "uses less handles" as pro, cause that is not really concern even on 512 MB ram machine
     
  18. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    Y, and UnitDex by TH exists. And basically the only difference between his and Nes one is that its smaller and uses no requirements. So..

    None uses 2000 machines now, guess TH should remove his script? ^)^
    Nope, in fact I rly enjoy using both UI :)

    Btw, without that pro, original RPUE has no point of existence either.
     
  19. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,855
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    it has point of existance, because you dont have to write loop every time you want PlayerUnitEvent registered, you just call one function.

    No, having "less" handles is not pro in my opinion. But you know, different people, different opinions

    And UnitDex is the only Jass unit indexer currently on Hive
     
  20. Bannar

    Bannar

    Joined:
    Mar 19, 2008
    Messages:
    3,087
    Resources:
    20
    Spells:
    5
    Tutorials:
    1
    JASS:
    14
    Resources:
    20
    So does mine :)
    Everything related with "registeringevents" with better execution.