0 votes

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.


extends Area2D

signal kill
export (int) var enemy_speed

func _ready():

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

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

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


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():

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

func _on_SurvivalTimer_timeout():
    score += 1

 func _on_BaseArea_area_entered(area):
    $BaseArea/CollisionShape2D.disabled = true
    get_tree().call_group("EyeWorms", "queue_free")

func _on_HUD_play_game():
    score = 0
    $BaseArea/CollisionShape2D.disabled = false
in Engine by (199 points)

1 Answer

+4 votes

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

by (7,896 points)

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!}}

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

var enemy = eyeworm.instance()
enemy.connect("kill", self, "killfunc)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.