SomeoneStrange Posted August 11, 2016 Share Posted August 11, 2016 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 Link to comment Share on other sites More sharing options...
Joachim Posted August 11, 2016 Share Posted August 11, 2016 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. Link to comment Share on other sites More sharing options...
SomeoneStrange Posted August 11, 2016 Author Share Posted August 11, 2016 (edited) 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 August 11, 2016 by SomeoneStrange Link to comment Share on other sites More sharing options...
Joachim Posted August 12, 2016 Share Posted August 12, 2016 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. Link to comment Share on other sites More sharing options...
SomeoneStrange Posted August 12, 2016 Author Share Posted August 12, 2016 Thank you so much for the help and everything! Link to comment Share on other sites More sharing options...
Joachim Posted August 12, 2016 Share Posted August 12, 2016 No problem. There is probably a little problem with it, but I think you will be able to solve it yourself. It has to do with currenthealth. If not, then you're in luck. Link to comment Share on other sites More sharing options...
SomeoneStrange Posted August 12, 2016 Author Share Posted August 12, 2016 Is this little problem putting my character at max stats every time I reload the world? Link to comment Share on other sites More sharing options...
Joachim Posted August 12, 2016 Share Posted August 12, 2016 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now