Restarting entire game

Hi there!
Thanks for reading.

I’m working on a pretty large-scale project (~7000 lines of code so far!), and I’m running into an annoying problem. When you die, there’s a play again button, and when you click it, it simply stops the game and gameover scenes (because game is blurred and paused in background for aesthetic reasons), and launches the menu scene again. However, I have a lot of global variables, and making a restart function to redo each variable would be time-consuming, tedious, and could introduce a lot of bugs.

How could I restart the entire game?

It should be able to stop and restart all sound effects, reset all variables to their original value, and essentially start the game afresh.

I have 9 different scenes[1].

Any advice is good!

Bread[2]
:bread:


  1. menuScene, loadScene, gameScene, shopScene, gunScene, droneScene, upgradeScene, itemScene, and gameoverScene ↩︎

  2. Did you know The word “companion” comes from Latin “com panis,” meaning “with bread”? ↩︎

Hi there!

It depends on your code structure. But here is my approach. I’m making sound manager, menu etc.. as separate scenes. Scene can be stopped in this way:

this.scene.stop(‘SceneKey’);
So, I’m flexible to switch on/off only those part of game I need.
Maybe it will work for you to stop scenes (maybe all of them) and create them again in some scene manager.

I’ve tried doing that; resetting different scenes. i’ve also tried destroying and switching. none seem to work.

1 - add log inside methods that creates and destroys scenes, for all of scenes
console.log(‘sceneName init()’)
console.log(‘sceneName preload()’)
console.log(‘sceneName create()’)
console.log(‘sceneName shutdown()’)
console.log(‘sceneName destroy()’)

That will allow you to understand in scenes are really destroyed, if scenes initiated or created. Follow logs and see in variables goes through method that you called for your game second play.
Also check if scenes are destroyed one by one and created one by one. Maybe they works in asyc mode, than it can take effect.

2 - I dont’t know where your game config is stored or you global variables. It is good if they are in one file, one class, abd it is a singlton. So, check if there is no other instance of config class/file. Must be just one instance of the config.

3 - Maybe you have game state as const parameter. If so, change it to let. Find place where you change game state. And make console.log() before and after the game state variable changing. Be sure the variable/variables really changed.

You can either stop all scenes and then restart the first or destroy and recreate the game.

Scene Watcher can help monitor scenes.

If you initialize all your global variables at once (e.g., in a function — it can be after they’re declared) it should be easy to reset them.

When I create the game, I start with the function prepareLevel(), it sets all global values to correct numbers depending on the level. And I call it every time, doesn’t matter is it a new game, restart game or anything else.
This is not advice you wanted :slight_smile: But when you do your next project, I think its a good idea :slight_smile:

thanks! (:

Can’t you just do this?

window.location.reload();

1 Like

that works perfectly, thanks!
:bread: