Ultroman Posted August 1, 2019 Share Posted August 1, 2019 You don't need anything other than what I mentioned. I've never heard of GetSaveRecord(), but it seems to be the system function that loads all the data of an entity, but you don't need to do anything with that. Instances of prefabs have their own save/load functions and components have their own save/load functions. I'm saying you should use the save/load functions on the instance, hence the mention of inst.OnSave and inst.OnLoad functions. You need to do your saving like I described in that previous post, whether it be an entity or a component. Link to comment Share on other sites More sharing options...
Dcrew Posted August 1, 2019 Author Share Posted August 1, 2019 It does nothing: AddPrefabPostInit(prefab, function(ent) if not ent.enhancedbiomes then ent.enhancedbiomes = {} end ent:ListenForEvent("ondropped", function() ent.enhancedbiomes.wasplayerowned = true end) ent.onsave = function(inst, data) data.wasplayerowned = ent.wasplayerowned end ent.onload = function(inst, data) if data and data.wasplayerowned then ent.wasplayerowned = data.wasplayerowned end end end) Link to comment Share on other sites More sharing options...
Ultroman Posted August 1, 2019 Share Posted August 1, 2019 The function names are case-sensitive, and are named OnSave and OnLoad. Link to comment Share on other sites More sharing options...
Dcrew Posted August 1, 2019 Author Share Posted August 1, 2019 (edited) In rocks.lua they are both all lowercase... Edit: didn't do anything even when I tried naming them OnSave & OnLoad Edited August 1, 2019 by Dcrew Link to comment Share on other sites More sharing options...
Ultroman Posted August 1, 2019 Share Posted August 1, 2019 (edited) In rocks.lua they are also capitalized. Only the local functions are not, and their names are arbitrary. Only the names of the functions on the inst are important to get right. local function onsave(inst, data) data.treeSize = inst.treeSize end local function onload(inst, data) if data ~= nil and data.treeSize ~= nil then inst.treeSize = data.treeSize setPetrifiedTreeSize(inst) end end -- In the fn() inst.OnSave = onsave inst.OnLoad = onload So, technically, you could also do naming like this: local function yoghurt(inst, data) data.treeSize = inst.treeSize end local function coconuts(inst, data) if data ~= nil and data.treeSize ~= nil then inst.treeSize = data.treeSize setPetrifiedTreeSize(inst) end end -- In the fn() inst.OnSave = yoghurt inst.OnLoad = coconuts But you don't have your save/load functions as local functions, but instead in-line functions, so I'm guessing you DID change inst.onsave to inst.OnSave. In that case, it means that there are other reasons why your code is not working. You're not setting the same variable as you are saving/loading. You set "ent.enhancedbiomes.wasplayerowned = true" but try to read "ent.wasplayerowned". I have fixed this in the code below. I have also made sure to cover certain cases where your enhancedbiomes array hasn't been added when loading, and the case where wasplayerowned is nil (so I set it to false). That said, with in-line functions you are always overwriting the original OnSave/OnLoad instead of extending them, so any existing save-data is lost and is not loaded. You should make local functions, like in the rocks.lua example, and then check if there are existing save/load functions to extend. Like this: local mysavefunction = function(inst, data) data.wasplayerowned = ent.enhancedbiomes and ent.enhancedbiomes.wasplayerowned or false end local myloadfunction = function(inst, data) if data and data.wasplayerowned then if not ent.enhancedbiomes then ent.enhancedbiomes = {} end ent.enhancedbiomes.wasplayerowned = data.wasplayerowned end end AddPrefabPostInit(prefab, function(ent) if not ent.enhancedbiomes then ent.enhancedbiomes = {} end ent:ListenForEvent("ondropped", function() ent.enhancedbiomes.wasplayerowned = true end) if ent.OnSave == nil then ent.OnSave = mysavefunction else local oldOnSave = ent.OnSave ent.OnSave = function(inst, data) mysavefunction(inst, data) oldOnSave(inst, data) end end if ent.OnLoad == nil then ent.OnLoad = myloadfunction else local oldOnLoad = ent.OnLoad ent.OnLoad = function(inst, data) myloadfunction(inst, data) oldOnLoad(inst, data) end end end) Edited August 1, 2019 by Ultroman Link to comment Share on other sites More sharing options...
Dcrew Posted August 1, 2019 Author Share Posted August 1, 2019 This works: AddPrefabPostInit(prefab, function(ent) if not ent.enhancedbiomes then ent.enhancedbiomes = {} end ent:ListenForEvent("ondropped", function() ent.enhancedbiomes.wasplayerowned = true end) local onsave, onload = ent.OnSave, ent.OnLoad ent.OnSave = function(inst, data) data.enhancedbiomes = {} if ent.enhancedbiomes.wasplayerowned then data.enhancedbiomes.wasplayerowned = ent.enhancedbiomes.wasplayerowned end if onsave then onsave(inst, data) end end ent.OnLoad = function(inst, data) if data and data.enhancedbiomes and data.enhancedbiomes.wasplayerowned then ent.enhancedbiomes.wasplayerowned = data.enhancedbiomes.wasplayerowned if onload then onload(inst, data) end end end end) I leave 'wasplayerowned' as nil if it's not true on purpose so I don't waste or save space saving it (since it's false if it's nil anyway), I think I did everything right here or have I missed something? Link to comment Share on other sites More sharing options...
Ultroman Posted August 1, 2019 Share Posted August 1, 2019 Looks good! Link to comment Share on other sites More sharing options...
Dcrew Posted August 1, 2019 Author Share Posted August 1, 2019 Thank you very much, I've learnt and achieved so much in just this thread! Link to comment Share on other sites More sharing options...
Ultroman Posted August 1, 2019 Share Posted August 1, 2019 Just now, Dcrew said: Thank you very much, I've learnt and achieved so much in just this thread! You're welcome Yeah, we went far and wide in this one ^^ 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