Area2D body entered signal not working at all?

Godot Version

v4.3

Question

Hello! I’m new to text based game engines in general.
I’m trying to make hybrid between a twin stick shooter and a vertically scrolling shooter.
Here is my code for the lazer.

extends Area2D
const SPEED = 4
var shot = false
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _physics_process(delta: float) -> void:
	position += transform.x * SPEED
	position.x += 50 * delta

func _on_visible_on_screen_notifier_2d_screen_exited() -> void:
	queue_free()

func _on_body_entered(body: Node2D) -> void:
	body.queue_free()
	queue_free()

This is my scene tree for the lazer and enemy scenes.

LazerTree 181914
EnemyTree 181940

I have already checked the collision layers & masks twice, they are correct.
The signal is connected to the script.
I have no idea what’s happening here?

What do you mean it’s not working? What do you expect to happen?

Your setup looks correct. Try putting a breakpoint at

body.queue_free()

and see if it hits the breakpoint.

I noticed you are calling queue_free on the collision body in the _on_body_entered function. What node do you expect the body to be? It might be something different then you think. It might be the StaticBody2D in your enemy node while you’re expecting the StillEnemy node.

Kinda like the post above, I’m not sure what you’re expecting to happen or what’s actually happening. This makes it hard to diagnose the problem and offer a solution.

However, from a general debugging perspective, I would try including print codes to help you figure out what is and what isn’t working. For example:

func _on_body_entered(body: Node2D) -> void:
    print(body.name + " was detected by " + self.name)
	body.queue_free()
	queue_free()

I’ll use something like this during debugging and it helps me make sure that all the things I’m 100% sure are working correctly, actually are. Are the bodies colliding in the first place? Is it the body I expected to collide and no others?

Even though the code is logically sound, there are sometimes tiny factors which can influence whether things interact correctly or not.

I’ve been programming for less than a year so maybe others can help with other debugging ideas, or you could provide us with more information to help us help you.

1 Like

I’m expecting the body the player entered to be deleted

1 Like

Oh, and the staticbody is from my efforts to fix the issue, I removed this now

1 Like

I do exactly that too (but with string formatting). Really helps debugging.

I also use call_deferred for queue freeing. It is much safer IMHO.

func _on_body_entered(body: Node2D) -> void:
    print("%s was detected by %s" % [body.name, self.name])
    body.call_deferred("queue_free")
    call_deferred("queue_free")