How to delete a object created with a factory

Hi, i’m using a factory to spawn a go, but i want to delete the first one if another is created. Theres any function to make the factory erase the first one or i have to use the go.delete()?

You have to use go.delete

Yes, use go.delete and pass the id of game object you wish to delete.

Some additional info worth knowing: If your factory is a collection factory you need to delete all the objects in that collection scene graph in order for them to be deleted. Deleting the root game object isn’t enough (to be clear, this applies to all scene graph deletions, not just collection factories). Failure to delete the other game objects will result in a leak.

So you need to do manual memory management of these things, or you will run out of resources/sprites after some time playing the game.

4 Likes

I would not call that “manual memory management”, I would call it “being responsible of what you create”. The relationship between a parent and a child in a scene graph is based on the geometrical transform, not ownership/existance. Things can attach and deattach, which makes it a very dynamic relationship. It is not universally true that deleting a parent should delete its child. If there is a need to delete entire scene graph hierarchies, we could look into supplying a recursive go.delete, recursive based on the scene graph of course.

2 Likes

Isn’t “manual memory management” and “being responsible of what you create” almost the same thing? :smile:

Anyways, there really is a need for a recursive delete. It is the common and intuitive case. Having the ability to delete just the root is a good feature, but it isn’t the common/intuitive case. If anything, that case should be something that you state explicitly.

4 Likes

Has there been any movement on this suggestion from Nov. '15 ?

Yes, there has been support for recursively deleting game objects for quite some time:

local recursive = true
go.delete("some_id", recursive)
2 Likes

Awesome!