Jump to content

If entity has been touched by player(s)


Recommended Posts

You don't need anything other than what I mentioned. I've never heard of GetSaveRecord(), but it seems to be the system function that loads all the data of an entity, but you don't need to do anything with that.

Instances of prefabs have their own save/load functions and components have their own save/load functions. I'm saying you should use the save/load functions on the instance, hence the mention of inst.OnSave and inst.OnLoad functions. You need to do your saving like I described in that previous post, whether it be an entity or a component.

Link to comment
Share on other sites

It does nothing:

AddPrefabPostInit(prefab, function(ent)
		if not ent.enhancedbiomes then
			ent.enhancedbiomes = {}
		end
		ent:ListenForEvent("ondropped", function()
				ent.enhancedbiomes.wasplayerowned = true
			end)
		ent.onsave = function(inst, data)
				data.wasplayerowned = ent.wasplayerowned
			end
		ent.onload = function(inst, data)
				if data and data.wasplayerowned then
					ent.wasplayerowned = data.wasplayerowned
				end
			end
	end)

 

Link to comment
Share on other sites

In rocks.lua they are also capitalized. Only the local functions are not, and their names are arbitrary. Only the names of the functions on the inst are important to get right.

local function onsave(inst, data)
    data.treeSize = inst.treeSize
end

local function onload(inst, data)
    if data ~= nil and data.treeSize ~= nil then
        inst.treeSize = data.treeSize
        setPetrifiedTreeSize(inst) 
    end
end

-- In the fn()
inst.OnSave = onsave
inst.OnLoad = onload

So, technically, you could also do naming like this:

local function yoghurt(inst, data)
    data.treeSize = inst.treeSize
end

local function coconuts(inst, data)
    if data ~= nil and data.treeSize ~= nil then
        inst.treeSize = data.treeSize
        setPetrifiedTreeSize(inst) 
    end
end

-- In the fn()
inst.OnSave = yoghurt
inst.OnLoad = coconuts

 

But you don't have your save/load functions as local functions, but instead in-line functions, so I'm guessing you DID change inst.onsave to inst.OnSave. In that case, it means that there are other reasons why your code is not working.

You're not setting the same variable as you are saving/loading. You set "ent.enhancedbiomes.wasplayerowned = true" but try to read "ent.wasplayerowned". I have fixed this in the code below. I have also made sure to cover certain cases where your enhancedbiomes array hasn't been added when loading, and the case where wasplayerowned is nil (so I set it to false).

That said, with in-line functions you are always overwriting the original OnSave/OnLoad instead of extending them, so any existing save-data is lost and is not loaded. You should make local functions, like in the rocks.lua example, and then check if there are existing save/load functions to extend. Like this:

local mysavefunction = function(inst, data)
	data.wasplayerowned = ent.enhancedbiomes and ent.enhancedbiomes.wasplayerowned or false
end

local myloadfunction = function(inst, data)
	if data and data.wasplayerowned then
		if not ent.enhancedbiomes then
			ent.enhancedbiomes = {}
		end
		ent.enhancedbiomes.wasplayerowned = data.wasplayerowned
	end
end

AddPrefabPostInit(prefab, function(ent)
	if not ent.enhancedbiomes then
		ent.enhancedbiomes = {}
	end
	ent:ListenForEvent("ondropped", function()
		ent.enhancedbiomes.wasplayerowned = true
	end)
	if ent.OnSave == nil then
		ent.OnSave = mysavefunction
	else
		local oldOnSave = ent.OnSave
		ent.OnSave = function(inst, data)
			mysavefunction(inst, data)
			oldOnSave(inst, data)
		end
	end
	if ent.OnLoad == nil then
		ent.OnLoad = myloadfunction
	else
		local oldOnLoad = ent.OnLoad
		ent.OnLoad = function(inst, data)
			myloadfunction(inst, data)
			oldOnLoad(inst, data)
		end
	end
end)

 

Edited by Ultroman
Link to comment
Share on other sites

This works:

AddPrefabPostInit(prefab, function(ent)
		if not ent.enhancedbiomes then
			ent.enhancedbiomes = {}
		end
		ent:ListenForEvent("ondropped", function()
				ent.enhancedbiomes.wasplayerowned = true
			end)
		local onsave, onload = ent.OnSave, ent.OnLoad
		ent.OnSave = function(inst, data)
				data.enhancedbiomes = {}
				if ent.enhancedbiomes.wasplayerowned then
					data.enhancedbiomes.wasplayerowned = ent.enhancedbiomes.wasplayerowned
				end
				if onsave then
					onsave(inst, data)
				end
			end
		ent.OnLoad = function(inst, data)
				if data and data.enhancedbiomes and data.enhancedbiomes.wasplayerowned then
					ent.enhancedbiomes.wasplayerowned = data.enhancedbiomes.wasplayerowned
					if onload then
						onload(inst, data)
					end
				end
			end
	end)

I leave 'wasplayerowned' as nil if it's not true on purpose so I don't waste or save space saving it (since it's false if it's nil anyway), I think I did everything right here or have I missed something?

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