Delete all instances of game object

After I did a bit of research looking for the answer myself I decided to resort to the forum. In the game that I am working on after the character dies, I would like to delete all instances of one of my game objects. The game objects that are to be deleted are being created dynamically with a factory. My question is, is there a command to simply delete all instances of a certain game object? Is there a way to keep track of all the game objects when they are created? (So I can iterate through and delete them)

Any and all help is greatly appreciated. Thanks.

Hello @benjaminlucier ,

Yes, there certainly is such a way of doing that, take a look at the getting started tutorial - resetting the level part and you will get exactly what you’re looking for.

Cheers

2 Likes

There is no built-in go.delete_all() or anything like that. When you create game objects using a factory you get game object ids back. You need to do the book-keeping yourself and delete the game objects when you no longer need them.

2 Likes

The simplest way to do this would probably be a global variable that is set to “true” when your character dies. Then, add a script in your game object saying “if variable is true go.delete()”.

(Someone from defold will now explain to you why global variables are best to be avoided, but I think that whilst you’re learning, they’re not so bad)

3 Likes

Alternatively create a property on the player script:

go.property("is_alive", true)

And then read that in each game object’s update():

go.get("/player#script", "is_alive")

and delete if false.

1 Like