Connect signal from instantiated scene

Godot Version

4.2

Question

I followed the current 2D tutorial to make the simple “dodge the creeps” game.
To learn and expand my skills, i added a bomb (also instantiated) with which i can kill the mobs.
All this works fine so far and only the mobs hit despawn properly.

I now want the killed Mobs to send a signal which i can listen for in my Main Scene. I do NOT want to add the Mobs as a child of the scene via editor, since a) there is many of them at the same time, b) i dont want a mob to spawn when loading the game and c) i dont think that would even help with this issue.

I have searched for a few hours now and i just cant make the connect() function work.

My mob.gd looks like this, the _on_hit_by_explosion() function is being called correctly, so the signal should be emitted.

extends RigidBody2D

signal killed

func _on_hit_by_explosion():
	killed.emit()
	queue_free()

#I deleted the other code for this question, it is equivalent to the tutorial

The important parts of my main.gd look like this:

@export var mob_scene: PackedScene

func _on_mob_timer_timeout():
	var mob = mob_scene.instantiate()
    # a bunch of initial values get declared here, deleted for this question
	add_child(mob)
    #the following line is what i am struggling with
	mob.connect("killed", self._on_mob_killed()) 

func _on_mob_killed():
	# do more things after signal was detected, e.g. increase a kill counter

#even more stuff not needed here

I tried MANY different ways of connecting the signal “killed” to the function _on_mob_killed(), but i just cant get it to work. This is just the latest attempt i made.

Any help is appreciated

You are calling the _on_mob_killed function instead of passing it.
Also, using the signal’s connect is slightly faster.
Try mob.killed.connect(_on_mob_killed) notice the lack of () after the function being passed.

1 Like

OMG I thank you so much!

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