Jump to content

[Help]OnSave and OnLoad?


Recommended Posts

I write code to try save item data to disk and hope read this data next booting, but seem this data don't save successfully. I couldn't read save data at reboot, why?

 

My code following:

function container:Open(doer)  -- Try to load last save data  inst.OnLoad = onload  -- If last data(inst.lockID) is empty, save opener userid to inst data  if inst.lockID == "" then    inst.lockID = doer.userid    -- Save Data    inst.OnSave = onsave    -- Reload Data    inst.OnLoad = onload    return nil   end	end-- Save Datalocal function onsave(inst, data)    data.lockID = inst.lockIDend-- Load Datalocal function onload(inst, data)  if data and data.lockID then    inst.lockID = data.lockID  end	end

But seems data don't save,  I restart serevr and trace first reload data, inst.lockid was still empty.

Link to comment
Share on other sites

What is container? A prefab? Or are you trying to edit the container component?

OnLoad and OnSave don't get executed whenever you write the line, they get executed when loading and saving.

AddComponentPostInit("container", function(self)	self.lockID = nil	local old1 = self.Open	function self:Open(doer)		if self.lockID == nil then			self.lockID = doer.userid		end		old1(self, doer)	end	local old2 = self.OnSave	function self:OnSave()		local data = {}		data.lockID = self.lockID		data.olddata = old2(self)		return data	end	local old3 = self.OnLoad	function self:OnLoad(data, newents)		if data and data.lockID then			self.lockID = data.lockID		end		if data and data.olddata then			old3(self, data.olddata, newents)		end	endend)

You have to override Open, OnSave, and OnLoad.

Link to comment
Share on other sites

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
 Share

×
  • Create New...