Collision issue if collectgarbage() is not used

Enjoying a bit of Defold time at the moment, which is a luxury!

I bumped into an issue recently that took me a while to figure out. This is the flow:

  1. Monarch shows a screen with a trigger collector collision object.
  2. The scene spawns collision objects with collectionfactory.create() and references to these objects are stored in a module.
  3. When the collector collision object touches a spawned object, it deletes it IF there is a matching reference in the module.
  4. Once all objects have been removed, the screen reloads.

The issue: After a few reloads the collision object starts returning game object ids that don’t match the ones in the module. The reference name is something like hash: [/collection94/collisionobject] and the collision object returns something like hash: [<unknown:3844889965836164311>].

Now, after a lot of troubleshooting, I realised that if collectgarbage() is called before spawning the collections, the collision object returns the correct id! See minimal project and vid below.

There are no errors in the console when the “unknown” ids are returned. Is this expected behaviour? Should I start taking garbage collection more seriously? :grin:

The reason I need to be able to match the id returned by the collision object with the module is because I’m using this method to circumvent the 16 collision group limit in box2d. On creation I can assign any arbitrary data to the objects, for example “this is a spike”, “this is a coin” and keep the same collision group id for all collision objects.

Vid:

Minimal repro project:
Collision Issue.zip (7.5 KB)

1 Like

I’ve been able to pinpoint exactly what the reason was: The module used to keep the references.

If the garbage collection is triggered after the variable that holds the ids is cleared, the issue never happens:

function M.clear()

	tracked = {}
	collectgarbage("collect")

end

If a module isn’t used, and the ids are stored in the scene being unloaded, the issue doesn’t happen.

The issue must have been caused by id references from the old scene surviving in the module and somehow interfering with ids in the new scene.

In my case I need a module, because I need to access the references from two separate .script files.

Case closed, thanks for listening! :slight_smile:

Relying on collectgarbage() for Defold functionality to work feels a bit sketchy, so I created an issue for this:

2 Likes