M_Balthazar_M Posted June 5, 2022 Share Posted June 5, 2022 Hi! I am trying to make a character that can craft consumable items used to upgrade himself. I already did the upgrades part but now i have a bunch of tags and stats that i need saved and loaded. I figured out that has something to do with the functions OnSave and OnLoad but I cannot seem to wrap my head around how to use them. Basically I just need to store the fact that the character has a tag like "test_tag" and his stats like inst.components.health.maxhealth and also being able to load the values like: local function onsave(inst, data) if inst:HasTag("test_tag") then data.something=true end data.maxhealth_or_something = inst.components.health.maxhealth end local function onload(inst, data) if data.something then inst:AddTag("testtag") end inst.components.health.maxhealth = data.something end Can someone please explain to me how all this works? Link to comment Share on other sites More sharing options...
Bumber64 Posted June 5, 2022 Share Posted June 5, 2022 (edited) You need to attach the functions to your character prefab. Put these lines in the function that defines your character: inst.OnSave = onsave inst.OnLoad = onload Edited June 5, 2022 by Bumber64 2 Link to comment Share on other sites More sharing options...
M_Balthazar_M Posted June 7, 2022 Author Share Posted June 7, 2022 Yes, I've put those lines... is the inside of the onsave / onload function that still does not work Link to comment Share on other sites More sharing options...
Bumber64 Posted June 7, 2022 Share Posted June 7, 2022 Doesn't work as in does nothing, or crashes? In your example, you should be loading maxhealth_or_something instead of something into maxhealth. Not sure if your actual code has this problem. Link to comment Share on other sites More sharing options...
M_Balthazar_M Posted June 8, 2022 Author Share Posted June 8, 2022 This is my code that I was using for testing: Spoiler local function onsave(inst, data) data.currenthealth = inst.components.health.currenthealth data.maxhealth = inst.components.health.maxhealth data.currenthunger = inst.components.hunger.current data.maxhunger = inst.components.hunger.max data.currentsanity = inst.components.sanity.current data.maxsanity = inst.components.sanity.max if inst:HasTag("speed") then data.speed = true else data.speed = false end if inst:HasTag("spiderwhisperer") then data.spiderwhisperer = true else data.spiderwhisperer = false end end local function onload(inst, data) if data =~ nil then inst.components.health:SetMaxHealth(data.maxhealth) inst.components.health:DoDelta(data.currenthealth-data.maxhealth) inst.components.hunger:SetMax(data.currenthunger ) inst.components.hunger:DoDelta(data.currenthunger -data.maxhunger) inst.components.sanity:SetMax(data.maxsanity) inst.components.sanity:DoDelta(data.currentsanity -data.maxsanity) if data.speed = true then inst.components.locomotor:SetExternalSpeedMultiplier(inst, "balthazar_speed_mod", 2) else inst.components.locomotor:SetExternalSpeedMultiplier(inst, "balthazar_speed_mod", 1) end if inst:HasTag("spiderwhisperer") then inst:AddTag("spiderwhisperer") else inst:RemoveTag("spiderwhisperer") --just to be sure you don't have a tag you shouldn't have end end end then in the master_postinit ... inst.OnSave = onsave inst.OnLoad = onload It crashes dontstarve_dedicated_server_nullrenderer_x64.exe and therefore I cannot access the world, but the game doesn't go down. Maybe it is a syntax error but I am not familiar enough with lua to understand where Link to comment Share on other sites More sharing options...
Bumber64 Posted June 9, 2022 Share Posted June 9, 2022 (edited) Should probably be: if data.spiderwhisperer then inst:AddTag("spiderwhisperer") elseif inst:HasTag("spiderwhisperer") then inst:RemoveTag("spiderwhisperer") --just to be sure you don't have a tag you shouldn't have end There should be a log file somewhere containing any errors that occur. Not familiar with dedicated servers, but regular logs are usually stored in Documents/Klei/DoNotStarveTogether. Edited June 9, 2022 by Bumber64 Link to comment Share on other sites More sharing options...
M_Balthazar_M Posted June 10, 2022 Author Share Posted June 10, 2022 Ok, I think I figured why that was crushing. First, Bumber64 was right... kind of. Yes, the spiderwhisperer thing was right but that wasn't the main problem. the if data =~ nil then had to become if data ~= nil then Then there was a problem with hunger that used the current hunger to set the max... Also the speed multiplier was set but it didn't add the tag "speed" again, therefore if you save and exit twice the character looses the speed bonus. Here there is the actual working code, I hope somebody will find this useful! :) Spoiler local function onsave(inst, data) data.currenthealth = inst.components.health.currenthealth data.maxhealth = inst.components.health.maxhealth data.currenthunger = inst.components.hunger.current data.maxhunger = inst.components.hunger.max data.currentsanity = inst.components.sanity.current data.maxsanity = inst.components.sanity.max if inst:HasTag("speed") then data.speed = true else data.speed = false end if inst:HasTag("spiderwhisperer") then data.spiderwhisperer = true else data.spiderwhisperer = false end end local function onload(inst, data) if data ~= nil then inst.components.health:SetMaxHealth(data.maxhealth) inst.components.health:DoDelta(data.currenthealth-data.maxhealth)--current-max=-(max-current) e dodelta lo toglie. inst.components.hunger:SetMax(data.maxhunger) inst.components.hunger:DoDelta(data.currenthunger-data.maxhunger) inst.components.sanity:SetMax(data.maxsanity) inst.components.sanity:DoDelta(data.currentsanity-data.maxsanity) if data.speed then inst.components.locomotor:SetExternalSpeedMultiplier(inst, "balthazar_speed_mod", 2) inst:AddTag("speed") else inst.components.locomotor:SetExternalSpeedMultiplier(inst, "balthazar_speed_mod", 1) inst:RemoveTag("speed") --just to be sure you don't have a tag you shouldn't have end if data.spiderwhisperer then inst:AddTag("spiderwhisperer") else inst:RemoveTag("spiderwhisperer") --just to be sure you don't have a tag you shouldn't have end end end 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