- Joined
- Nov 25, 2008
- Messages
- 1,309
JASS:
//RegisteredSpell_Add
// Parameters:
// integer Id - The spell id to bind the function to.
// code F - The function to be bound to a spell.
// Returns:
// nothing.
//
// Adds a spell, that when cast, gets called via TriggerEvaluate by the handler trigger.
// Note: This can be called before or after RegisteredSpell_Init.
function RegisteredSpell_Add takes integer Id,code F returns nothing
local trigger T=CreateTrigger()
call TriggerAddCondition(T,Filter(F))
call SaveTriggerHandle(GLOBAL_HASHTABLE,0,-Id,T)
set T=null
endfunction
//RegisteredSpell_AddEx
// Parameters:
// integer Id - The spell id to bind the function to.
// filterfunc F - The function(s) to be bound to a spell.
// Returns:
// nothing.
//
// Same as RegisteredSpell_Add, expect that by directly passing a filterfunc you can register multiple functions to the same spell.
// For example: call RegisteredSpell_AddEx('A000',Or(function SpellA,function SpellB))
// Note: This can be called before or after RegisteredSpell_Init.
function RegisteredSpell_AddEx takes integer Id,boolexpr Fs returns nothing
local trigger T=CreateTrigger()
call TriggerAddCondition(T,Fs)
call SaveTriggerHandle(GLOBAL_HASHTABLE,0,-Id,T)
set T=null
endfunction
//RegisteredSpell_Cast
// Parameters:
// nothing.
// Returns:
// nothing.
//
// This is the handler trigger. It's job is to call the registered function to the casted spell, if there is one.
function RegisteredSpell_Cast takes nothing returns nothing
local trigger T=LoadTriggerHandle(GLOBAL_HASHTABLE,0,-GetSpellAbilityId())
if T!=null then
call TriggerEvaluate(T)
set T=null
endif
endfunction
//RegisteredSpell_Init
// Parameters:
// nothing.
// Returns:
// nothing.
//
// This function, when called, initializes the handler trigger.
// Note: This can be called before or after RegisteredSpell_Add or RegisteredSpell_AddEx.
function RegisteredSpell_Init takes nothing returns nothing
local trigger T=CreateTrigger()
call TriggerAddCondition(T,Filter(function RegisteredSpell_Cast))
call TriggerRegisterAnyUnitEventBJ(T,EVENT_PLAYER_UNIT_SPELL_EFFECT)
set T=null
endfunction
RegisteredSpell_Init()
and the trigger registered to to the spell. This saves having the massive if-then-elseif-else branches as well as removing the need for many triggers listening for the same event.Steps for importing:
- Copy and paste into map header, or anywhere above your initialization trigger.
- Call
RegisteredSpell_Init()
first to initialize the handler trigger (this doesn't have to be called first). - Call
RegisteredSpell_Add()
orRegisteredSpell_AddEx()
for all abilities you want triggered through this handler (again, you can call this before you initialize the handler trigger). - Cast some spells.
GLOBAL_HASHTABLE
needs to be replaced with a hashtable that you use.- Index of zero can be replaced with any constant that doesn't conflict with your hashtable.
EVENT_PLAYER_UNIT_SPELL_EFFECT
can be replaced with the spell event that you want, incase you want to trigger it at SPELL_CAST instead or something.
- I use the negative value of the spell id to lessen the chances of it conflicting with index 0 and other entries to the index.
- I use the evil, red Blizzard JASS function
TriggerRegisterAnyUnitEventBJ
because there is no sense to inline it. Feel free to change it/inline the entire initialization function to suit your map. - To have two or more spells bound to the same function (or functions) you can copy the trigger over with
call SaveTriggerHandle(GLOBAL_HASHTABLE,0,-NEW_SPELL_ID,LoadTriggerHandle(GLOBAL_HASHTABLE,0,-OLD_SPELL_ID))
where OLD_SPELL_ID is the already (first) registered spell for the function(s) and NEW_SPELL_ID is the second (or later) spell to also register to the function(s). - Yes, I know it is not vJASS, but it doesn't need to be either. If you are so inclined to only use vJASS then just wrap all the code in
library SpellHandler /* ... */ endlibrary
Could even properly create the initialization function.