Jump to content

Need help with energy stat


Recommended Posts

Hi, I'm currently trying to make a new character that has a new stat called energy.  This stat is like hunger in that it slowly drains and has to be regained by eating/sleeping and if it runs out you lose health.  I can't seem to figure out how to get this to work.  I realize that I need a widget to show the stat and I need a new component for the stat, but I can't seem to figure out how to get it to work.  I've looked everywhere and no one has any tutorials on how to do this that I could find.

This is what I have for my component.

Spoiler

local function oncurrent(self, current)
    if self.inst.player_classified ~= nil then
        assert(current >= 0 and current <= 255, "Player currentlightenergy out of range: "..tostring(current))
        self.inst.player_classified.currentlightenergy:set(math.ceil(current))
    end
end

local LightEnergy = Class(function(self, inst)
    self.inst = inst

    --Recommended to explicitly add tag to prefab pristine state
    inst:AddTag("lightenergy")

    self.max = 100
    self.current = self.max
    self._old = self.current
    self.rate = -0.1
    self.sleeprate = 1
    if inst.player_classified ~= nil then
        makereadonly(self, "max")
    end
end,
nil,
{
    current = oncurrent,
})

local function OnTimeEffectTick(inst, self, delta, dt)
    self:DoDelta(delta * self.time_effect_multiplier, true)
    --Energy hitting 0 does starving damage
    if self.current <= 0 then
        inst.components.health:DoDelta(-inst.components.hunger.hurtrate * dt, false, "hunger")
    end
    if inst:HasTag("sleeping") then
        self:DoDelta(self.sleeprate*dt, true)
    else
        self:DoDelta(self.rate*dt, true)
    end
end

function LightEnergy:StartTimeEffect(dt)

    if self.task ~= nil then
        return
    end
    --print("Starting energy time effect")
    self.task = self.inst:DoPeriodicTask(dt, OnTimeEffectTick, nil, self, dt)
end

function LightEnergy:StopTimeEffect()
    if self.task ~= nil then
        self.task:Cancel()
        self.task = nil
    end
end


function LightEnergy:DoDelta(delta, overtime)
    local old = self._old
    self.current = math.clamp(self.current + delta, 0, self.max)
    self._old = self.current

    self.inst:PushEvent("lightenergydelta", { oldpercent = old / self.max, newpercent = self.current / self.max, overtime = overtime })

end

function LightEnergy:GetPercent()
    return self.current / self.max
end

function LightEnergy:SetPercent(percent, overtime)
    self.current = self.max * percent
    self:DoDelta(0, overtime)
end

function LightEnergy:OnSave()
    return 
    {
        current = self.current,
    }
end

function LightEnergy:OnLoad(data)
    if data ~= nil and data.current ~= nil and data.current ~= self.current then
        self.current = data.current
        self:DoDelta(0, true)
    end
end

function LightEnergy:GetDebugString()
    return string.format("%2.2f / %2.2f", self.current, self.max)
end

return LightEnergy

Please help.

Link to comment
Share on other sites

2 hours ago, Mysticwolf6 said:

I've looked everywhere and no one has any tutorials on how to do this that I could find.

The reason why there isn't a tutorial on this subject is because of its complexity. Not only do you have to create the component, replica, and widget you also have to modify the character prefab, item prefabs and player actions/monster prefabs(if you want them to drain energy).

Link to comment
Share on other sites

On 2/5/2016 at 7:16 PM, Kzisor said:

The reason why there isn't a tutorial on this subject is because of its complexity. Not only do you have to create the component, replica, and widget you also have to modify the character prefab, item prefabs and player actions/monster prefabs(if you want them to drain energy).

Yeah I am aware that it's more complex, but I would still like to add this feature to my character.  I mostly just want it to drain over time.  I assume you wouldn't have to mess with monster prefabs to do that.  Could you help me figure out how to do this?  I've already looked at code from woodie and from other mods.  I just can't seem to get it to work for my mod no matter what I do.

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...