Hello,
I am having an issue when trying to implement a game over screen. I am attempting to update a text element on screen which is only enabled when the player “dies”. This works fine when I first launch the game and load in the scene for the first time. However, when I reload the scene and attempt to do this again, I get an error saying that this element is undefined. I have attempted to both find the text entity by name in the script and by adding an entity attribute to the script and adding the text entity manually.
Hi @eggs!
How do you reload the scene?
Do you use events? Maybe an event before reloading is still active and using an entity that you destroyed at reload.
Otherwise, can you please share the editor link of your project so someone can take a look?
I use this.app.scenes.changeScene('myScene');
to reload the scene.
My link is here
The issue is in ui.js lines 59-66. All the other entities work fine. There is some redundant code in there that I haven’t taken out yet so forgive me . The script is attached to the 2D screen entity in the “main” scene. I have attempted to both make the “score gameOver” entity (the text entity in question) a child of the gameOver overlay and as a child of just the 2D screen entity.
The problem is probably caused by your events. When you create an event, you also need to make sure you remove it at some point, for example when you change the scene and destroy the entities. Below some example code.
// listen for the player:move event
this.app.on('player:move', onPlayerMove);
// remove player:move event listeners when script destroyed
this.on('destroy', function() {
this.app.off('player:move', onPlayerMove);
});
This seems to have been the issue! Thanks!
Do you know why this happens?
The error is coming from an event that is created in your previous scene. This event is still active, because it seems to be impossible to remove them automatically. This event has references to entities that are removed at scene change. That’s why the entities are undefined in your new scene.
I’m wondering why the event isn’t overwritten by the event from your new scene, so maybe someone else knows an answer to that.