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.