Is it possible to track all factory objects in a collection?

The situation is as follows: I want all creatures to be removed after a player loses. In theory I can write a function that records the id of all spawned creatures, but maybe there is a simpler way to do it ? I have in mind an internal function in the defold itself that keeps track of all spawned go.

Note: Restarting the level is not an option due to code peculiarities.

I think the easier way, or at least what I usually use, is: the spawner keeps track of the spawned collections and remove them when asked so.

Ciao!

Ciao

That’s how I wanted to implement it at first, but then I thought maybe I’m overcomplicating things and there’s an internal function.

By the way, how to know the url of the created object ?

collectionfactory.create returns a table of urls, one for each game object in the collection. I usually have a root game object containing all other game objects of the collections. So maybe the spawner can do something like this:

-- in init
	self.spawned_urls = {}
	self.count = 0

-- when spawning
	self.count = self.count + 1
	self.spawned_urls[self.count] = collectionfactory.create(...)["/root"]

-- when deleting
	for i = 1, self.count do
		go.delete(self.spawned_urls[i], true) -- true to recursively delete all children
	end

I hope what I am suggesting is right. I am not an expert in Lua, maybe there are better ways or, even, I am missing something.

Ciao!

2 Likes