Detecting what's inside of an area 2d

Godot Version

4.0.4

Question

I am making a cooking pot in a game and I need to detect was is put (physically) inside of it. i am using get_bodies_entered() and I don’t know how to make an if statement to detect what has entered

Could you show your code? You could use groups

func _on_body_entered(body) -> void:
    if body.is_in_group("Rice"):
        pass
    if body.is_in_group("Potato"):
        pass
1 Like

body.name is also good:

	if body.name == ("turnip"):
1 Like

You could detect them by groups or by class_name (my preferred method)

class_name Pasta

Create a signal connecting from your Area2D/Area3D to your code.
Select the Area node → Node tab → body_entered(body: Node2D)

It will auto generate a function in your code.

func _on_body_entered(body):
  if body is Pasta:
  //Do Something
2 Likes

Just be aware the name variable contains the unique name of the node. If you have more than one instance of the object in game, Godot will auto-generate a unique name for that node.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.