Jump to content

Recommended Posts

Just need a bit of help with the positioning of things I'm spawning from my component.

I've had the idea to try making some kind of portable base, something the player can carry around and take through shards.
Currently, I just search for entities, store them in a table, and remove them from the world.

But when I spawn them back into the world, I can't figure out how to keep their spacing at the new location. So I just dump them into a single spot at the moment. I'm not really any good at math, so if anyone can help with that, it would be amazing!

PocketInterior:

Spoiler
function PocketInterior:Recreate_Test()
	if next(self.interior_storage) ~= nil then		
	
		--Currently the component owner. Swap this for a valid interior location eventually.
		local target_x,target_y,target_z = self.inst.Transform:GetWorldPosition()
	
		for i, v in ipairs(self.interior_storage) do
			--==Spawn==--
			local spawned_object =  SpawnSaveRecord(v)

			--==Reposition==--
			local x,y,z = spawned_object.Transform:GetWorldPosition()
		
			--Math... Need to relocate to our target position, WITHOUT messing up our spawned_object'a spacing.
			x = x
			y = y
			z = z

			--Just dump all our stuff into a our target spot for now. Fix later.
			spawned_object.Transform:SetPosition(target_x, target_y, target_x)
			
			--==Print New Location==--
			print("Spawned: -"..v.prefab.."- at location : X:"..target_x.." Y:"..target_y.." Z:"..target_z)
			
		end
	else
		print("We don't have anything to make our rebuild our interior!")
	end
end

 

 

Link to comment
https://forums.kleientertainment.com/forums/topic/156826-positioning-help/
Share on other sites

Alright. How though?

I've tried making the spawned objects position relative to 0,0,0 by subtracting the component owner's position from the spawned objects, then adding the owner's position again to get the actual target position. But that wildly changes if I move around while carrying the item with my PocketInterior component.

 

Spoiler
function PocketInterior:Recreate_Test() --Cleaned it up a bit.
	if next(self.interior_storage) ~= nil then		
	
		--Currently the component owner. Swap this for a valid interior location eventually.	
		local target_pos = Vector3(self.inst.Transform:GetWorldPosition())
		print(target_pos)
	
		for i, v in ipairs(self.interior_storage) do
			--==Spawn==--
			local spawned_object = SpawnSaveRecord(v)

			--==Reposition==--
			local object_pos = Vector3(spawned_object.Transform:GetWorldPosition())
			
			local new_x = object_pos.x - target_pos.x
			local new_z = object_pos.z - target_pos.z
			
			spawned_object.Transform:SetPosition(new_x, object_pos.y, new_z)
			
			--==Print New Location==--
			print("Spawned: -"..v.prefab.."- at location : X:"..new_x.." Y:"..target_pos.y.." Z:"..new_z)
			
		end
	else
		print("We don't have anything to make our rebuild our interior!")
	end
end

 

 

Edited by DingoLingo
Forgot to add snippet.



Oh, right! My bad.

Spoiler
local PocketInterior = Class(function(self, inst)
	self.inst = inst
	self.interior_storage = {}
end)


--===========================--
--==oO REMOVE / RECREATE Oo==--
--===========================--

function PocketInterior:Remove_Test()
	--==Clear Interior Storage==--
	print("Forgetting current Interior...")
	self.interior_storage = {}
	
	--==Search for things to store==--
	--Currently the component owner. Swap this for a valid interior location eventually.
	local x,y,z = self.inst.Transform:GetWorldPosition()
	
	local ents = TheSim:FindEntities(x, y, z, 20, nil, { "player", "INLIMBO", "FX", "pocketinterior_item" })
    for k,v in pairs(ents) do
        if v then
			--==Save and Remove==--
			local stored_object = v:GetSaveRecord()
			table.insert(self.interior_storage, stored_object)
			v:Remove()
        end
    end
end

function PocketInterior:Recreate_Test()
	if next(self.interior_storage) ~= nil then		
	
		--Currently the component owner. Swap this for a valid interior location eventually.	
		local target_pos = Vector3(self.inst.Transform:GetWorldPosition())
		print(target_pos)
	
		for i, v in ipairs(self.interior_storage) do
		
			--==Spawn==--
			local spawned_object = SpawnSaveRecord(v)

			--==Reposition==--
			local object_pos = Vector3(spawned_object.Transform:GetWorldPosition())
			
			local new_x = object_pos.x - target_pos.x
			local new_z = object_pos.z - target_pos.z
			
			spawned_object.Transform:SetPosition(new_x, object_pos.y, new_z)
			
			--==Print New Location==--
			print("Spawned: -"..v.prefab.."- at location : X:"..new_x.." Y:"..target_pos.y.." Z:"..new_z)
			
		end
	else
		print("We don't have anything to make our rebuild our interior!")
	end
end



--=====================--
--==oO SAVE / LOAD Oo==--
--=====================--
function PocketInterior:OnSave()
    return
    {
        interior_storage = self.interior_storage,
    }
end

function PocketInterior:OnLoad(data)
	if data ~= nil then	
		self.interior_storage = data.interior_storage
	end
end

return PocketInterior

 

 

--==Save and Remove==--
local stored_object = v:GetSaveRecord()
-- stored_object.x/y/z is from v.Transform
stored_object.relative_x = stored_object.x - x
stored_object.relative_y = (stored_object.y or 0) - y
stored_object.relative_z = stored_object.z - z
table.insert(self.interior_storage, stored_object)
v:Remove()


--==Spawn==--
local spawned_object = SpawnSaveRecord(v)

--==Reposition==--
local relative_x = v.relative_x
local relative_y = v.relative_y
local relative_z = v.relative_z

spawned_object.Transform:SetPosition(relative_x + target_pos.x, relative_y + target_pos.y, relative_z + target_pos.z)

 

Edited by _zwb

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