Clearing a gameworld (SOLVED)

The Backcground

As I work my way through the various capabilities of Defold I am creating a hypercasual game. Every once in a while the game is deemed “over” - out of lives, won etc being the different triggers that cause it to be over. At that point I would like to clear all the residual objects in the gameworld so I can start the next game with a “clean slate”.

My solution
Unless I am much mistaken Defold places the onus for keeping track of factory spawned game objects on the developer. So here is what I do

  • While I have several flavors of game object they are all driven by the same script.
  • The GOs are spawned from their respective factories during the course of the game.
  • Some of the spawned objects might well get consumed during the game - as a result of player actions or other, incidental, collisions
  • When the game is deemed to be “over” zero, one or more residual objects will remain behind and have to be discarded in preparedness for the next game
  • In the shared script that drives the lifecycle of these game objects I do the following

I use a global table _goList to keep track of all factory created game objects

function init(self)
  _goList[go.get_id()] = object_sub_type
 -- the assigned value could be anything. I am using the object_sub_type for convenience
end

function final(self)
  _goList[go.get_id()] = nil
  -- for objects destroyed DURING the course of the game remove them from the table
end

function endOfGameCleanUp()
  for k,v  in pairs(_goList) do go.delete(k) end
end    

This works. However, there is a residual issue arising from the fact that most, if not all, these game objects are animated. In the normal course of events I trap the animation_done message and do other housekeeping chores. However, when the game is deemed to be over

  • Those chores don’t really matter anyway
  • The fact that I have in the interim deleted the game object(s) in question results in error messages similar to the one below

Could not send animation_done to instance: /instance12#animator

being generated. Short question - how can I prevent this? Or should I be looking at handling by “game over” cleanup differently

I load everything in the game world through a collection proxy and unload & reload it to restart the game. This also lets you pause it. No housekeeping whatsoever.

5 Likes

What he said :slight_smile:

This is the way to do it! Super easy and very convenient. And usually not even noticeable to the player since loading and unloading usually is very fast.

Thank you for the suggestion. However, it is not evident to me that I can apply it in my particular case. I do not have a pre-defined set of objects to load. I start with a small set of objects which then interact in ways that are entirely unpredictable and cause other objects to poplulate the game world. The whole process is unpredictable, unrepeatable and dynamic

A collection can be used for many different things. It can be used to create a boss or player character or a group of smaller enemies. In these cases the collections are either added directly to another collection or spawned using collection factories.

But a collection can also be used as the base for a game or level, with some scripts and factories to spawn the actual game objects. In this case the collection is usually loaded from a collection proxy. This is what I’m thinking you should use. And the nice thing here is that anything spawned from a factory in the collection will be removed when the collection us unloaded.

1 Like

Yeah, it doesn’t matter what you have going on. Everything you can do happens inside one collection or another. Instead of doing things inside the bootstrap collection, you do them inside a proxy collection. That’s it.

To make the change, create a new collection and set it as your bootstrap collection in your game.project. In the new collection, only add a game object with a loader script and a collection proxy for your old main collection. The loader script just needs to handle loading and unloading the proxy. You can send it messages from anywhere in your game when you need to pause, resume, unload, or reload.

2 Likes

I couldn’t agree more. Using collection proxies to do housekeeping for you is a huge time saver. And as a bonus you can pause the game for free, or even run it in slow/fast motion with set_time_step().

I can recommend Monarch to let you switch between collections effortlessly.

2 Likes

Thank you for your help - and all the repies