Initializing spell triggers

Oli

Oli

Level 3
Joined
Aug 9, 2015
Messages
33
Hi,

I have been wondering whether in terms of optimilization it is better to have one function that checks what spell is being used and calls the appropiate function, or whether it is better to have many functions checking for which spells are being used.

In the example below, which is in lua but I believe for the sake of the question it does not matter, I present two scripts (with errors) that would be a graphical interpretation of what I'm talking about.

Lua:
do
    function rocket_init()
        if GetSpellAbilityId() == 'blah' then
            blah blah blah
        end
    end

    OnMapInit(function ()
        local rocketinit = CreateTrigger()
        TriggerAddAction(rocketinit, rocket_init)
        TriggerRegisterAnyUnitEventBJ(rocketinit, EVENT_PLAYER_UNIT_SPELL_CAST)
    end)
end

do
    function spells_init()
        if GetSpellAbilityId() == 'blah2' then
            blah blah blah
        end
        if GetSpellAbilityId() == 'blah3' then
            blah blah blah
        end
        if GetSpellAbilityId() == 'blah4' then
            blah blah blah
        end

        OnMapInit(function ()
            local spellsinit = CreateTrigger()
            TriggerAddAction(spellsinit, spells_init)
            TriggerRegisterAnyUnitEventBJ(spellsinit, EVENT_PLAYER_UNIT_SPELL_CAST)
        end)
end

In the first script each spell has it's own trigger, for the function that initializes actions checks only for one spell. In the second script all spells "belong" to one trigger, as the function initialized by the trigger checks for all spells in the map, and it would call appropiate other functions.
 

Uncle

Warcraft Moderator
Level 73
Joined
Aug 10, 2018
Messages
7,866
The beauty of Lua is it's tables and how they can use any type of [index] and store any type of value:
Lua:
-- [SPELL SYSTEM] --
do
    Spell_Table = {}

    OnMapInit(function()
        local t = CreateTrigger()
        TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        TriggerAddAction(t, OnSpellCast)
    end)

    function OnSpellCast()
        local abilId = GetSpellAbilityId()
        Spell_Table[abilId]()
    end

    function RegisterSpell(abilId, func)
        Spell_Table[FourCC(abilId)] = func
    end
end

-- [PALADIN] --
do
    OnMapInit(function()
        RegisterSpell("AHds", DivineShield)
        RegisterSpell("AHhb", HolyLight)
        -- Register more as needed
    end)

    function DivineShield()
        local u = GetTriggerUnit()
        SetUnitInvulnerable(u, true)
    end

    function HolyLight()
        local t = GetSpellTargetUnit()
        SetWidgetLife(t, GetWidgetLife(t) + 200.00)
    end
end
This allows you to have one central "on cast" trigger which utilizes the id of the cast spell to run an associated function. This is done by calling the RegisterSpell function and providing the id of your ability as well as the function that you want to run when it's cast.

Note that it may be necessary to set and use your own variables instead of the standard Event Responses. That's because some Event Responses are lost when calling another function from OnSpellCast. You could probably fix this issue while also simplifying the entire spell-making process with something like this for example, where Spell is a table that has data populated based on the ability cast:
Lua:
    function HolyLight()
        local healAmount = (200.00 * Spell.Level)
        SetWidgetLife(Spell.Target, GetWidgetLife(Spell.Target) + healAmount)
    end

Edit: Also, make sure to this Event instead:
Lua:
EVENT_PLAYER_UNIT_SPELL_EFFECT
That's when it actually executes it's effects.
 
Last edited:
Level 45
Joined
Feb 27, 2007
Messages
5,578
I have been wondering whether in terms of optimilization it is better to have one function that checks what spell is being used and calls the appropiate function, or whether it is better to have many functions checking for which spells are being used.
I understand perhaps you're asking the question just to ask it but: the difference will never ever matter or be noticeable to you in any map you will ever make or play. In order to use enough abilities at once to encounter a difference, such a map would have extreme issues with the number of units, pathfinding, and generally issuing orders long before cast trigger efficiency would come up.
 
Top