Jump to content

Need help nerfing my healing item, make the effect non-stackable


Recommended Posts

My character can craft band-aids that heal for 20 hp over 10 seconds. The problem is that a friend testing the character for me discovered he could use several instances of the healing item to stack the heal over time and facetank bosses with impunity. This clearly was not intended. 

This is the code for the healer component that the item calls for:

 

local Healer = Class(function(self, inst)
    self.inst = inst
    self.health = TUNING.HEALING_SMALL
    self.time = 0
end)

function Healer:SetHealthAmount(health_per_tick, t_time, timespan)
    self.health = health_per_tick
    if t_time then
        self.time = t_time
    else
        self.time = 0
    end
    if timespan then
        self.timespan = timespan
    else
        self.timespan = 1
    end
end

function Healer:Heal(target)
    if target.components.health ~= nil then
        Healing_Task(self.time, self.timespan, target, self.health, self.inst.prefab, self.GUID)
        if self.inst.components.stackable ~= nil and self.inst.components.stackable:IsStack() then
            self.inst.components.stackable:Get():Remove()
        else
            self.inst:Remove()
        end
        return true
    end
end

function Healing_Task(ticks_left, timespan, target, amount, prefab, id)
    if target.components.health ~= nil then
        target.components.health:DoDelta(amount,false,prefab)
        --print("Ticksleft: " .. ticks_left .. "  timespan: " .. timespan)
        if ticks_left > 1 then
            scheduler:ExecuteInTime(timespan, 
                function()
                    Healing_Task(ticks_left - 1, timespan, target, amount, prefab, id)
                end,
                id)
        end
    end
end
return Healer

 

I am terrible at LUA, this code is pretty much adapted from a similar healing item i saw on another mod. Could someone help me modify it?

I would like to make it so reapplying the healing item before the timer runs out doesnt add a new instance of healing, but merely extends the duration of the current heal with a new count from when its reapplied.

 

hopefully these would be enough to make the item more fair. (I also intend to change it so it heals 20 over 20 seconds instead of 10 so it's more useful as aftercare rather than in-battle healing)

 

Edited by Proc
Link to comment
Share on other sites

I think you should try using DoPeriodicTask instead of ExecuteInTime. You would then need to save this task and reset it every time the player uses your item.

It would look something like this:

function Healing_Task(ticks_left, timespan, target, amount, prefab, id)
  if target.components.health then
    --print("Ticksleft: " .. ticks_left .. "  timespan: " .. timespan)
    
    if self.healing_task then
      self.healing_task:Cancel()
      self.healing_task = nil
    end
    
    if ticks_left >= 1 then
      self.healing_task = target:DoPeriodicTask(timespan, function()
        target.components.health:DoDelta(amount, false, prefab)
        ticks_left = ticks_left - 1

        if ticks_left <= 0 then
          self.healing_task:Cancel()
          self.healing_task = nil
        end
      end)
    end
  end
end

 

One thing you should keep in mind is that this task is not saved between sessions so whenever you leave and re-enter the server or enter/exit caves it will not resume if it was running before. You would need to use OnSave and OnLoad for that.

Link to comment
Share on other sites

I tried replacing the Healing_Task function of my code with the one you provided, but it seems that using self.healing_task is a problem for some reason. This is the error that the game showed me.

darnalueaerror1.png

Link to comment
Share on other sites

Ah, I see. You need to make the Healing_Task function into a method of Healer. So just change this:

function Healer:Healing_Task(ticks_left, timespan, target, amount, prefab, id)
  --
  --

 

Link to comment
Share on other sites

Well, that solved the "self" variable problem. But now the code takes issue with the "Healing_Task" variable in an earlier part of the code, inside the function Healer:Heal(target). 

I figure this is because Healing_Task is no longer a function and cannot be called in the same way anymore. But I don't know how to make function Healer:Heal(target) work with the new function Healer:Healing_Task

 

darnalueaerror2.png

Link to comment
Share on other sites

You should change it to:

self:Healing_Task(self.time, self.timespan, target, self.health, self.inst.prefab, self.GUID)

since it's now a method of the Healer component (self in this case).

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
  • Create New...