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
963
> 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.

      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