1
votes

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

1
To make it short, have the object that is responsible for game_over() have a boolean variable called something like has_game_ended. At that top of game_over() check has_game_ended. If it's true return from game_over(). - hola
when the player is hit, disable the collider (you can also disconnect the signal) and "kill" the player, then init a new player scene - svarog

1 Answers

1
votes

Maybe you should try something like this:

var is_game_over = false

func game_over():
    if not is_game_over:
        # your code to add coins etc.
        is_game_over = true

First, game_over() will check if the game's already over (is_game_over variable), if it's true, it'll do the work and in the end it will set the is_game_over variable to true to know that game is already over; and when your player hits the second enemy, the game_over() function will know that the game is over and won't add coins again.