How to get the positions of game objects spawned by a factory component? (SOLVED)

Can the positions if all the instances in a factory component be access from the script?

You need store new Id from factory in local table/variable and you can access to properties.

For more details read - https://www.defold.com/manuals/collection-factory/
https://www.defold.com/manuals/factory/

3 Likes

Like this:

function init(self)
	-- create a table to hold ids of spawned game objects
	self.ids = {}
end

function update(self, dt)
	-- iterate the list and move game objects
	-- note that animation in a straight line as below should be animated
	-- using go.animate for better performance
	for i,id in ipairs(self.ids) do
		local pos = go.get_position(id)
		pos.x = pos.x + 100 * dt
		go.set_position(pos, id)
	end
end

function on_input(self, action_id, action)
	if action_id == hash("fire") and action.released then
		-- spawn a game object and add the id to the list
		local id = factory.create("#bulletfactory")
		table.insert(self.ids, id)
	end
end

PS Please add a descriptive topic when you create a post. “Need some help please” is not a very helpful topic :slight_smile: (I’ve changed it for you)

2 Likes

thanks