System Event Broadcaster

Reaction score
333
I previously had a bulky and non-dynamic system which could perform this sort of stuff, but this one is better. You can attach functions to an "event" string, and then you can broadcast the event in order to call all functions associated with it.

Pros:

1. Dynamic - can attach or remove actions associated with any string at any time.
2. Scalable - as long as you stay below the limit of around 8190 associations, there is no fixed limit on how many associations a single event can have. 800 events with 10 actions each is just as possible as 10 events with 800 actions each.
3. Multiple usages - can be used to attach actions to a unit type, a handle. Anything that can be expressed in string form somehow can have actions attached.

Cons:

1. Slow.
2. Can't pass arguments to associated functions. You'll have to attach data to event-responses or use globals.

Copy into map header or put in empty trigger to use:

JASS:
library Event initializer DeclareCache
    globals
        private gamecache gc
    endglobals

    private struct link
        string str
        link parent = 0
        link child = 0
        
        static method create takes string str returns link
            local link li = link.allocate()
            set li.str = str
            return li
        endmethod
    endstruct

    private function DeclareCache takes nothing returns nothing
        set gc = InitGameCache(SCOPE_PRIVATE+"vars.w3v")
    endfunction

    private function FindLastLink takes link li returns link
        if (li.child == 0) then
            return li
        endif

        return FindLastLink.evaluate(li.child)
    endfunction

    private function DeleteLink takes link li returns nothing
        set li.parent.child = li.child
        set li.child.parent = li.parent
        call li.destroy()
    endfunction

    private function GetLink takes link li, string str returns link
        if (li.str == str) then
            return li
        elseif (li.child == 0) then
            return 0
        endif

        return GetLink.evaluate(li.child, str)
    endfunction

    public function AddAction takes string ev, string ac returns nothing
        local link li = GetStoredInteger(gc, SCOPE_PRIVATE+"event", ev)

        if (li == 0) then
            set li = link.create(ac)
            call StoreInteger(gc, SCOPE_PRIVATE+"event", ev, li)
        else
            set li = FindLastLink(li)
            set li.child = link.create(ac)
            set li.child.parent = li
        endif
    endfunction

    public function RemoveAction takes string ev, string ac returns nothing
        local link li = GetStoredInteger(gc, SCOPE_PRIVATE+"event", ev)

        if (li != 0 and li.str != ac) then
            call DeleteLink(GetLink(li, ac))
        elseif (li != 0) then
            if (li.child != 0) then
                 call StoreInteger(gc, SCOPE_PRIVATE+"event", ev, li.child)
            else
                 call FlushStoredInteger(gc, SCOPE_PRIVATE+"event", ev)
            endif
            call DeleteLink(GetLink(li, ac))
        endif
    endfunction

    public function Broadcast takes string ev returns nothing
        local link li = GetStoredInteger(gc, SCOPE_PRIVATE+"event", ev)

        if (li != 0) then
            loop
                call ExecuteFunc(li.str)
                exitwhen li.child == 0
                set li = li.child
            endloop
        endif
    endfunction
endlibrary


Usage details:

Use Event_AddAction(string event, string action) to associate an event with an action. Event_RemoveAction removes an action from an event. Call Event_Broadcast(string event) to run the associated actions.

Update: I have added a test map.
 

Attachments

  • EventBroadcaster.w3x
    22 KB · Views: 265
sounds pretty similiar to what vexorian did in his caster system event casting module
OnAbilityEffect('AbilityId',"FuncName")
OnAbilityPrecast('AbilityId',"FuncName")
etc.

Yours sounds interesting nonetheless; could you give a more elaborate example and illustrate the possibilities of your system?
 
sounds pretty similiar to what vexorian did in his caster system event casting module
OnAbilityEffect('AbilityId',"FuncName")
OnAbilityPrecast('AbilityId',"FuncName")
etc.

Yours sounds interesting nonetheless; could you give a more elaborate example and illustrate the possibilities of your system?

Well, I will use it for a damage detection system, after I optimize it.

call Event_Broadcast("damaged") in the actions for your generic damage detection system. You can then easily add actions for things like calculating crit chance, etc. A trigger that calculates a crit chance might call Event_Broadcast("critical"), which would make it easy to create an item which, say, adds a special effect to critical strikes.

You could also have a trigger which calls Event_Broadcast(I2S(GetUnitTypeId(GetTriggerUnit()))) when a unit enters a region, and add an action to the footman type which causes the triggering unit to explode. Footmen entering the region would explode.

All this stuff can be accomplished through other means, but its easier to use broadcasting events.
 
sounds good.

how about adding a map with some usage examples?

why do you say it's slow? The code doesn't look that bulky. And there are enough alternatives for gamecache around.
 
sounds good.

how about adding a map with some usage examples?

why do you say it's slow? The code doesn't look that bulky. And there are enough alternatives for gamecache around.

Its not too slow if you only use it occasionally, but it might not be fast enough for intensive use.

Anyway, I have attached a test map which may give ideas as to possible usages.

EDIT: Attachment added for no particular reason.
 
I've looked at it, and I find it quite useful:
E.g. for a map we*re about to have a little physics effect for shard damage done by siege tanks so that units in a small area around the impact field are thrown around. Your event system can be of help there.

+rep
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • The Helper The Helper:
    It is weird seeing a way more realistic users online number
  • The Helper The Helper:
    Happy Tuesday Night!
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top