Code not running when the signal is emmitted

Godot Version

Godot_v4.2.2-stable_win64.exe

Question

I made a signal for restarting my game and I’ve gonnected it to the player and the mobs. I’ve done the exact same thing for both but for some reason the code never runs in the mob script.
I don’t know what pictures I should attach but here is the code for the player and mob

It looks like there are some errors in the top picture that might be the issue but I don’t really know unless you post the code for both. Also you can press the preformatted text (it looks like this </>) and past your code in it.

It will look like this
print("Its useful")
1 Like

I think I know what is causing the problem but I don’t know how to fix it
I checked and it also works with other scripts and the mob one is the only one where it doesn’t work. I think it’s because I’m creating copies of the mob while the game is running but I don’t know why it has anything to do with it
(The red dots on the screenshot are breakpoints I placed to check if the code was being run so it’s not errors)

Code for the mob:

extends CharacterBody2D

var health = 3
const SPEED = 300
@onready var player = get_node("/root/Game/Player")

func _ready():
	%Slime.play_walk()

func _physics_process(delta):
	# Make mobs move towards the player
	var direction = global_position.direction_to(player.global_position)
	velocity = direction * SPEED
	move_and_slide()

func take_damage():
	# Losing health
	health -= 1
	%Slime.play_hurt()
	# Dying
	if health == 0:
		queue_free()
		const SMOKE_SCENE = preload("res://smoke_explosion/smoke_explosion.tscn")
		var smoke = SMOKE_SCENE.instantiate()
		get_parent().add_child(smoke)
		smoke.global_position = global_position

func _on_game_over_screen_game_restart():
	queue_free()

Code for the player:

type or paste code hereextends CharacterBody2D

signal health_depleated

const DAMAGE_RATE = 5.0
var health = 100.0
@onready var happy_boo = $HappyBoo

func _physics_process(delta):
	# Scaracter movement
	var direction = Input.get_vector("move_left", "move_right", "move_up", "move_down")
	velocity = direction * 600
	move_and_slide()
	
	# Walk animation player
	if velocity.length() > 0.0:
		happy_boo.play_walk_animation()
	else:
		happy_boo.play_idle_animation()
	
	# Dying
	var overlapping_mobs = %HurtBox.get_overlapping_bodies()
	if overlapping_mobs.size() > 0:
		health -= DAMAGE_RATE * overlapping_mobs.size() * delta
		%ProgressBar.value = health
		if health <= 0.0:
			health_depleated.emit()


func _on_game_over_screen_game_restart():
	health = 100.0

Does the queue free work when the mob dies. If it does something is going on with the signal.

Yes it works when shooting the mob just not when restarting the game

My guess is a signal is misconnected. Showing us how signals are connected might help. Also, since you’re instantiating dynamically, maybe connect the signals through script? That might give you a clearer idea of which signals are connected to what.

1 Like

Thanks, how do I do it through code though?

You’ll find that info toward the very end of the Using Signals documentation.

1 Like

it finally works thank you so much for helping

1 Like

That’s great!