"_on_player_detection_body_entered" not triggering
Asked Answered
L

7

0

Following this tutorial:

After setting up Area2D and CollisionShape2D to detect a player coming near a sprite, the tutorial writes this signal as follows:
func _on_player_detection_body_entered(body)
if body.name == "Player":
print("Player")

I'm writing the scripts in C#, so my version of the function looks like this:
private void _on_player_detection_body_entered(Node2D body)
{
// Replace with function body.
Console.WriteLine("hi");
Console.WriteLine(body.Name);
}

However the function doesn't trigger when the player approaches the sprite. Furthermore, I'm getting the warning: "Missing connected method 'on_player_detection_body_entered' for signal 'body_entered' from node 'Frog/PlayerDetection' to node 'Frog'."
I've tried changing the function name to "
OnPlayerDetectionBodyEntered(Node2D body)", but that only results in the debugger throwing errors about not finding '_on_player_detection_body_entered'.

What am I doing wrong?

Lorineloriner answered 22/10, 2023 at 7:17 Comment(0)
A
0

Did you set your collision layer and mask?

Antipersonnel answered 22/10, 2023 at 7:36 Comment(0)
L
0

I would have to see more of the code to understand fully, but it makes sense that the debugger would throw the error '_on_player_detection_body_entered' because that's what it's expecting to find.

I would double check that the signal is connected properly, maybe disconnect & reconnect it & generally review the steps just before that in the tutorial.

I very much want to try this tutorial now, thanks for posting about it.

Lemkul answered 22/10, 2023 at 7:45 Comment(0)
L
0

Okay I just got the same symptom while doing the first tutorial in C#. All the messages like that vanished when I put all the class and scene names in lowercase. So like "Player" became "player", etc... otherwise it wouldn't work. I assume the lowercase version is stored somewhere. For the time being I think I'll redo it with the gdscript version.

Lungki answered 24/10, 2023 at 20:18 Comment(0)
R
0

Make sure you are using a CharacterBody for the player. without it, it won’t detect as a body. also, you might find if body.is_in_group() useful in this situation also.

Riggins answered 25/10, 2023 at 0:56 Comment(0)
L
0

I started over that section of the tutorial twice, once with C# and another with GDscript. The problems with C# persisted, but it works just fine with GDscript despite following the same steps, so for the sake of progressing on this tutorial I'll consider this problem solved for now.

Maybe it has to do with the signal being generated outside of the Frog class in the C# file? I didn't have issues connecting a signal to the play/quit buttons, though. Mercalys brought up an interesting suggestion with the lowercase names -- I'll have to go back and try that later. Come to think of it I think "Frog" was my only class where the scene/script filenames started with an uppercase letter.

Lorineloriner answered 25/10, 2023 at 5:7 Comment(0)
B
0

Lorineloriner may have just been the string/name, like you said. It looks for exact strings for names when you reference nodes so that capital letter may have been the culprit.

Brieta answered 26/10, 2023 at 20:35 Comment(0)
B
0

Lorineloriner
Hi.
I dont think you still have this Problem but i had it. After looking up the code again. I noticed that the Player "Root" (where you define the Player CollisionShape and so on was correct named, somehow in the World it had a different name and thats why it didnt worked out.
And for the other guys maybe got a Problem aswell the group solution is very good:
Sign ur player into a group (click on player node->groups)
then call it:
func _on_player_detection_body_entered(body):
if body.is_in_group("Player"):
print("test")

Have fun 🙂

Binal answered 30/12, 2023 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.