Jump to content

Recommended Posts

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 by Vuohi

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	endend

But 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_bend

As 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 by Arkathorn

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.

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...