Jump to content

Recommended Posts

From when I made other mods I was under the impression that to save and reload variables in components all one had to do was something like...

function Amanager:OnSave()	return { killed = self.killed, }endfunction Amanager:OnLoad(data)	if data.killed ~= nil then		self.killed = data.killed	endend

...but for some reason neither of these functions are firing. My guess is I'm missing something obvious but for the life of me I can't find it. Here is the mod...

 

achieve_0.1_working.zip

 

...if anyone could point out what I am doing wrong I would appreciate it.

Link to comment
https://forums.kleientertainment.com/forums/topic/29480-data-not-saving/
Share on other sites

If you're adding the component to a character prefab using SimPostInit, saving won't work. Not totally sure the cause, but simplex posted a workaround here

Since his code got smashed into one line, here it is in the proper format:

-- need to add the component in here, otherwise OnSave doesn't work rightAddPrefabPostInit("world", function(inst)	GLOBAL.assert( GLOBAL.GetPlayer() == nil )	local player_prefab = GLOBAL.SaveGameIndex:GetSlotCharacter()	-- Unfortunately, we can't add new postinits by now. So we have to do	-- it the hard way...	GLOBAL.TheSim:LoadPrefabs( {player_prefab} )	local oldfn = GLOBAL.Prefabs[player_prefab].fn	GLOBAL.Prefabs[player_prefab].fn = function()		local inst = oldfn()		-- Add components here.		inst:AddComponent("yourcomponent")		return inst	endend)

If you're adding the component to a character prefab using SimPostInit, saving won't work. Not totally sure the cause, but simplex posted a workaround here

Since his code got smashed into one line, here it is in the proper format:

-- need to add the component in here, otherwise OnSave doesn't work rightAddPrefabPostInit("world", function(inst)	GLOBAL.assert( GLOBAL.GetPlayer() == nil )	local player_prefab = GLOBAL.SaveGameIndex:GetSlotCharacter()	-- Unfortunately, we can't add new postinits by now. So we have to do	-- it the hard way...	GLOBAL.TheSim:LoadPrefabs( {player_prefab} )	local oldfn = GLOBAL.Prefabs[player_prefab].fn	GLOBAL.Prefabs[player_prefab].fn = function()		local inst = oldfn()		-- Add components here.		inst:AddComponent("yourcomponent")		return inst	endend)

Thank you,  that's good to know I thought I was going crazy

 

thanks @simplex as well.

Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now.

Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now.

Using AddPrefabPostInit works, what doesn't is using AddSimPostInit. So the above workaround is for when you want to add a component to all player characters, not just a predetermined subset.

Just curious if I'm mistaken here, but from my experience they work when adding them via PrefabPostInit. I know this is problematic with mod characters but as long as you are interested in adding a component to your own mod character that should be easier than what you're doing now.

What I'm doing is making a modular mod that all you have to do is add a few lines of code to the modmain and it runs a few different lua files that add in more than just a lone component could do (at least as far as I can tell there are things in my achieve.lua that I can't do in a component file). The other thing I believe it will do is if multiple mods use these files it will only install it once, though I need to test this feature to make sure.

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