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