Search code examples
godotgdscript

How would I check if my player is colliding with my collectable in Godot?


I'm writing a game in godot, and I can't figure out how to make my collectable be collected. All I want to do is have a script inside of my coin, that checks if my player is colliding with it. If it does, it will dissapear. I can't figure out how to do it.
This is the structure of my collectable: Coin
|- Sprite
|_ Area2D \ CollisionShape2D

I already tried doing an func _on_area_entered(), but that didn't work. I also tried a func _on_body_entered(), but that also didn't work.


Solution

  • Signal connections in Godot are not declarative.

    If you are simply copying the code from some tutorial (or generated by some large language model), it is not going to work.

    Creating a func with a name that looks like it was generated to connect to a signal, does not connect it to a signal. You need to actually connect the signal... And as far as connecting signals go, the name can be whatever.

    The documentation explains it on Using Signals.


    Since a link only answer is not considered a good answer, I'll write my own short version of what you have to do anyway... First, I shall point out there are two methods to connect signals: from the editor and from code. I'll just cover doing it frm the editor in this answer.

    First you need two things:

    • A Node with a script attached.
    • A Node that emits the signal you want.

    Then to connect signals from the editor, select the object that emits the signal (the Area2D in this case), go to the "Node" dock (it is on the right by default). On the Signals tab select the signal you want:

    • "body entered" if what you want to detect is a physics body.
    • "area entered" if it is an area.
    • If it is something else, then that's the problem. Make it one of those. If you don't know what, I have an answer on what to use for the common cases elsewhere.

    Then you can either select "connect" from the context menu, or double click it, or click the "connect" button at the bottom.

    And Godot will a dialog window where you pick what you are going to conect it to. Pick the Node with the script attached, on the bottom of the window you can write the name for the method (Godot will generate one for you, for example _on_body_entered, but you can change that).

    There is also "advanced options" that I will not go into...

    And finally you click "connect".

    If there isn't a method with the name you said, Godot will generate one for you, and connect the signal to it.

    In the script editor you can tell a method is connected because by this method there will be a green icon next to it (i.e. if the connection is done from code you will not see an icon).


    You might also be interested in How do I detect collisions in Godot? where I cover - as far as I can tell - every case.


    You might do that and find that it is still not working. Try this check list:

    • See if you got that green icon that indicates that the method is connected.
    • Make sure the Node with the signal does not have monitoring disabled.
    • Make sure the collision_mask of Node with the signal overlaps with the collision_layer of what you want it to detect.
    • Enable "Visible Collision Shapes" from the debug menu, so you can see the colliders while debugging your game, and check if they are positioned correctly and actually touching.
    • Use either a breakpoint or a print statement to see if the method is being called. You can also try printing (or inspecting in the debugger panel) the body from the signal to see if it is what you expect.