Jump to content

Help with Upgrading Custom Stat


Recommended Posts

Hello, I need some help with making my stat upgradable.  I currently have it so that my stat is working and so I can upgrade health, hunger, and sanity, but if I do the same thing for my custom stat energy then it won't upgrade.

Here is the code I have for upgrading.

Spoiler

local LIGHT_MIN_HEALTH = 50
local LIGHT_MIN_HUNGER = 150
local LIGHT_MIN_SANITY = 50
local LIGHT_MIN_ENERGY = 100
local LIGHT_MAX_HEALTH = 100
local LIGHT_MAX_HUNGER = 300
local LIGHT_MAX_SANITY = 100
local LIGHT_MAX_ENERGY = 500

local function applyUpgrades(inst)
    inst:AddComponent("lightenergy")
    local max_upgrades = 10
    inst.level = math.min(inst.level, max_upgrades)

    local hunger_percent = inst.components.hunger:GetPercent()
    local health_percent = inst.components.health:GetPercent()
    local sanity_percent = inst.components.sanity:GetPercent()
    local energy_percent = inst.components.lightenergy:GetPercent()

    inst.components.hunger.max = math.ceil(LIGHT_MIN_HUNGER + inst.level * (LIGHT_MAX_HUNGER - LIGHT_MIN_HUNGER) / max_upgrades)
    inst.components.health.maxhealth = math.ceil(LIGHT_MIN_HEALTH + inst.level * (LIGHT_MAX_HEALTH - LIGHT_MIN_HEALTH) / max_upgrades)
    inst.components.sanity.max = math.ceil(LIGHT_MIN_SANITY + inst.level * (LIGHT_MAX_SANITY - LIGHT_MIN_SANITY) / max_upgrades)
    inst.components.lightenergy.max = math.ceil(LIGHT_MIN_ENERGY + inst.level * (LIGHT_MAX_ENERGY - LIGHT_MIN_ENERGY) / max_upgrades)

    inst.components.hunger:SetPercent(hunger_percent)
    inst.components.health:SetPercent(health_percent)
    inst.components.sanity:SetPercent(sanity_percent)
    inst.components.lightenergy:SetPercent(energy_percent)
end

-- Gain energy when eating food and upgrade when eating gears
local function oneat(inst, data)
    if data.food ~= nil and data.food.components.edible ~= nil and ENERGYVALUE[data.food.prefab] ~= nil then
        inst.components.lightenergy:DoDelta(ENERGYVALUE[data.food.prefab], false)
    end
    if data.food and data.food.components.edible and data.food.components.edible.foodtype == FOODTYPE.GEARS then
        --give an upgrade!
        inst.level = inst.level + 1
        applyUpgrades(inst) 
        inst.SoundEmitter:PlaySound("dontstarve/characters/wx78/levelup")
    end
end

I have tried many different methods to upgrade the max energy, but nothing seems to work.

Link to comment
Share on other sites

On 2/13/2016 at 0:32 AM, zUsername said:

Your code is messed up. You should clean up the code.
 

Light-DST.rar

Yea I was trying to add lots of things at once which made my code look really messy.  I was planning on cleaning it up once I fixed this issue.  What did you change to get the upgrades to work?  Also why did you comment out "light_none" in the prefabs?

Link to comment
Share on other sites

13 hours ago, zUsername said:

You can look in light.lua prefab. I just edited there. And "light_none" make me crash so I disabled it for testing.

 

Okay, I looked at it and noticed that everything was cleaned up, but I didn't notice any actual changes to the code.

Also whenever I exit the game and get back in all of the upgrades are reset.  Do you know how I can fix this?

Link to comment
Share on other sites

local function OnSave(inst, data)
   data.level = inst.level
end

local function OnPreload(inst, data)
   if data ~= nil and data.level ~= nil then
      inst.level = data.level
      applyUpgrades(inst)
   end
end

local function master_postinit(inst)
   inst.OnSave = OnSave
   inst.OnPreLoad = OnPreload
end

Don't know it work or not. Just try.

Link to comment
Share on other sites

Thanks.  It worked, although I don't understand why, because I already had that code in there.  I just replaced it.  I had the exact same code, with the exception that I had a few lines extra on the OnPreload function.  I think my laptop is just being screwy with me.

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