Restarting game

Godot Version

4.2

Question

How would you go reset your main game scene once the game is over? The game loop once the game is over is: main game → menu (then play again) → main game. How would you do this so that you reset the game scene but also go back to the menu?

1 Like

Create the UI inside a canvas layer, you can use button for it, and here is two code line that can help you:

Restart the current scene:

get_tree().reload_current_scene()

Change scene to main menu or any other level:

get_tree().change_scene_to_file(preload("path/to/your/scene.tscn"))

You can connect the pressed signal of the button.

1 Like

This is the code I put:

func _on_button_pressed():
	get_tree().reload_current_scene()	
	get_tree().change_scene_to_file("res://menu.tscn")

But the game just freezes

Also when I put preload in front of the tscn I got an error

Try to reload the current scene only, not to change. Is it still freezes?


Game just freezes like that, no player, no map, no enemy, etc

Do like this:

func _on_button_pressed(): 
    get_tree().change_scene_to_file("res://menu.tscn")

I read your topic properly, you want to add a button for replay the main game in the menu scene.
Same change scene to the main game when the try again button pressed.

That works, but I need to reset the main game scene as well, because once I go back to the menu and play again, the same screenshot I sent before happens

1 Like

Do you added the player in the main scene normally? Or is it related to Global or Autoload.

My game map is procedurally generated, I think my player is added somewhat normally, where the tilemap sends the spawn coords to the player.

What does global and autoload mean? How can I check

In the project settings, I not think you are using a Global script. Anyway, I think it is a problem with spawning codes not changing or resetting scenes. So check everything properly?

not thing? sorry is this noticed?

Also, does ‘reload_current_scene()’ reset everything, or just the main scene? Would I need to reset the player scene, enemy scene, etc that are child nodes of that main scene?

Sorry I means “think”, actually I am typing in mobile as I am out of home.

‘reload_current_scene()’ changes everything in the current or main scene. Otherwise if you want to change to menu when game is over and back to main scene when try again, then you can just use the change scene codes.

For some the ‘reload_current_scene()’ isn’t reloading everything, mainly the map I don’t think

I think you also need reset global state manually, like score = 0, health = 100, etc.

I’m using C# with Godot and was having similar issues when trying to reset my main scene. One issue I found was that signals were still connected to “old” nodes that had already been removed from the scene tree. After finding those problem nodes, I’m disconnecting signals in code as they exit the scene tree. Hope that helps.