signal not working !!!!

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

Hello Community :slight_smile:

i have a simple question why signal in this code is not working ?

i have a ship shoot laser ( area 2d) to enemy (rigidbody 2d) , the enemy must disappear and a signal sent to main node in order to give player points

the enemy disappear but the signal doesn’t connect to main node

even though i added laser as an instance child scene to main node (node 2d)

laser script :

extends Area2D
signal score
export var Laser_speed = 1000
var Laser_direction = Vector2(0,-1)

func _process(delta):
self.position += Laser_direction * Laser_speed *delta

func _on_VisibilityNotifier2D_screen_exited():
queue_free()

func _on_Laser_2_body_entered(body):
body.queue_free()
queue_free()
emit_signal(“score”)

i connected the signal to a function in main script to test it:

func _on_Laser_2_score():
print(“Success”)

but nothing happen …

:bust_in_silhouette: Reply From: FortunePilot

If I see your code correctly, in your function onLaser2bodyentered(body),
you are using queue_free() before you emit the signal. This causes the laser to get freed, and as such, will not execute code below it.

Try writing your _on_Laser_2_body_entered(body) function like this:

func _on_Laser_2_body_entered(body):
    body.queue_free()
    emit_signal("score")
    queue_free()

Thank You so Much

Zylo_X | 2020-04-21 19:31