Vuohi Posted December 20, 2015 Share Posted December 20, 2015 (edited) I tried googling but didn't find quite the answer I needed. What's the correct way to save a custom component in a player entity? I'm making a character who is able to give himself permanent enhancements, a bit like WX-78, but instead of just WX's level he has separate enhancement tracks for health, hunger etc. To make things a little more compact I tried saving all the data in a custom component but attempting to save it with an onsave() function causes a crash. The error is:Cannot dump userdata (DynamicShadow (0C3C09E0) - unknown)(I'm using the Moar Save Slots mod, if that matters.) The onsave() looks like this:local function onsave(inst, data)if inst.components.alchemyupgrades thendata.alchemyupgrades = {}for k, v in pairs(inst.components.alchemyupgrades) dodata.alchemyupgrades[k] = vendendend(Also, how do you keep the forum software from flattening the indents in code?) Edited December 20, 2015 by Vuohi Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/ Share on other sites More sharing options...
Vuohi Posted December 20, 2015 Author Share Posted December 20, 2015 Update: doh! I forgot the alchemyupgrades component contains a back-reference to the entity. So of course it crashes when it tries to do that. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699546 Share on other sites More sharing options...
Arkathorn Posted December 20, 2015 Share Posted December 20, 2015 (edited) Inside the component:function AlchemyUpgrades:OnSave() local data = {} for k,v in pairs(self) do data[k] = v endendfunction AlchemyUpgrades:OnLoad(data) for k,v in pairs(data) do self[k] = v endendBut I would suggest manually specifying each field, as it is good form:--In OnSavedata.field_a = self.field_adata.field_b = self.field_b...--In OnLoadif data.field_a then self.field_a = data.field_aendif data.field_b then self.field_b = data.field_bendAs for the tabs, I just copy paste the tab character in. EDIT: The back reference would also be a reason to manually specify fields. Edited December 20, 2015 by Arkathorn Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699547 Share on other sites More sharing options...
Vuohi Posted December 20, 2015 Author Share Posted December 20, 2015 I wanted to save some typing with the for loop; guess laziness doesn't always pay off. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699549 Share on other sites More sharing options...
Arkathorn Posted December 20, 2015 Share Posted December 20, 2015 I wanted to save some typing with the for loop; guess laziness doesn't always pay off.You could store all the persistent (savable/loadable) fields inside a table (eg. 'self.data'), and then iterate through that instead if you really wanted to. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699553 Share on other sites More sharing options...
Vuohi Posted December 20, 2015 Author Share Posted December 20, 2015 Do a component's OnSave and OnLoad get called automatically, or do they have to be called explicitly? Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699555 Share on other sites More sharing options...
Mobbstar Posted December 20, 2015 Share Posted December 20, 2015 Those get called automatically. There's also a LoadPostInit and (I think) a PreLoad, but those are usually not necessary. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699571 Share on other sites More sharing options...
Arkathorn Posted December 20, 2015 Share Posted December 20, 2015 Do a component's OnSave and OnLoad get called automatically, or do they have to be called explicitly?Automatically. Take a look at the built-in components, almost every one has this. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699580 Share on other sites More sharing options...
Vuohi Posted December 21, 2015 Author Share Posted December 21, 2015 Thanks everyone for the advice! I got the saving/restoring of his abilities working. If everything else goes well, I'll have the mod up in the workshop in a few days. Link to comment https://forums.kleientertainment.com/forums/topic/61070-saving-custom-component-on-player/#findComment-699882 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