Jump to content

Recommended Posts

I have having issues with my custom character. Before I made a change every time I loaded up a world I would load in with max health (current values didn't save). I tried fixing it with code I learned from examples so its not the best. Anyways I need help saving and loading current health and hunger values, Thank you here is the code! Oh it no longer works because of specified line "hunger is not a value"

local function OnSave(inst, data)
     data.maxhealth = inst.components.health.maxhealth
  data.maxhunger = inst.components.hunger.max
  data.health = inst.components.health.current
  data.hunger = inst.components.hunger.current
end
local function OnLoad(inst, data)
    if data then
  if data.maxhealth then
            inst.components.health:SetMaxHealth(data.maxhealth)
         end
     if data.maxhunger then
            inst.components.hunger:SetMax(data.maxhunger)
         end
    end
end
local function onpreload(inst, data)
 if data.health and data.health.health then inst.components.health:SetCurrentHealth(data.health.health) end
    if data.hunger and data.hunger.hunger then inst.components.hunger.current = data.hunger.hunger end         ------------------THIS LINE!
end
Link to comment
Share on other sites

3 hours ago, SomeoneStrange said:

data.hunger = inst.components.hunger.current

You are storing a number value inside the data table with key "hunger".

local function onpreload(inst, data)
    if data then
        if data.health then
            inst.components.health:SetCurrentHealth(data.health)
        end
        if data.hunger then
            inst.components.hunger.current = data.hunger
        end
    end
end

With OnLoad and OnSave should be enough though.

 

function EntityScript:SetPersistData(data, newents)
    if self.OnPreLoad ~= nil then
        self:OnPreLoad(data, newents)
    end

    if data ~= nil then
        for k, v in pairs(data) do
            local cmp = self.components[k]
            if cmp ~= nil and cmp.OnLoad ~= nil then
                cmp:OnLoad(v, newents)
            end
        end
    end

    if self.OnLoad ~= nil then
        self:OnLoad(data, newents)
    end
end

FIRST, the entity runs its PreLoad method.

SECOND, the entity runs the OnLoad function of its components.

THIRD, the entity runs its OnLoad method.

Edited by DarkXero
Link to comment
Share on other sites

*THIRD, the entity runs its OnLoad method.

Anyway according to what you say you want to do, I do not see why you save the maxhealth, maxhunger, etc... These do not save the current values anyway so that won't help with your issue.

Plus the current values of these stats are handles by the OnSave function of each of the concerned components so I am not sure why it would not be reloaded?

Link to comment
Share on other sites

Alright now I have a probably with a line of code in the game itself.

scripts/components/hunger.lua

attempt to index local 'data' (a number value)

line 44

function Hunger:OnLoad(data)
    if data.hunger ~= nil and self.current ~= data.hunger then    ----this one
        self.current = data.hunger
        self:DoDelta(0)
    end
end

Edited by SomeoneStrange
Link to comment
Share on other sites

31 minutes ago, ZupaleX said:

*THIRD, the entity runs its OnLoad method.

Anyway according to what you say you want to do, I do not see why you save the maxhealth, maxhunger, etc... These do not save the current values anyway so that won't help with your issue.

Plus the current values of these stats are handles by the OnSave function of each of the concerned components so I am not sure why it would not be reloaded?

The max stat values is for the upgrading aspect of my character. Before hand every time the world was reloaded I was full on hunger and health

Link to comment
Share on other sites

8 minutes ago, SomeoneStrange said:

Alright now I have a probably with a line of code in the game itself.

You had a wrong OnSave and you ended up saving a table (which made data.hunger.hunger work).

Try generating a new world.

Link to comment
Share on other sites

2 minutes ago, SomeoneStrange said:

I was able to make a new world. And run it, sadly when I re-opened the world the same error occurred.

  data.health = inst.components.health.current
  data.hunger = inst.components.hunger.current

This causes the issue.

The component data gets saved as "data.hunger = table" for the hunger component. Then we were replacing that table for a number doing "data.hunger = value".

Try renaming your stuff from data.health to data.healthtosave and data.hungertosave so there are no name collisions.

Link to comment
Share on other sites

Alright hunger is working 100% correct. Now I am not getting any errors messages but the current health won't save so resuming the world heals me pretty much. This is my new set up, I have tried data.health and data.healthtosave, also I have tried in both onpreload and onLoad functions. 

 

local function onpreload(inst, data)
 if data.healthtosave then
        inst.components.health:SetCurrentHealth(data.healthtosave)
    end
end
 
local function OnSave(inst, data)
  data.maxhealth = inst.components.health.maxhealth
 data.maxhunger = inst.components.hunger.max
 data.healthtosave = inst.components.health.current
 data.hungertosave = inst.components.hunger.current
end 
  
local function OnLoad(inst, data)
 if data.maxhealth then
        inst.components.health:SetMaxHealth(data.maxhealth)
    end
 if data.maxhunger then
        inst.components.hunger:SetMax(data.maxhunger)
    end
    if data.hungertosave then
        inst.components.hunger.current = data.hungertosave
    end
end 

 

Edited by SomeoneStrange
Link to comment
Share on other sites

I misunderstood what you wanted to do actually. Now I see the issue.

When you call SetMax...(maxvalue) you set the maximum to maxvalue but you set the current value to that max value as well.

You need to do the same as what you did for the hunger. After setting the max health with SetMaxHealth, you need to reset the current helth with the value you saved in the OnSave

Link to comment
Share on other sites

4 minutes ago, SomeoneStrange said:

Now I am not getting any errors messages but the current health won't save so resuming the world heals me pretty much.

You are setting the current health first (preload), and updating your max health later (load).

There's a caveat when setting max health using that method, I'm sure you can spot it:

function Health:SetMaxHealth(amount)
    self.maxhealth = amount
    self.currenthealth = amount
    self:ForceUpdateHUD(true) --handles capping health at max with penalty
end

It also happens with hunger too.

Link to comment
Share on other sites

local function onpreload(inst, data)
    if data then
        if data.maxhealth then
            inst.components.health:SetMaxHealth(data.maxhealth)
        end
        if data.maxhunger then
            inst.components.hunger:SetMax(data.maxhunger)
        end
    end
end
 
local function OnSave(inst, data)
    data.maxhealth = inst.components.health.maxhealth
    data.maxhunger = inst.components.hunger.max
    data.healthtosave = inst.components.health.current
    data.hungertosave = inst.components.hunger.current
end 
  
local function OnLoad(inst, data)
    if data then
        if data.healthtosave then
            inst.components.health:SetCurrentHealth(data.healthtosave)
            inst.components.health:ForceUpdateHUD(true)
        end
        if data.hungertosave then
            inst.components.hunger.current = data.hungertosave
        end
    end
end 

 

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