- Joined
- Sep 26, 2009
- Messages
- 9,505
A handy approach to using spell effect responses. You can assign an event to fire for only a specific ability code, or when any ability is cast. API is intuitive and avoids creating tons and tons of handles to get the job done. This kind of thing has similar approaches by other people but this is definitely the most efficient way to do it.
Effectively, this is supposed to turn:
Into:
As you can see, a lot fewer lines of code. It saves TONS of efficiency on maps with a lot of spells in them. The two main reasons why are:
1. Does not generate 16 events per spell.
2. This uses one trigger evaluation instead of one for each individual spell (some maps have quite a few). This helps keep framerate high and fluid when a spell is cast.
Effectively, this is supposed to turn:
JASS:
private function onCast takes nothing returns boolean
if (GetSpellAbilityId() == 'A000') then
// Do actions
endif
return false
endfunction
private function onInit takes nothing returns nothing
local trigger t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddCondition(t, Condition(function onCast))
set t = null
endfunction
Into:
JASS:
private function onCast takes nothing returns nothing
// Do actions
endfunction
private function onInit takes nothing returns nothing
call RegisterSpellEffectEvent('A000', function onCast)
endfunction
As you can see, a lot fewer lines of code. It saves TONS of efficiency on maps with a lot of spells in them. The two main reasons why are:
1. Does not generate 16 events per spell.
2. This uses one trigger evaluation instead of one for each individual spell (some maps have quite a few). This helps keep framerate high and fluid when a spell is cast.
JASS:
//============================================================================
// SpellEffectEvent
// - Version 1.1.0.0
//
// API
// ---
// RegisterSpellEffectEvent(integer abil, code onCast)
//
// Requires
// --------
// RegisterPlayerUnitEvent: hiveworkshop.com/forums/showthread.php?t=203338
//
// Optional
// --------
// Table: hiveworkshop.com/forums/showthread.php?t=188084
//
library SpellEffectEvent requires RegisterPlayerUnitEvent, optional Table
//============================================================================
private module M
static if LIBRARY_Table then
static Table tb
else
static hashtable ht = InitHashtable()
endif
static method onCast takes nothing returns nothing
static if LIBRARY_Table then
call TriggerEvaluate(.tb.trigger[GetSpellAbilityId()])
else
call TriggerEvaluate(LoadTriggerHandle(.ht, 0, GetSpellAbilityId()))
endif
endmethod
private static method onInit takes nothing returns nothing
static if LIBRARY_Table then
set .tb = Table.create()
endif
call RegisterPlayerUnitEvent(EVENT_PLAYER_UNIT_SPELL_EFFECT, function thistype.onCast)
endmethod
endmodule
//============================================================================
private struct S extends array
implement M
endstruct
//============================================================================
function RegisterSpellEffectEvent takes integer abil, code onCast returns nothing
static if LIBRARY_Table then
if not S.tb.handle.has(abil) then
set S.tb.trigger[abil] = CreateTrigger()
endif
call TriggerAddCondition(S.tb.trigger[abil], Filter(onCast))
else
if not HaveSavedHandle(S.ht, 0, abil) then
call SaveTriggerHandle(S.ht, 0, abil, CreateTrigger())
endif
call TriggerAddCondition(LoadTriggerHandle(S.ht, 0, abil), Filter(onCast))
endif
endfunction
endlibrary
Lua:
--Lua RegisterSpellEffectEvent
do
local tb = {}
local trig = nil
function RegisterSpellEffectEvent(abil, onCast)
if not trig then
trig = CreateTrigger()
TriggerRegisterPlayerUnitEventBJ(trig, EVENT_PLAYER_UNIT_SPELL_EFFECT)
TriggerAddCondition(trig, Condition(function()
local i = tb[GetSpellAbilityId()]
if i then i() end
end))
end
tb[abil] = onCast
end
end
--syntax is:
--RegisterSpellEffectEvent(FourCC('Abil'), function() print(GetUnitName(GetTriggerUnit())) end)
Last edited: