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.
  • Ghan Ghan:
    Still lurking
    +3
  • The Helper The Helper:
    I am great and it is fantastic to see you my friend!
    +1
  • The Helper The Helper:
    If you are new to the site please check out the Recipe and Food Forum https://www.thehelper.net/forums/recipes-and-food.220/
  • Monovertex Monovertex:
    How come you're so into recipes lately? Never saw this much interest in this topic in the old days of TH.net
  • Monovertex Monovertex:
    Hmm, how do I change my signature?
  • tom_mai78101 tom_mai78101:
    Signatures can be edit in your account profile. As for the old stuffs, I'm thinking it's because Blizzard is now under Microsoft, and because of Microsoft Xbox going the way it is, it's dreadful.
  • The Helper The Helper:
    I am not big on the recipes I am just promoting them - I use the site as a practice place promoting stuff
    +2
  • Monovertex Monovertex:
    @tom_mai78101 I must be blind. If I go on my profile I don't see any area to edit the signature; If I go to account details (settings) I don't see any signature area either.
  • The Helper The Helper:
    You can get there if you click the bell icon (alerts) and choose preferences from the bottom, signature will be in the menu on the left there https://www.thehelper.net/account/preferences
  • The Helper The Helper:
    I think I need to split the Sci/Tech news forum into 2 one for Science and one for Tech but I am hating all the moving of posts I would have to do
  • The Helper The Helper:
    What is up Old Mountain Shadow?
  • The Helper The Helper:
    Happy Thursday!
    +1
  • Varine Varine:
    Crazy how much 3d printing has come in the last few years. Sad that it's not as easily modifiable though
  • Varine Varine:
    I bought an Ender 3 during the pandemic and tinkered with it all the time. Just bought a Sovol, not as easy. I'm trying to make it use a different nozzle because I have a fuck ton of Volcanos, and they use what is basically a modified volcano that is just a smidge longer, and almost every part on this thing needs to be redone to make it work
  • Varine Varine:
    Luckily I have a 3d printer for that, I guess. But it's ridiculous. The regular volcanos are 21mm, these Sovol versions are about 23.5mm
  • Varine Varine:
    So, 2.5mm longer. But the thing that measures the bed is about 1.5mm above the nozzle, so if I swap it with a volcano then I'm 1mm behind it. So cool, new bracket to swap that, but THEN the fan shroud to direct air at the part is ALSO going to be .5mm to low, and so I need to redo that, but by doing that it is a little bit off where it should be blowing and it's throwing it at the heating block instead of the part, and fuck man
  • Varine Varine:
    I didn't realize they designed this entire thing to NOT be modded. I would have just got a fucking Bambu if I knew that, the whole point was I could fuck with this. And no one else makes shit for Sovol so I have to go through them, and they have... interesting pricing models. So I have a new extruder altogether that I'm taking apart and going to just design a whole new one to use my nozzles. Dumb design.
  • Varine Varine:
    Can't just buy a new heatblock, you need to get a whole hotend - so block, heater cartridge, thermistor, heatbreak, and nozzle. And they put this fucking paste in there so I can't take the thermistor or cartridge out with any ease, that's 30 dollars. Or you can get the whole extrudor with the direct driver AND that heatblock for like 50, but you still can't get any of it to come apart
  • Varine Varine:
    Partsbuilt has individual parts I found but they're expensive. I think I can get bits swapped around and make this work with generic shit though
  • Ghan Ghan:
    Heard Houston got hit pretty bad by storms last night. Hope all is well with TH.
  • The Helper The Helper:
    Power back on finally - all is good here no damage
    +2
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    New recipe is another summer dessert Berry and Peach Cheesecake - https://www.thehelper.net/threads/recipe-berry-and-peach-cheesecake.194169/

      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