Signal doesnt always received

Godot Version

v4.1.1.stable.official [bd6af8e0e]

Question

Hi all, so i was learning to make games from this gdquest video https://www.youtube.com/watch?v=GwCiGixlqiU and im trying to modify a bit by making the enemies spawn in waves, but the enemy remaining counter doesnt work bcs the signal doesnt always received

here’s the game code

extends Node2D

var wave_spawn=10
var enemies_remaining=0


func _physics_process(delta):
	if Input.is_action_pressed("ui_cancel"):
		$Pause.visible=true
		get_tree().paused=true

func spawn():
	if wave_spawn>0:
		wave_spawn-=1
		enemies_remaining+=1
		var new_mob = preload("res://mob.tscn").instantiate()
		%PathFollow2D.progress_ratio = randf()
		new_mob.global_position=%PathFollow2D.global_position
		add_child(new_mob)

func _on_timer_timeout():
	spawn()

func _on_character_body_2d_health_depleted():
	$Gameover.visible=true
	get_tree().paused=true

func _on_button_pressed():
	$Pause.visible=false
	get_tree().paused=false

func _on_mob_enemydead():
	enemies_remaining-=1
	print("working")
	print(enemies_remaining)

and this is the mob code

extends CharacterBody2D

@onready var player= get_node("/root/Game/CharacterBody2D")
signal enemydead

func _ready():
	$AnimatedSprite2D.play("walk")
	

var health=3
func _physics_process(delta):
	var direction = global_position.direction_to(player.global_position)
	velocity= direction * 100
	move_and_slide()
	
func take_damage():
	health-=1
	$AnimatedSprite2D.play("hurt")
	if health<=0:
		enemydead.emit()
		print("emit")
		queue_free()

this is the example of the output
image

i dont see you connect the enemydead signal here when instantiated the enemy

try change spawn function to:

func spawn():
	if wave_spawn>0:
		wave_spawn-=1
		enemies_remaining+=1
		var new_mob = preload("res://mob.tscn").instantiate()
		new_mob.enemydead.connect(_on_mob_enemydead)
		%PathFollow2D.progress_ratio = randf()
		new_mob.global_position=%PathFollow2D.global_position
		add_child(new_mob)
1 Like

Where is take_damage called? Maybe put your project up on gitlab or something and we can play with it.

Thx a lot this works

1 Like

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