Snippet RegisterPlayerUnitEvent

Magthridon96

Member
Reaction score
2
I submitted this a while ago on The Hive and thought it
would be appropriate to post it here as well.

What it does:
- Replaces Blizzard's "CreateTrigger - RegisterEvent - AddCondition - AddAction" with a simple "RegisterPlayerUnitEvent"
- Optimizes scripts (You know what slows down your map? Hundreds upon hundreds of triggers firing.)
- This also reduces the amount of RAM used.

Special Thanks to Bribe, azlier and BBQ :)

JASS:
/**************************************************************
*
*   RegisterPlayerUnitEvent
*   v4.2.0.0
*   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
*   return false. They can return nothing as well.
*
*   Disclaimer:
*   -----------
*
*       - Don't use TriggerSleepAction inside registered code.
*
*      API:
*      ----
*
*       function RegisterPlayerUnitEvent
*           takes
*               playerunitevent whichEvent  :   The event you would like to register
*               code whichFunction          :   The code you would like to register
*           returns
*               nothing
*
*           - Registers code that will execute when an event fires.
*
**************************************************************/
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
endlibrary</i></i></i></i>


Feel free to comment..
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Magthridon96:
1. - Optimizes scripts (You know what slows down your map? Hundreds upon hundreds of triggers firing.)

1. Well I can see how this reduces RAM but can you elaborate more on this (optimizes scripts) point. I mean scripts start executing/running after the triggers have fired so where is the script optimization? Do you mean that the optimization is that it (RegisterPlayerUnitEvent) reduces the lines of script required? Or the optimization is that less triggers have to fire?

Edit:

Is there anything that prevents the other event groups (unitevent, playerevent, gameevent) being wrapped in a similar function? Why just the playerunitevent =)?
 

luorax

Invasion in Duskwood
Reaction score
67
Well, imagine, you have 30 triggers with an "A hero learns a skill" event. When you learn one, each trigger fires and does it job, in a different thread, at the same time. This might cause lags depending on how many triggers you have.

If you use this snippet, those 30 events will be fired by the same trigger, after each other.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
luorax:
1. This might cause lags depending on how many triggers you have.

2. If you use this snippet, those 30 events will be fired by the same trigger, after each other.

1. How many triggers with the same event does one needs to have to actually get lag from the firing of the triggers themselves not the action they perform 30, 300 or 3000?

2. Sure I agree less triggers, less events less RAM usage but does someone know if there is a limit to the amount of conditions/actions a trigger can have or does their "stack" increases as needed?
 

Bribe

vJass errors are legion
Reaction score
67
No one's ever reported a condition limit on triggers.

This also saves 17 handles per duplicate playerunitevent. Saves creation of trigger and 16 event handles.
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
Bribe:
1. No one's ever reported a condition limit on triggers.

1. Yeah guess their "stack" really does increase, tested with 350.
 

tooltiperror

Super Moderator
Reaction score
231
This is a lot less overhead than GTrigger, that is good. But it should still work with trigger(condition)s, as it does in GTrigger.

JASS:
if (TriggerEvaluate(t)) then
    call TriggerExecute(t)
endif
 

Nestharus

o-o
Reaction score
84
JASS:

if (TriggerEvaluate(t)) then
    call TriggerExecute(t)
endif


This is no good unless you split the triggers up... you are merging trigger conditions here, meaning that any of these trigger conditions could return any boolean. That means the trigger execute may or may not fire from different resources that are unrelated.

The if statement is completely useless, thus this shouldn't be done.

If the triggers were completely split up, that'd be a different story ; ).

edit
It seems that I misunderstood what you were saying. I believe that mag wrote this with combining all triggers into one trigger and running off of conditions in mind. He didn't write this to support the normal TriggerEvaluate TriggerExecute stuff = ).

Yes, I can understand why you are saying that the thing should work just like regular triggers, but mag didn't write it for regular triggers ^^. It's more for the onInit stuff in libraries = ). It's like a RegisterAnyPlayerEvent for a function condition =).


I personally find it easy to use myself and like how it combines the triggers ^)^. I was never a fan of GTrigger, heh, that did way more than I needed or wanted =).
 

Bribe

vJass errors are legion
Reaction score
67
I don't understand why anyone wants both triggers and trigger actions. Conditions can "return nothing" now as Blizzard fixed the Mac bug, and anything else that makes trigger actions valuable is either deprecated or easy to implement using an ExecuteFunc statement.
 

tooltiperror

Super Moderator
Reaction score
231
Because WC3 events are great, and they are a lot better for extending code and keeping it mangeable. Event systems work with triggers, I don't see why you would want to limit your system to be less functional.
 

Magthridon96

Member
Reaction score
2
If you need conditions, just do this:

JASS:
function TriggerAction takes nothing returns nothing
    if Conditions() then
        // run
    endif
endfunction

private struct Inits extends array
    private static method onInit takes nothing returns nothing
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function TriggerActions)
    endmethod
endstruct


Pretty simple.

Is there anything that prevents the other event groups (unitevent, playerevent, gameevent) being wrapped in a similar function? Why just the playerunitevent =)?

