- Joined
- Feb 15, 2006
- Messages
- 851
A library to reduce the time writing the same shit. I took some code ideas from a similar resource. approved here. This one supports all the possibilities in calling the trigger. If user defines the condition and action functions this function will return the respective trigger, so it can be controlled by other instance (just in case).
If the user only sets a condition or action, then it will return
Usage:
Actual code
Examples:
Condition only
Action Code Only
Condition and action separated
If the user only sets a condition or action, then it will return
null
because this trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.Usage:
- For functions without triggersleepaction or waits, it's recommended to use
call RegisterAnyUnitEvent(<Event>, code Function, null)
- For functions with triggersleepaction or waits, it's recommended to use
call RegisterAnyUnitEvent(<Event>, null, code Function)
- For triggers which condition and action codes must be separated (it could happen) you set this:
JASS:
private function Conditions takes nothing returns boolean return GetSpellAbilityId() == 'A000' endfunction private function Actions takes nothing returns nothing // your stuff... endfunction private function init takes nothing returns nothing local trigger t = RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions) endfunction
Actual code
JASS:
//************************************************************************
// RegisterAnyUnitEvent by moyack
//************************************************************************
// Code improved with the idea developed by Magtheridon96 , Bribe, azlier
//************************************************************************
/*
A library to reduce the time writing the same shit. I took some code ideas from a similar
resource approved here. This one supports all the possibilities in calling the trigger.
If user defines the condition and action functions this function will return the respective
trigger, so it can be controlled by other instance (just in case).
If the user only sets a condition or action, then it will return null because this
trigger handles a lot of conditions and actions and it shouldn't be accessible by the user.
* For functions without triggersleepaction or waits, it's recommended to use:
call RegisterAnyUnitEvent(<Event>, code Function, null)
* For functions with triggersleepaction or waits, it's recommended to use:
call RegisterAnyUnitEvent(<Event>, null, code Function)
*/
library RegisterAnyUnitEvent
globals
private trigger array ts
endglobals
function RegisterAnyUnitEvent takes playerunitevent e, code cond, code act returns nothing
local integer i = GetHandleId(e)
local trigger t
if act != null then
set t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, e)
if cond != null then
call TriggerAddCondition(t, Filter(cond))
endif
call TriggerAddAction(t, act)
set t = null
else
if ts[i] == null then
set ts[i] = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(ts[i], e)
endif
call TriggerAddCondition(ts[i], Filter(cond))
endif
endfunction
endlibrary
Examples:
Condition only
JASS:
scope test initializer init
private function Conditions takes nothing returns boolean
// your code... please DO NOT use waits or triggersleeaction...
return false // please always set it to false
endfunction
private function init takes nothing returns nothing
// that's the recommended way, it's faster and nice :)
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, null)
endfunction
endscope
Action Code Only
JASS:
scope test initializer init
private function Actions takes nothing returns nothing
// your stuff...
endfunction
private function init takes nothing returns nothing
// It's valid but not as faster as using conditions. Use it when you have in the code
// Polledwait(), triggersleepaction() or wait()
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, null, function Actions)
endfunction
endscope
Condition and action separated
JASS:
scope test initializer init
private function Conditions takes nothing returns boolean
return GetSpellAbilityId() == 'A000'
endfunction
private function Actions takes nothing returns nothing
// your stuff...
endfunction
private function init takes nothing returns nothing
call RegisterAnyUnitEvent(EVENT_PLAYER_UNIT_SPELL_CAST, function Conditions, function Actions)
endfunction
endscope
Last edited: