Groups in dynamically created scenes

I am a little stuck at the moment, spent quite a bit of time debugging into Phaser to see what is going on, and could really use a little advice.

I currently have a main scene that then creates 4 sub-scenes dynamically at run time. In those dynamically created scenes I utilize numerous groups as pools.

Everything works correctly. I now navigate to another screen, but before I do I manually remove the dynamically created scenes and then issue the scene.start to move to another scene.

This also works correctly.

However, when I navigate back to the first screen, it successfully creates the 4 dynamically created sub-scenes and all of the groups appear to be created correctly with their children initially set to an empty array.

But when a timer event is called some of the groups have their children now undefined, which causes an error on getting an item from the pool.

Uncaught TypeError: Cannot read property ‘entries’ of undefined

The only place I can find in the Phaser 3 code that sets the children is the destroy method, but it doesn’t appear to be called after I navigate back to the first scene. My first though was that there might be some events being triggered after the scene is up and running, but that doesn’t appear to be the case.

I could try to move away from dynamically creating and destroying sub-scenes, but wasn’t sure why the groups were getting set up correctly, but then changing.

Sorry for the long post, but just wondering if anyone has run into something like this before.

Thank you.

1 Like

So I finally found the issue.
When you remove() dynamically added sub-scenes before start another scene from the host scene, it seems to clear everything out but the listeners on the registry. If I put the following in the main scenes’ init() method that hosts the sub scenes

this.registry.events.off(‘changedata’, undefined, undefined, false);

Then everything is cleared out properly and the next time I move(start) the host scene everything works.

Update: I actually moved the remove listener code from the init method to right before I start the next scene so I’m not left with extra handlers existing until I come back to my main scene.

FWIW I ran into something similar with registry listeners not being cleaned up as well (Help with error after `launch`ing a scene that has been `stop`ped).

I ended up listening to the shutdown event in the scene that added the listeners, and removed them in my shutdown event handler.