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

**URL:** <https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911>\
**Category:** Questions\
**Created:** [November 10, 2023, 10:44am UTC](https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911 "2023-11-10T10:44:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Regven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/regven/32/32919_2.png) [@Regven](https://forum.defold.com/u/Regven)\
**Post date:** [November 10, 2023, 10:44am UTC](https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911/1 "2023-11-10T10:44:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [November 10, 2023, 11:24am UTC](https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911/2 "2023-11-10T11:24:45Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Regven](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/regven/32/32919_2.png) [@Regven](https://forum.defold.com/u/Regven)\
**Post date:** [November 10, 2023, 11:37am UTC](https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911/3 "2023-11-10T11:37:26Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![roccosaienz](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/roccosaienz/32/19991_2.png) [@roccosaienz](https://forum.defold.com/u/roccosaienz)\
**Post date:** [November 10, 2023, 11:47am UTC](https://forum.defold.com/t/is-it-possible-to-track-all-factory-objects-in-a-collection/74911/4 "2023-11-10T11:47:44Z")

</div>

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!
