I have a problem with the game over function I created in GDScript with Godot 4.0

Godot Version

Godot 4.0


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.

1 Like

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():

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():

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
func quit():
	# back to main menu (TransitionLayer is my singleton that does fade in/out between scenes)

One question, how do I create the TransitionLayer singleton, I know how to create autoloads, but how can I assign that function to go between scenes?

Screenshot 2023-12-25 at 7.36.37 AM

extends CanvasLayer

func change_scene(target: String) -> void:
	$AnimationPlayer.play("fade to black")
	await $AnimationPlayer.animation_finished
	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.

Screenshot 2023-12-25 at 7.36.49 AM

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.