Jump to content

Saving and loading references to other entities?


Recommended Posts

So yesterday I was experimenting with using a mod to fix a bug where Spider Monkey herds from Hamlet will move to a new tree every time the game is loaded, which seems to be because the reference they make to the tree they bond to isn't saved or loaded between sessions. To attempt to fix this, I borrowed a method I saw in a few other places used for similar situations:

	AddPrefabPostInit("spider_monkey_herd", function(inst)
		inst.OnSave = function(inst, data)
			if inst.homeTree then
				data.homeTree = inst.homeTree.GUID
			end
		end
		inst.OnLoadPostPass = function(inst, ents, data)
			if data and data.homeTree then
				if ents[data.homeTree] then
					inst.homeTree = ents[data.homeTree].entity
				end
			end
		end
	end)

But for some reason, it's not working in this case. I borrowed this method from the mant warrior prefabs, which use this same method to save a reference to the Queen Womant. I used print and the console to do some debugging and verified that in both cases, the saved and loaded GUID was the correct GUID from the previous session, but for some reason, it works perfectly for the mant warrior's case and not at all for the spider monkey herd's. I even tried editing this directly into the game's files to see if it was just a limitation of the mod environment, and it still doesn't work. I can't figure out why.

Link to comment
Share on other sites

Have you tried just Save/Load the tree? Like:

AddPrefabPostInit("spider_monkey_herd", function(inst)
    inst.OnSave = function(inst, data)
		if inst.homeTree then
			data.homeTree = inst.homeTree
		end
	end
    	
    inst.OnLoad = function(inst, data)
      if data and data.homeTree then
      	inst.homeTree = data.homeTree
      end
    end
end)

Idk, I like fixing bugs, I'll look it better later.

Link to comment
Share on other sites

On 6/5/2022 at 9:48 AM, Faintly Macabre said:

Doesn't work. Datadumper (understandably) doesn't like being passed an entire entity like that.

I was thinking. Have you tried using a DoTaskInTime(0) with your method? I don't know, sometimes it works like magic.

Link to comment
Share on other sites

On 6/7/2022 at 1:50 PM, Leonidas IV said:

I was thinking. Have you tried using a DoTaskInTime(0) with your method? I don't know, sometimes it works like magic.

That didn't do it, but I did manage to work it out by looking around for more examples.

	AddPrefabPostInit("spider_monkey_herd", function(inst)
		inst.OnSave = function(inst, data)
			local refs = {}
			if inst.homeTree then
				data.homeTree = inst.homeTree.GUID
				table.insert(refs, inst.homeTree.GUID)
			end
			return refs
		end
		inst.OnLoadPostPass = function(inst, ents, data)
			if data and data.homeTree then
				print("Debug", data.homeTree, ents[data.homeTree].entity)
				if ents[data.homeTree] then
					inst.homeTree = ents[data.homeTree].entity
				end
			end
		end
	end)

The entity manager allows OnSaves to optionally pass tables of references. I assume these make it easier for the game to cross-reference things properly between saves and loads somehow. It does beg the question of why it worked in the Mant Warrior case, though, because it wasn't returning a table of references. Maybe because there should only ever be one Mant Queen so it doesn't need more reference information? Or maybe it shouldn't work but just kind of does? I dunno.

In any case, please add this to your Bug Fixes mod if you like, after sanity checking and such of course (I'm not a real programmer and can only just kind of fumble my way through code that was written to be readily human-readable). I was playing around with it in the first place to pass the bug and hopefully a fix along to you.

Link to comment
Share on other sites

32 minutes ago, Leonidas IV said:

Congrats on discovering it! Very kind of you, I'll include your fix, mentioning you :) The code is well done.

Great! You can leave out the debug line obviously, thought for sure I'd cut that out already.

I actually have a bunch more bugs and fixes I've been working on to pass along too if you'd like them. I'll try to get those to you sometime later today.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...