Creating Triggers in Structs

TheOverWhelm

Member
Reaction score
16
Just a quickie about Structs,
From what I've noticed, you can't do a TriggerAddAction with the added function being a nonstatic method?
 

luorax

Invasion in Duskwood
Reaction score
67
Yes, TriggerAddCondition/TriggerAddAction takes a static method. However you can always declare the instance you may wanna use in the current static method (local thistype this=XYZ)
 

Romek

Super Moderator
Reaction score
963
Code variables passed to TriggerAddAction need to take no parameters. Instance (non-static) methods compile to take an integer: the struct instance, 'this'.
 

TheOverWhelm

Member
Reaction score
16
So, are static methods in one instance? Or every one?
I'm just trying to make a very -simple- DPI thing without using any external systems.
 

dudeim

New Member
Reaction score
22
static methods are the same for every struct instance unlike normal methods which can change values and do stuff for their own instance.
 

TheOverWhelm

Member
Reaction score
16
So is it possible to create a trigger for each instance with a timer event, add actions to it that'll detect which instance created it?
 

luorax

Invasion in Duskwood
Reaction score
67
No, it's impossible. You can create triggers for each instance, but the Action must be static.
If you want to store those instances (and attach them to something) you better use Table or AIDS. You can find both of them in JASS resource section.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Since there's no way for the game to inherently know which instance to call the method with, the method must be static. If you want to create an individual trigger for each instance of the spell, you could use Hashtables to attach the individual instance to the respective trigger. I made a few functions that will simplify this:

JASS:
    globals        
        private key TRIGGERS
        private key DYMET
    endglobals
    
    function interface dymet takes integer this returns nothing
    
    private function TriggerAddMethodActionCB takes nothing returns nothing
        //Runs the given non-static method with the data attached to the trigger.
        call dymet(LoadInteger(hash,DYMET,GetHandleId(GetTriggeringTrigger()))).evaluate(LoadInteger(hash,TRIGGERS,GetHandleId(GetTriggeringTrigger())))
    endfunction
    
    function TriggerAddMethodAction takes integer data, trigger t, dymet which returns nothing
        /*
        
        Attaches data to a trigger, and when the trigger is executed, it runs the method with the data.
        Attachment to triggers (instead of events, which is probably impossible) limits attachment to one data set per trigger.
        
        Example Usage (pseudo code):
            trigger t
            Data data
            
            struct Data
                method onTriggerExecute takes nothing returns nothing
                    //Do stuff with 'this'
                endmethod
            endstruct
            
            call TriggerAddMethodAction(data,t,Data.onTriggerExecute)
        
        */
        
        call SaveInteger(hash,TRIGGERS,GetHandleId(t),data)
        call SaveInteger(hash,DYMET,GetHandleId(t),which)
        call TriggerAddAction(t,function TriggerAddMethodActionCB)
    endfunction


If you cut out the comments, you can see that it does the relatively simple action of attaching the individual this 'data' and method to the individual trigger, and then when the trigger runs, it merely executes the method that is attached, and passes the data that is also attached. It's a bit of a hack, but it makes for a nice interface. :)

Random note about the code: 'dymet' stands for dynamic method (attachment).
 

TheOverWhelm

Member
Reaction score
16
I've read about everything you did in thar function and I may try it.
My question is about doing a if GetExpiredTimer()==DPICounter(i).t
I'm guessing this'd be inefficient? And what about that whole linked thread stuff? Would that do anything? Whats the actual purpose of that, if anyone doesn't mind sharing :)
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
I didn't read post #6 when you created it. It may be simpler to do a timer attachment (I have a similar method, if you'd like me to share). If you are insistent upon triggers, you could also attach a timer event to the trigger you pass to that function.
 

TheOverWhelm

Member
Reaction score
16
Timer Attachment? Whats the idea behind it?
I can attach a timerevent (if I understand you correctly), currently I'm able to do the
[t being the trigger and this.t being the timer]

On a sidenote, how does one do a single line of JASS like in those tutorials? The [] code part
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Timer Attachment? Whats the idea behind it?
I can attach a timerevent (if I understand you correctly), currently I'm able to do the
[t being the trigger and this.t being the timer]

That's a timer event, there's also the [ljass]TimerStart[/ljass] function (note that you can click that for a link to the function to see its arguments and return value), which allows you to pass a function to be called upon the timer's expiration. Timer attachment is essentially attaching a value, such as a struct instance, to the timer, so that when the timer expires, you have specific data attached to that timer. There are also timer systems, which use efficient algorithms to reduce the overhead of having multiple timers, which is common in maps with triggered custom spells.

On a sidenote, how does one do a single line of JASS like in those tutorials? The [] code part

You can use the [noparse][ljass][/ljass][/noparse] tags to make an inline jass tag (and you can display tags using the [noparse][noparse][/noparse][/noparse] tags.
 

TheOverWhelm

Member
Reaction score
16
Ahhh, I see. That's a neat thing to know for the future!
But I don't believe I can pass the instance number to that without have a loop to check which instance's timer expired..
Unless I use the timer systems available? Isn't that their purpose? Or is it for overhead?
Sorreh, I'm a bit newby to the vJASS community
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      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