playerunitevents are the ones that we want to optimize because of how often they're used =)

If you use this snippet, those 30 events will be fired by the same trigger, after each other.

The original trigger-registry method does the same thing. This has much less overhead though because it limits the number of triggers per event to 1 -> Less RAM and less trigger evaluations/executions.

Example:
30 triggers -> Each with one death event and 1 function
1 triggers -> 30 events and 30 functions

A unit dies
30 triggers -> 30 TriggerEvaluations
1 trigger -> 1 TriggerEvaluation
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
What is the point of multiple triggers, if they are all getting passed the same conditions? Wouldn't it be simpler to just put them all on one trigger, since there isn't any code that actually differentiates between the different triggers?

You need to include its limitations, and the things it requires to work. For example:
Code passed cannot use TriggerSleepAction or PolledWaits.
All code passed must return a boolean.
All code passed must return true.
etc.
 

Bribe

vJass errors are legion
Reaction score
67
Code passed can return false.
Code passed can return true.
Code passed can return nothing (newsflash: it is no longer a bug for Mac, and yes pJass lets it compile with RegisterPlayerUnitEvent)

Someone using trigger sleep actions is going to get trolled and flamed.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Code passed can return false.
Code passed can return true.

I haven't worked much with using trigger actions as conditions. All conditions are evaluated, even if one returns false in the process? I assumed it would follow short-circuit evaluation.

Code passed can return nothing (newsflash: it is no longer a bug for Mac, and yes pJass lets it compile with RegisterPlayerUnitEvent)

What would the trigger condition default it to?

Someone using trigger sleep actions is going to get trolled and flamed.

It is valid JASS, and it DOES have its uses, believe it or not. This thinking is similar to the thinking of "All BJs are bad".

JASS:
library foo initializer init uses RegisterPlayerUnitEvent

    function condition takes nothing returns nothing
        local item it = GetManipulatedItem()
        local real x = GetItemX(it)
        local real y = GetItemY(it)
        call BJDebugMsg(&quot;Item is located at (&quot; + R2S(x) + &quot;, &quot; + R2S(y) + &quot;).&quot;) // Item is located at (0, 0).  (or wherever the last time the item was when it was picked up)
        call TriggerSleepAction(0)
        set x = GetItemX(it)
        set y = GetItemY(it)
        call BJDebugMsg(&quot;Item is located at (&quot; + R2S(x) + &quot;, &quot; + R2S(y) + &quot;).&quot;) // This line is never reached.  Uh oh.
    endfunction

    function action takes nothing returns nothing
        local item it = GetManipulatedItem()
        local real x = GetItemX(it)
        local real y = GetItemY(it)
        call BJDebugMsg(&quot;Item is located at (&quot; + R2S(x) + &quot;, &quot; + R2S(y) + &quot;).&quot;) // Item is located at (0, 0).  (or wherever the last time the item was when it was picked up)
        call TriggerSleepAction(0)
        set x = GetItemX(it)
        set y = GetItemY(it)
        call BJDebugMsg(&quot;Item is located at (&quot; + R2S(x) + &quot;, &quot; + R2S(y) + &quot;).&quot;) // Item is located at (12.6500, 999.3000). (the actual correct location)
    endfunction

    function init takes nothing returns nothing
        local trigger t = CreateTrigger()
        call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_DROP_ITEM, function condition)
        call TriggerRegisterPlayerUnitEvent(t, Player(0), EVENT_PLAYER_UNIT_DROP_ITEM, null)
        // Repeat for other players...
        call TriggerAddAction(t, function action)
    endfunction
endlibrary
 

Bribe

vJass errors are legion
Reaction score
67
Only [ljass]Or[/ljass]/[ljass]And[/ljass] short circuit and I don't know why trigger conditions are different but they are you can have as many returning false or returning nothing as you want and the next one in the queue will fire regardless. And since there are no actions the condition return value doesn't even matter.

If I had to guess "return nothing" would default "false" but I have not tested it. I don't use it with actions anyway.

Yeah it is extremely rare and 99.999999% of the time needs to be replaced with a timer, but I can see the point in him adding a disclaimer saying "don't use TSA with this".

I know the uses of having a TSA, and I am not a raving moron stating "all bj's are bad" in fact I have met people so naive they won't even use bj_lastCreatedGroup or bj_mapInitialPlayableArea. The math BJ's are among my favorites to use, it is a shame they are highlighted red. BJDebugMsg is a pretty good one because I hate typing the ridiculously long DisplayTimedTextToPlayer(GetLocalPlayer(), 0, 0, 60, "").
 

Magthridon96

Member
Reaction score
2
Going to fix documentation.

And yeah, it is a shame that the Math BJs are in red :O
I only avoid them because of the evil red color.
 

Magthridon96

Member
Reaction score
2
Updated.

Added some really cool functions:
EnablePlayerUnitEvent
DisablePlayerUnitEvent

These are really useful when you want to do some very 'sensitive' actions like adding items to a unit that get removed at the end of a function.

edit
Bad idea. Removed.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top