Coin Collection | Help with Area2D Script

Hello!
I’m new to these forums and also new to game development in general. So please forgive me if my issue is silly!

Regarding the issue, I’m having trouble having my player collect coins. It seems like the player is ignoring collisions (even though the player and coins both have collisions).

I also organized all the coins found in the level into a 2D node (see below). Perhaps that is causing issues? Although removing the node seems to do nothing.

test level

Here is a gif of my problem below. The player phases through the coins:

coin collection gif

If anyone is willing to help a beginner out, I’d be very appreciative!
Thank you!

1 Like

on_body_entered signal does not look connected and I can’t see any manual connection through code.

Did you connect it through the signal tab in the editor? If you did, there well be an icon in front of the method to show you it is connected.

2 Likes

After checking the signal as mentioned above

if body.name == "coin" is checking if it is a coin that has entered the area, not the player.

if body.name == "player" should probably do it.

3 Likes

first of all, you need to decide who should handle the collision.

In the case of your first screenshot, it’s coin_01.gd.
Therefore the body that has entered the coin must be the player, so please change line6 of coin_01.gd to check for the player instead.
feel free to print out the body.name above the if statement in order to see if the coin is detecting anything at all.

  • I will assume that the coin is on a “coin” collision layer and the player is masking the same “coin” collision layer.
  • I will also assume that the body_entered() signal is connected correctly for the coin.

btw, even if checking for the coin name was correct, the code still would not work because duplicates of all coin objects would be numbered.
Therefore I would refactor the if statement into:
if body.name.begins_with("coin"):
more on that can be found in the documentation.

2 Likes

Your issue it not silly. We all have to start somewhere and you’ll only get better and better! :smile:

Your game looks fun and I’m looking forward to playing it on itch.io

Place a print or print_debug before the body name so you can see whether it’s triggered or not.

Maybe even print_debug (body.name)

1 Like

So as aforementioned, I’m really, REALLY, new to Gadot. I had no clue what signals even were when FreakyGoose mentioned them, so I spent a lot of time just learning about signals over the web and finding out what they do. But FreakyGoose was correct and I fixed the issue by simply connecting the signal.

This script also applies to all the coin duplicates found throughout the level, but I will keep Locher’s response in mind—especially the “if body.name.begins_with(“coin”):” statement in case I run into any issues regarding numbered duplicates.

Also, I removed any mention of the coin and player within the script. Not sure if it is best practice to incorporate a check for the player?

Thank you for all the help and support! I very much appreciate it!!!

2 Likes

It depends if anything other than the player can run into the coins. If something else can that doesn’t have a score variable then it will cause a problem.

I had that issue in one of my early projects - the enemies were stealing the coins. All part of the learning process!

I actually considered leaving that in the game. :laughing:

It’s always nice to be thorough even though it means more thought and effort. You never know if you want to add objects like a box that your player can push around. In that case, the box touching the coin can make the coin disappear as well.