System Key Triggers

Jesus4Lyf

Good Idea™
Reaction score
397
Key Triggers​
Version 1.1​

Requirements:
- None. This should even work without NewGen.

Overview:
Oh no, I didn't!
Yes, I did.
These 9 lines of code is a very lightweight system (such as CSdata or ABC) for attaching a single struct or integer to a trigger. However, it must be used with caution - it is not recommended for noobs. If you can thoroughly understand it, go ahead and use it appropriately.
JASS:
////////////////////////////////////
// Jesus4Lyf's Key Trigger System //
////////////////////////////////////

// Use with caution! Follow the below directions:
// - Have one structtype to one generic trigger template.
// - Have only up to about 40 triggers of a single template or else initialize on map start up, not mid game.
// - Only have structs of a trigger template structtype which will actually be attached to a trigger.
// - Don't use wait functions in your actions.
// - Put your trigger actions in the conditions.
// - Have them return false, so the "actions" never run.
// - Leave the "actions" blank.

function TriggerAttachStruct takes trigger t, integer s returns nothing
	call ResetTrigger(t)
	loop
		exitwhen s==0
		call TriggerExecute(t)
		set s=s-1
	endloop
endfunction
// The below is for the start of your actions (which are in the conditions).
// local structtype d=GetTriggerExecCount(GetTriggeringTrigger())

// If you don't like the above, use this function to get a trigger's attached struct:
function GetTriggerAttachedStruct takes trigger t returns integer
	return GetTriggerExecCount(t)
endfunction

Example:
JASS:
function TriggerAttachStruct takes trigger t, integer s returns nothing
	call ResetTrigger(t)
	loop
		exitwhen s==0
		call TriggerExecute(t)
		set s=s-1
	endloop
endfunction

struct structtype
	unit u // Whatever you want attached to the trigger.
endstruct

function TriggerActions takes nothing returns boolean
	local structtype d=GetTriggerExecCount(GetTriggeringTrigger())
	// Your Actions
	return false
endfunction

function WhateverIsMakingYourTrigger takes nothing returns nothing
	local structtype d=structtype.create()
	local trigger t=CreateTrigger()
	set d.u=null // Basically change null to some unit to attach the unit, etc.
	call TriggerAddCondition(t,Condition(function TriggerActions))
	call TriggerAttachStruct(t,d)
	// Add trigger event here.
	// Remember not to use TriggerAddAction. Actions are in the condition.
	set t=null
endfunction
// The above will work perfectly, regardless how many times the trigger is run.


About This System:
This system easily is the fastest for loading the attached struct. However, the more instances of a struct exist, the slower it is to save the struct to the trigger. Don't worry though, even if there is 500 you won't see any freeze in game at all. The problem is the numerous conditions about using the system. Because of the way it stores the struct, it is slow if there is more than 500 of the same struct at the same time, or if you try to store to a large batch of triggers (40+) at the same time. Your trigger actions must also be attached as a condition which returns false, meaning they also can't use wait functions (but if people want to use wait functions, I can design a work around for them). Below is a list of all the restrictions for this system
- Have one structtype to one generic trigger template.
- Have only up to about 40 triggers of a single template or else initialize on map start up, not mid game.
- Only have structs of a trigger template structtype which will actually be attached to a trigger.
- Don't use wait functions in your actions.
- Put your trigger actions in the conditions.
- Have them return false, so the "actions" never run.
- Leave the "actions" blank.

If you have any questions, feel free to ask here.

I'm well aware that this isn't noob friendly. However, I've found it very helpful myself so I considered it something worth posting for everyone to access.

Updates:
- Version 1.1: Added the ResetTrigger call, which removed one restriction and allows replacing the struct.
- Version 1.0: Release

Example map may be coming soon.
 

Rheias

New Helper (I got over 2000 posts)
Reaction score
232
Unique idea, although you should use ResetTrigger() in TriggerAttachStruct() in case you attach to the trigger a second struct after you are done with the first one. You do realize this will only work if the trigger will be executed once (and even then you need to add -1 in the GetTriggerAttachedStruct function).
 

0zaru

Learning vJASS ;)
Reaction score
60
One of your PROS will dissapear if What Rheias say is true, you will need a H2I function. And even this may end like a 0x10000(Enought 0?, never though about it :p) system.
 

Jesus4Lyf

Good Idea™
Reaction score
397
You do realize this will only work if the trigger will be executed once (and even then you need to add -1 in the GetTriggerAttachedStruct function).

Actually, this isn't true. Getting the Execute count gets the number of times the actions have been run. Getting the Evaluate count gets the number of times the conditions have been run. Since the conditions return false and contain the real actions, the number of times the blank actions have been run is constant. That means this will work solidly. Trust me, I've tested it. I should include an example and I probably will soon.
Does ResetTrigger just reset the counts? Or does it remove conditions or actions too.

0zaru: You lost me. Sorry. =X
 

Troll-Brain

You can change this now in User CP.
Reaction score
85
JASS:
function TriggerAttachStruct takes trigger t, integer s returns nothing
        local integer i = s - GetTriggerExecCount(t) // or why not a global
        if i < 0 then
           set i = s
           call ResetTrigger(t)
        endif
        loop
                exitwhen i==0
                call TriggerExecute(t)
                set i = i-1
        endloop
endfunction


Just an idea for speed freak, but i dunno if it does really matter, and ofc only in the case that you need to attach a different integer to the same trigger later.
 

Jesus4Lyf

Good Idea™
Reaction score
397
Yeah, that can reduce some complexity if you recycle triggers. But this kind of assumes you don't.

Actually, a nice thing you can do with this is load all values on init. Precache like TU and stuff. Then GetTriggerExecCount can be used like H2I, except stable. :cool:

Problem is event registration. Can work with GTrigger, though. :)
 
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