When a scene is instanced through code, how can I connect its signal to another scene?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By System_Error

Hi all.

I have a scene (Enemy) that is spawned in with code on my Level scene. When it comes into contact with a player-shot projectile (LazerShot), it emits a signal, and is destroyed. Problem is, I don’t know how to have this signal {kill} connect to a child scene of Level.

Enemy.gd

extends Area2D

signal kill
export (int) var enemy_speed


func _ready():
	set_process(true)


func _process(delta):
	position -= Vector2(enemy_speed * delta, 0)



func _on_VisibilityNotifier2D_screen_exited():
	if position.x < 0:
		queue_free()


func _on_Enemy_area_shape_entered(area_id, area, area_shape, self_shape):
	if area.has_method("lazershot"):
		emit_signal("kill")
		queue_free()

Level.gd

extends Node2D

const SCREEN_HEIGHT = 580
const SCREEN_WIDTH = 1010

signal game_over
var score
var finalscore
var EnergyShot = preload ('res://player-side/EnergyShot.tscn')
var eyeworm = preload ('res://Enemy.tscn')
var SpawnWaitTime = 5

func _ready():
	randomize()


func _on_SpawnTimer_timeout():
	var enemy = eyeworm.instance()
	enemy.position = Vector2(SCREEN_WIDTH + 10, rand_range(0, SCREEN_HEIGHT))
	add_child(enemy)
	SpawnWaitTime -= 0.2
	if (SpawnWaitTime < 1.2):
		SpawnWaitTime = 1.2
	$Node/SpawnTimer.set_wait_time(SpawnWaitTime)

func _on_SurvivalTimer_timeout():
	score += 1
	$HUD.score_count(score)


 func _on_BaseArea_area_entered(area):
	$Node/SpawnTimer.stop()
	$Node/SurvivalTimer.stop()
	$HUD.player_loss() 
	emit_signal('game_over')
	$BaseArea/CollisionShape2D.disabled = true
	get_tree().call_group("EyeWorms", "queue_free")


func _on_HUD_play_game():
	score = 0
	$Node/SurvivalTimer.start()
	$Node/SpawnTimer.start() 
	$HUD.show_message("Survive!")
	$BaseArea/CollisionShape2D.disabled = false
:bust_in_silhouette: Reply From: eons

When creating the enemy:
enemy.connect("kill", target, "your_kill_callback_on_target", [params_if_any])
this will make the enemy’s signal to be connected with the target’s callback.


The 3.1 version of docs will have a section about signals

Check other questions about it too
https://forum.godotengine.org/126/is-there-any-documentation-about-using-connections

And the target would be a code block in the desired location?

{i.e. a scene instanced through the editor, not by code.}

{{EDIT: Never mind. Had a bit of help. Thanks, anyway!}}

System_Error | 2018-12-31 05:10

I’m trying to do something similar.
using the given example i do the following:

var enemy = eyeworm.instance()
enemy.connect(“kill”, self, "_kill_func)

but this only works for about 5 instances or so. spawning like 20 enemies leads to the issue that about 15 signals are emitted directly after loading the scene.

Magicman | 2021-11-02 20:41