Emitting a signal to a especific instance in a scene

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

I’m making a game for a Game Jam that’s going on here in Brazil, and the theme is “Towers”. So my game is based in many “cake pieces” falling from sky, while the player needs to catch then, and make a “cake tower”.

But when the player catch a cake piece, all the cake pieces at the screen are deleted (via queue_free).

(I have not yet done the mechanics of counting how many cake pieces the player picked up)

Someone can help me to delete just that one piece that the player caught ?

code of the World Scene:

extends Node2D

var cake_piece = preload("res://Cake/CakePiece.tscn")
var Cake_piece =  null

signal cake_col

func _ready():
	$Timer.start(1.5)
	

func _on_Timer_timeout():
	Cake_piece = cake_piece.instance()
	get_parent().add_child(Cake_piece)
	Cake_piece.position.x = 384
	Cake_piece.position.y = 0
	connect("cake_col", Cake_piece, "on_World_cake_col")


func _on_Player_cake_collected():
	emit_signal("cake_col")

code of the player scene:

 extends KinematicBody2D

const UP = Vector2(0,-1)

var velocity = Vector2.ZERO
var move_speed = 5 * 96
var gravity = 1200
var jump_force = 600

signal cake_collected

func _physics_process(delta):
	get_input()
	velocity.y += gravity * delta
	velocity = move_and_slide_with_snap(velocity,Vector2.ZERO, UP)
	

func _input(event):
	if event.is_action_pressed("ui_up") and self.is_on_floor():
		velocity.y = -jump_force
func get_input():
	var move_direction = - int(Input.get_action_strength("ui_left")) + int(Input.get_action_strength("ui_right"))
	velocity.x = lerp(velocity.x, move_speed * move_direction, 0.2)
	


func _on_Area2D_area_entered(area):
	emit_signal("cake_collected")

code of the Cake Piece scene:

extends KinematicBody2D

const UP = Vector2(0,-1)
var gravity = 12000
var velocity = Vector2.ZERO

func _physics_process(delta):
	velocity.y = gravity * delta
	
	velocity = move_and_slide(velocity, UP)

func on_World_cake_col():
	queue_free()

The World scene: ( I’m using a 1368x720 monitor, so the print quality is not that good)

(The Godot Logo most at right side is the player, the other 2 are cake pieces)

:bust_in_silhouette: Reply From: plasmid

Maybe try having the player’s function be

func _on_Area2D_area_entered(area):
    area.queue_free()
    emit_signal("cake_collected")

so the player frees just the area that triggered the “area entered”, and don’t give the cake’s code any instructions to handle the collision or signal? I admit that I’m a little surprised that it was working at all – it looks like the cake is a kinematic body so I would have expected it to be detected with a “body entered” instead of “area entered”. Depending on how it’s set up, if you have a child Area node within the cake KinematicBody, if you want to queue free the KinematicBody then you might need to use queue free on the area’s parent (or grandparent, or however deep the Area node is nested within the KinematicBody) instead with something like

area.get_parent.queue_free()

Oh yeh, how did I forgot of body_entered ?? kkk. I messed up everything with this area_entered.

Thanks for the awnser, I would probably discover this like, 2 days and a lot of foruns/videos later

Ps: I’m not racist, im Brazil we laugh with k (normaly kk, or kkk, or maybe KKKKKKKKKKKKKKKKKKKKKKKKKKKKKK, if the meme, or anything like that was breathtaking), and I don’t know how to laugh in english

O_Chernobas | 2020-12-27 20:13