• Godot Help
  • how to you use godot's body_entered and body_exited signals

I'm fairly new to Godot and have been having problems with signals. I am trying to create an extremely simple interaction system where if the player character is in the Area 2D of the object it is trying to interact with, a label for the action key will show up, letting the player know that the object could be interacted with. I've been trying for hours to make this work but just can't figure it out. At this point I'm getting 2 errors, with one (Invalid set index 'visible' (on base: 'null instance') with value of type 'bool') and the other being (E 0:00:00:0783 InteractUi.gd:4 @ _ready(): Node not found: "InputUi" (relative to "/root/Chapter 1/terminal 1/Sprite2D/Area2D"). Even without including the UI part, the signals themselves simply don't work. here is my code.

`extends Area2D

@onready var InputUi = $InputUi

func _ready() -> void:
connect("body_entered", EnterCheck)
connect("body_exited", ExitCheck)
InputUi.visible = false

func EnterCheck(body) -> void:
InputUi.visible = true
print("on")

func ExitCheck(body) -> void:
InputUi.visible = false
print("off")`

    what I do is to create a variable with a boolean value and tell it that when it enters the area its value is true and when it leaves it is false.

    stimpus1 (E 0:00:00:0783 InteractUi.gd:4 @ _ready(): Node not found: "InputUi" (relative to "/root/Chapter 1/terminal 1/Sprite2D/Area2D")

    $ only gets children and needs a precise path to the Node you're trying to get. Is InputUi an immediate child of Area2D? Look at the page on NodePath for examples of how to format the path after $

    As for your Enter and Exit funcs not firing, check and make sure that the Area2D is able to collide with your player in its collision mask.

    stimpus1 Why do you connect signals in the _ready function instead of doing that in the Signals tab of the Inspector panel? It's more straightforward and less prone to errors.

      Or at least use body_entered.connect(EnterCheck), which uses no strings.

      update: I managed to get it working correctly, however, I'm definitely going to read up on the manual for signals since I clearly don't understand them. I originally worked in Unity, however, after the drama I moved my project to Godot 4 so I'm still fairly new.

      Fencer this is true (if you know what you are doing) but connecting in code is a good thing to know how to do.

      Let's say you have 20 signals to connect from objects. You could, by hand, connect each one of them and spend a lot of time doing it. But if they're all doing something similar, you could just connect it in code and save yourself a lot of time. Or, think of it like this: if you could make a robot mow your lawn with a command versus pushing the mower yourself, you surely would, right?

      There are also instances where objects might not be in a scene at _ready() (say a projectile you fire) and you have to connect them in code. Therefore, you connect them when they enter the tree.