Godot Version
Godot 4.3
Question
To keep a kill score in my game I have my goblin enemie emit a signal whenever he dies. This signal is emitted to an autoplay EventBus and then used in my world scene to increase the score. However when a goblin dies the signal is emitted multiple times making the function to increase the score run multiple times.
EventBus
signal dead(points)
Goblin enemy
func take_damage():
hit.play("hit")
hp -= 1
if hp == 0:
queue_free()
EventBus.emit_signal("dead", 1)
const blood_splatter = preload("res://Utility/blood.tscn")
var blood = blood_splatter.instantiate()
get_parent().add_child(blood)
blood.global_position = global_position
World
@export var wave: Array[Wave_Info] = []
var time = 0
var score = 0
func _ready():
EventBus.dead.connect(increase_counter)
func _on_timer_timeout() -> void:
%PathFollow2D.progress_ratio = randf()
time += 1
var enemy_spawns = wave
for i in enemy_spawns:
if time >= i.time_start and time <= i.time_end:
if i.spawn_delay_counter < i.enemy_spawn_delay:
i.spawn_delay_counter += 1
else:
i.spawn_delay_counter = 0
var new_enemy = load(str(i.goblin.resource_path))
var counter = 0
while counter <= i.goblin_num:
var enemy_spawn = new_enemy.instantiate()
enemy_spawn.global_position = %PathFollow2D.global_position
add_child(enemy_spawn)
counter += 1
func increase_counter(points):
score += points
print(score)
My guess is that it has something to do with the fact that there are multiple enemys on screen so the signal is emitted for all of them but i have no clue how i would fix that.