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.

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
in Engine by (197 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
http://docs.godotengine.org/en/latest/getting_started/step_by_step/signals.html

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

by (7,860 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!}}

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 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.