selecting a unit affected by this modifier crashes game

this modifier is part of an ability im making to turn heroes into couriers and couriers into heros, kek.

i dont see a problem with this code. it does what i want; setting their hp/mana/armor to be the same as a walking courier.

but for whatever reason ^title. any idea why?

modifier_couriers_reign_courier_stats = class({})

function modifier_couriers_reign_courier_stats:IsHidden()
    return true
end

function modifier_couriers_reign_courier_stats:IsPurgable()
    return false
end

function modifier_couriers_reign_courier_stats:DeclareFunctions()
    local funcs = {
        MODIFIER_PROPERTY_HEALTH_BONUS,
        MODIFIER_PROPERTY_MANA_BONUS,
        MODIFIER_PROPERTY_PHYSICAL_ARMOR_BONUS,
    }
    return funcs
end

function modifier_couriers_reign_courier_stats:GetModifierHealthBonus()
    local targetHealth = self:GetParent():GetMaxHealth()
    return -targetHealth + 75
end

function modifier_couriers_reign_courier_stats:GetModifierManaBonus()
    local targetMana = self:GetParent():GetMaxMana()
    return -targetMana
end

function modifier_couriers_reign_courier_stats:GetModifierPhysicalArmorBonus()
    local targetArmor = self:GetParent():GetPhysicalArmorValue()
    return -targetArmor
end

Comments

  • Posts: 273

    I don't know if GetModifierHealthBonus() and GetModifierManaBonus() work with negative values. Also, not really relevant to the crash but:

    function modifier_couriers_reign_courier_stats:GetModifierHealthBonus()
       if IsServer()
          local targetHealth = self:GetParent():GetMaxHealth()
          return -targetHealth + 75
       end
    end
    

    GetMaxHealth() is not available on the client side.

  • I can't help but imagine that this wouldn't go well even if it didn't crash. What'd happen would be the health bonus is applied as 75 minus the current max health, which would end up as 75 on the first tic ... but then, on the next tic, the function sets itself to the 75 minus the current max health, which is 0, which leads the standard max health.

    I wouldn't be surprised if it's just unstable to refer to the stat within its own modifier property function.

    Also, Darkonius, you generally don't want to shove stat-based modifier properties into IsServer(), since the client needs to get them to update the HUD. Might not be the case with health and mana, but still.

  • Posts: 273

    @Arctangent

    I know that. But the client console will spam for errors, because GetMaxHealth() is not available on the client side. See for yourself: Link or with cl_script_help2 in vconsole.

    @DankBud

    Try doing this:

    function modifier_couriers_reign_courier_stats:OnCreated( kv )
       if IsServer() then
          self.health_reduction = 75 - self:GetParent():GetMaxHealth()
       end
       self.mana_reduction = 0 - self:GetParent():GetMaxMana()
       self.armor_reduction = 0 - self:GetParent():GetPhysicalArmorValue()
    end
    
    function modifier_couriers_reign_courier_stats:GetModifierHealthBonus()
       if self.health_reduction then
          return  self.health_reduction
       end
    end
    
    function modifier_couriers_reign_courier_stats:GetModifierManaBonus()
       return self.mana_reduction
    end
    
    function modifier_couriers_reign_courier_stats:GetModifierPhysicalArmorBonus()
       return self.armor_reduction
    end
    
  • Posts: 182

    @Darkonius

    that seems to have fixed it. thanks a lot!