How to prevent game over function from being called twice

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Titox

So I had a node with area_2d enemies. When a player hits any of the enemies, the on_enemy_area_entered signal calls the game over function which adds the coins collected during that game instance to the global coin variable and saves it. Nice and dandy…except if the player hits both at the same time. When that happens, the signal calls the game_over function twice and therefore the player sees double of his real score in the main menu…Due to the type of game it is I cannot prevent the player from hitting two enemies at the same time

What can I do

:bust_in_silhouette: Reply From: EnderCreeper1st

You could make like a pause menu, but instead it’ll be your game over scene.
Link to GDQuest’s video:

Alter the buttons and the “pop-up menu” to fit your Game Over Menu, that should help.

:bust_in_silhouette: Reply From: Adam_S

Make a variable and check its value before calling your game_over function.
For example:

var dead = false

on_enemy_area_entered():
    if dead == false:
        dead = true
        game_over()
:bust_in_silhouette: Reply From: Zylann

In the script that has the game_over function, add a boolean member variable to remember what state the game is. So if game_over is called a second time, you can ignore the second call.

var game_is_over = false

func game_over():
	if not game_is_over:
		game_is_over = true
		# Execute game over logic

Don’t forget to reset the variable when starting a new game (you won’t need to if you re-instance that scene, depends on your game).