Snippet TrigWrap

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
JASS:

////////////////////////////////////////
//      ~TrigWrap v1.0.1~
//          by kingking
//  
//  TrigWrap is a trigger wrapper
//  that enables trigger recycling.
//
//  Functions :
//  call Trigger.create() -> Trigger
//  call Trigger.getTriggering() -> Trigger
//  set <vars>.event = whichEvent
//  call <vars>.unregister()
//  call <vars>.destroy()
//  set <vars>.data = integer
//  set <vars>.condition = conditionfunc
//  set <vars>.action = code
//  set <vars>.enable = true/false
//  <vars>.data -> integer
//  <vars>.trig -> trigger
//
//  Precautions :
//  Do not destroy the trigger manually,
//  use .destroy() instead.
//
//  Requires : Jasshelper 0.A.2.9 or newer, Event
/////////////////////////////////////
library TrigWrap requires Event
    
    struct Trigger
        private static hashtable hasht
        trigger trig
        EventReg eventReg
        Event registeredEvent
        
        static method create takes nothing returns thistype
            local thistype this = thistype.allocate()
            if this.trig == null then
                set this.trig = CreateTrigger()
                call SaveInteger(thistype.hasht,GetHandleId(this.trig),0,this)
            else
                call EnableTrigger(this.trig)
            endif
            return this
        endmethod
        
        static method getTriggering takes nothing returns thistype
            return LoadInteger(thistype.hasht,GetHandleId(GetTriggeringTrigger()),0)
        endmethod
        
        method operator condition= takes conditionfunc func returns nothing
            call TriggerAddCondition(this.trig,func)
        endmethod
        
        method operator action= takes code func returns nothing
            call TriggerAddAction(this.trig,func)
        endmethod
        
        method operator data= takes integer data returns nothing
            if this.eventReg != 0 then
                set this.eventReg.data = data
            endif
        endmethod
        
        method operator data takes nothing returns integer
            return this.eventReg.data
        endmethod
        
        method operator enable= takes boolean flag returns nothing
            if flag then
                call EnableTrigger(this.trig)
            else
                call DisableTrigger(this.trig)
            endif
        endmethod
        
        method operator event= takes Event whichEvent returns nothing
            debug if this.registeredEvent > 0 then
            debug   call BJDebugMsg("TrigWrap error : Trying to register trigger to more than 1 events")
            debug endif
            set this.registeredEvent = whichEvent
            set this.eventReg = whichEvent.register(this.trig)
        endmethod
        
        method unregister takes nothing returns nothing
            call this.registeredEvent.unregister(this.trig)
            set this.eventReg = 0
        endmethod
        
        method destroy takes nothing returns nothing
            if this.registeredEvent > 0 then
                call this.registeredEvent.unregister(this.trig)
            endif
            call DisableTrigger(this.trig)
            call TriggerClearConditions(this.trig)
            call TriggerClearActions(this.trig)
            set this.eventReg = 0
            set this.registeredEvent = 0
        endmethod
        
        static method onInit takes nothing returns nothing
            set thistype.hasht = InitHashtable()
        endmethod
    endstruct
    
endlibrary
 

Lyerae

I keep popping up on this site from time to time.
Reaction score
105
[ljass]// Cons : You only can add 1 condition and 1 action for trigger.[/ljass]
Not a fan of that line.

How 'bout a demo script, to show us how it's used, exactly.
 

Sevion

The DIY Ninja
Reaction score
413
@Lyerae

Ditz. I don't really like that you can only add 1 condition and 1 action.

I don't really see how this could be useful considering you can only add one action and condition.

Really, all this is doing is adding the ability to attach data to a trigger and confining you to one action and one condition?

I could easily replicate this with a hashtable and not confine myself to that.

A simple hashtable wrapper is all this is.

Besides, you can easily remove that "con".

Also, [ljass]struct Trigger extends array[/ljass] ;)
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Huh? I think my info is leading people to misunderstand what is it. I will add example.

Example added.
 

Narks

Vastly intelligent whale-like being from the stars
Reaction score
90
I like how you use a "z" instead of a "s".
 

Sevion

The DIY Ninja
Reaction score
413
The example only furthers my reasonings.

The only purpse of this is to allow data attachment to triggers and limit maximum actions and conditions.

You can easily replicate this with a hashtable.
 

Executor

I see you
Reaction score
57
The example only furthers my reasonings.

The only purpse of this is to allow data attachment to triggers and limit maximum actions and conditions.

You can easily replicate this with a hashtable.

Agreed. I really don't see the sense of using this lib. Furthermore sometimes attachment by executing could be faster (depending on the usage).
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Furthermore sometimes attachment by executing could be faster (depending on the usage).
Then I need to sacrify the trigger's action. It is not worth.
 

Executor

I see you
Reaction score
57
Yes. It wasn't a suggestion to include in your sys, I just pointed out that in some cases triggers with only a condition and a exec attachment make more sense.

BTW: As far as I know, using the condition to do everything related to event reaction makes more sense, because it is faster (?).
 

Romek

Super Moderator
Reaction score
964
> Hmm, "s" means it have more than 1 trigger. lol.
Adding 'z' is just a corny alternative to 's'.
They're pronounced almost the same too.

It seems that you're using method operators for the lulz:
JASS:
.
        method operator data= takes integer whichData returns nothing
            set this.dataz = whichData
        endmethod
        
        method operator trigger takes nothing returns trigger
            return this.triggerz//Please use method in this library to add condition and action, thanks.
        endmethod

        method getData takes nothing returns integer
            return this.dataz
        endmethod

Could all be removed.

JASS:
.
        static method create takes nothing returns thistype
            set thistype.d = thistype.allocate()
            set thistype.d.triggerz = CreateTrigger()
            set thistype.d.triggerzId = GetHandleId(thistype.d.triggerz)
            call SaveInteger(thistype.ht,thistype.d.triggerzId,0,thistype.d)
            return thistype.d
        endmethod

Do locals offend you?
 

weaaddar

New Member
Reaction score
6
On an aside, you must also destroy the boolexpr sent to a condition or else it will leak.

So really you should take the trigger, a boolexpr, and the action. The event will leak ~400bytes last I heard.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Lol, destroying it is not stable. If I understand correctly (and I'm fairly sure I do) if you use the same function as a condition for two triggers, then destroying the boolexpr will cause [LJASS]Condition[/LJASS] or [LJASS]Filter[/LJASS] generated boolexprs which have been used in multiple triggers to all fail in those other triggers.

If the boolexprs were created with [LJASS]And[/LJASS], [LJASS]Or[/LJASS] or [LJASS]Not[/LJASS] then they need to be destroyed, are unique, and will leak, but I should think that is the user's job.

I don't see why we need a wrapper for this - it just kind of confuses and restricts and adds a hashtable wrap...

PS. the coolest type of trigger attachment still has to be Azlier's - typecast int to bool and store it inside [LJASS]IsTriggerWaitOnSleeps[/LJASS]. No cleanup! :thup:

So to be blunt... what does this do that ABC can't?
 

Deaod

Member
Reaction score
6
I would never approve of anything that calls DestroyTrigger. Also...this looks a little bit pointless to me.
 

kingkingyyk3

Visitor (Welcome to the Jungle, Baby!)
Reaction score
216
Updated. Hmm.. Anyone here does register a trigger more than 1 event? If so, I will add support to it. In this stage, I feel it is very limited because Event in systems are privatized, the authors made some TriggerRegister--Event functions for registration, so I can't access the event and store it.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Ghan Ghan:
    Howdy
  • 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
    +1
  • V-SNES V-SNES:
    Happy Friday!
    +1

      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