I had a problem, I need to create a global game over, that is to say that this game over detects the level in which it was executed and when pressing some key it returns to that level of the game, someone can give me a hand with the code please? or at least give me an idea of how to write the code, thank you very much.
I am new so someone else can chime in if there is a better solution but I think you can utilize an autoloader to track game-wide variables that can be updated from any relevant scene and you can use signals to send updates to and from that if your character advances to a new level or needs to reset the current one.
A video to show how to use an autoload singleton. This is a high level overview without any code examples but just searching godot autoload or singleton might help you manage this data.
All of your levels should be inheriting from (i.e. extending) same base Level scene. After you create your base Level scene, make sure to create other levels using menu item Scene → New Inherited Scene… - just writing “extends Level” in new scene’s script won’t work.
Now create another scene, root is CanvasLayer. Call it LevelUI. Add any UI components there, add it as child to your base Level class.
Now create another scene, root is just Control, and call it GameOverScreen or smth. Add it as child scene to LevelUI. Now set it’s Process mode to Disabled coz you don’t need it to run while game is happening.
In LevelUI let’s say you reference the game over screen with this variable:
@onready var game_over_menu = %GameOverScreen
Whenever your player dies (or w/e other event leads to “game over”), just tell the GameOverScene to show itself. In my current code, I have this func inside Level:
@onready var levelui = $LevelUI
func _on_player_died():
levelui.game_over_menu.display()
And the code inside GameOverScreen:
extends CanvasLayer
class_name GameOverScreen
@onready var quit_button: Button = %QuitButton
@onready var restart_button: Button = %RestartButton
# Called when the node enters the scene tree for the first time.
func _ready():
quit_button.pressed.connect(quit)
restart_button.pressed.connect(restart)
func display():
# show game over screen
self.visible = true
self.process_mode = Node.PROCESS_MODE_WHEN_PAUSED
# pause the game
get_tree().paused = true
# stop background music, play game over sound, or do w/e else you need to do
## player wants to play again / restart the level
func restart():
# hide game over screen
get_tree().paused = false
self.visible = false
self.process_mode = Node.PROCESS_MODE_DISABLED
# since game over screen is part of base Level scene, it will restart w/e child level is loaded
get_tree().reload_current_scene()
func quit():
# back to main menu (TransitionLayer is my singleton that does fade in/out between scenes)
TransitionLayer.change_scene("res://scenes/ui/menus/main_menu_screen.tscn")
extends CanvasLayer
func change_scene(target: String) -> void:
$AnimationPlayer.play("fade to black")
await $AnimationPlayer.animation_finished
get_tree().change_scene_to_file(target)
get_tree().paused = false #unpause if it was paused
$AnimationPlayer.play_backwards("fade to black")
AnimationPlayer just modulates the full-screen colorrect from 0-alpha black to 255-alpha black.