Restarting a Scene in Phaser 4: Resetting Levels and Game State
Restarting a scene is one of the most common actions in a game. You use it when the player loses, presses a restart button, chooses “Try Again,” or wants to replay a level from the beginning in Phaser 4.
In Phaser 4, scene management is handled through this.scene. To restart the current scene, use:
this.scene.restart();
The Basic Scene Restart
The simplest way to restart the currently running scene is calling restart(). For example, restart a level when the player loses:
gameOver() {this.scene.restart();}
Phaser runs the scene lifecycle again:
init() -> preload() -> create() -> update()
In most cases, this means the level setup in create() runs again and creates a fresh player, enemies, score UI, and input handlers. Remember that if you were manually destroying objects, you should clean up your scene properly. Check out our guide on Destroying Game Objects in Phaser 4 to ensure you aren't leaking memory before a restart.
Restarting with Data
You can send data when restarting a scene:
this.scene.restart({level: 2,lives: 3});
Then receive it in the scene’s init() method:
init(data) {this.level = data.level ?? 1;this.lives = data.lives ?? 3;}
Resetting Variables Correctly
A scene restart only resets values that you reset during the lifecycle. Use scene properties for level-specific state so they are overwritten when the scene re-initializes.
create() {this.score = 0; // This resets every restart}
Preventing Multiple Calls
A game-over event can sometimes happen more than once. Protect your restart logic with a flag:
endGame() {if (this.isGameOver) return;this.isGameOver = true;this.physics.pause();}
FAQ
What is the difference between restart() and start() in Phaser 4?
restart() stops the current scene and restarts it from the beginning, while start() stops the current scene and switches to a completely different scene (like returning to a menu).
Does scene.restart() automatically reset all my variables?
No. It resets the scene lifecycle (init, create, etc.), which recreates scene objects. However, variables stored in external modules or global scopes will not reset. Always use scene properties (this.score) for level-specific data.
How can I pass data when restarting a scene?
You can pass an object to the restart method: this.scene.restart({ level: 2 }). This data will be available in the scene's init(data) method.
Should I call scene.restart() inside the update() loop?
No. Avoid calling restart() directly in update(), as it runs every frame. Use a guard (like an isGameOver flag) and call it from a user input event or a timer.
Conclusion
this.scene.restart() is your primary tool for creating a fluid "Retry" loop. By combining it with proper data passing in init()and state flags like isGameOver, you ensure your game remains stable across multiple playthroughs.