Jump to content

Recommended Posts

Hey I need some help with saving my character. The upgrade system works for my character but the problem is every time I reload the world the stats of my character resets. Anyone know how to code to save the max heath, hunger, and sanity of my character in-game. My character is below, Scruffy. Thank you.

scruffy.lua

You can use the OnLoad and OnSave methods for this. With OnSave, you can save variables into a table, which is then returned and stored outside the game. When the game starts, OnLoad is called with an extra data table parameter, which you can then check for entries. If it is the first time the game loads, then the data table will be empty. Otherwise it will be populated with the entries you set in OnSave before. You can use this to set the max hp, sanity, etc. back to what it was when the game was saved.

Thanks for that but what is the term I should use for it, I believe this is the right setup but it says it is invalid.

local function OnSave(inst, data)
  if inst.components.health:SetMaxHealth then
     data.components.health:SetMaxHealth = inst.components.health:SetMaxHealth
  end

local function OnLoad(inst, data)
   if data then
      if data.components.health:SetMaxHealth then
         inst.components.health:SetMaxHealth = data.components.health:SetMaxHealth
      end
   end 
end

So I know  inst and data need to be declared as the same but how do I declare the max health?

Edited by SomeoneStrange
16 hours ago, SomeoneStrange said:

Thanks for that but what is the term I should use for it, I believe this is the right setup but it says it is invalid.

local function OnSave(inst, data)
  if inst.components.health:SetMaxHealth then
     data.components.health:SetMaxHealth = inst.components.health:SetMaxHealth
  end

local function OnLoad(inst, data)
   if data then
      if data.components.health:SetMaxHealth then
         inst.components.health:SetMaxHealth = data.components.health:SetMaxHealth
      end
   end 
end

So I know  inst and data need to be declared as the same but how do I declare the max health?

Keep in mind the syntax of Lua. Using a colon is syntactic sugar and can only be used in a function call.

This

inst:DoSomething(...)

is equivalent to

inst.DoSomething(inst, ...)

You can't use it as a variable and assign it to something (or assign something to it).

Besides, what you are looking for is not to save the function SetMaxHealth, but the variable containing the max health. If you look at the health component, you see that it contains a variable maxhealth. You should use this instead. So your code becomes:

local function OnSave(inst, data)
     data.maxhealth = inst.components.health.maxhealth
end

local function OnLoad(inst, data)
   if data then
      if data.maxhealth then
         inst.components.health:SetMaxHealth(data.maxhealth)
      end
   end
end

Although there is a little unintended side-effect here, but I think it is more important that you understand the save and load process first.

7 minutes ago, SomeoneStrange said:

Is this little problem putting my character at max stats every time I reload the world?

 

Yes. Probably. Depends on the order of OnLoad calls (prefab vs health component), so I cannot say for sure. But it is easy enough to override.

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
×
  • Create New...