How to find last used spell?

I am trying to make a spell that burns percent of mana of target, based on the last spell he cast. How to do it?

Comments

  • Posts: 1,670

    OnAbilityExecuted

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • edited January 2016 Posts: 188

    With lua modifier:

    if my_passive_lua_modifier == nil then
        my_passive_lua_modifier = class({})
    end
    
    function my_passive_lua_modifier:OnCreated()
        if IsServer() then
            self.units = {}
        end
    end
    
    function my_passive_lua_modifier:DeclareFunctions()
        local funcs = {
        MODIFIER_EVENT_ON_ABILITY_EXECUTED
        }
        return funcs
    end
    
    function my_passive_lua_modifier:OnAbilityExecuted(kv)
        if IsServer() then
            if kv.ability:IsItem() then return end
            if kv.unit:GetTeam() ~= self:GetParent():GetTeam() then
                self.units[kv.unit:entindex()] = kv.ability:GetAbilityIndex()
            end
        end
    end
    
    function my_passive_lua_modifier:RequestLastSpell(iIndex)
        if IsServer() then
            if self.units[iIndex] then
                return self.units[iIndex]
            else
                return -1
            end
        end
    end
    

    Records all enemy spell casts. Returns the index of the ability when you call RequestLastSpell with target's entity index.

    --assuming lua ability so 'self' refers to handle of the ability
    local hTarget = self:GetCursorTarget()
    local iAbility = self:GetCaster():FindModifierByName("my_passive_lua_modifier"):RequestLastSpell(hTarget:entindex())
    local hAbility = hTarget:GetAbilityByIndex(iAbility)
    local iManaCost = hAbility:GetManaCost(hAbility:GetLevel())
    
  • edited January 2016 Posts: 1,670

    @DrTeaSpoon I learned from @DoctorGester that if you put the IsServer() on top, your functions will only be ever called on the server. Example

    Also is the my_passive_lua_modifier == nil check even necessary?

    The concept of Modding Community doesn't go well together with Competitive Business
    My Project Page || My GitHub Profile ||

  • edited January 2016 Posts: 188
    1. Isn't that obvious? It's mostly there to make sure client-side doesn't try use functions not available for client side. If you don't need to render anything on client then it's better to use it than not. When you deal with modifiers that effect things in hud (damage? armor? stats?) then you should try to do work around so you don't need to use IsServer(). Best way to go about it is use of stacks as they are synchronized properly between clients and server unlike storing values with self.mycoolvalue. But by encasing everything you may run into problems with functions intended for clients like "IsHidden" as they have default values.
    2. To my understanding in the instances where two abilities try to link same modifier the nil check is there to keep errors from occurring. (or the cases where you have to heroes with same abilities).