extends Node2D
func _on_area_2d_body_entered(body: Node) -> void:
if body is PlayerController:
print('body is PlayerController')
else:
print('body NOT a PlayerController')
altough moving the player I get into the area, the script allert me: “body NOT a PlayerController”
Code is solid so that’s not the issue. Things that come to mind are:
You’re instancing from a different scene than the one you think you are pointing to
Your script is not properly attached to the root node of the player scene
Scene tree of your character isn’t the correct hierarchy (e.g. another physics body, like a weapon or something, attached to your scene enters)
You can bugfix all by printing the body that triggers the signal. That should give you a first look at least.
Over time I’ve come to prefer adding scenes to groups to control these game logic interactions instead of relying on class names because groups are exposed to the editor, including real time bug-fixing.
There’s no code for instantiating the player here, whereas you mentioned you instantiate the player somehow.
Or is the player just included in the main scene already? If so, can you check that the player has a script attached to it and that it’s still attached during runtime?
Are you sure it’s detecting your player and not something else, like an Enemy? You could try adding the print statement to see what comes up.
func _on_area_2d_body_entered(body: Node) -> void:
print(body)
if body is PlayerController:
print('body is PlayerController')
else:
print('body NOT a PlayerController')
indeed you are right;what is printed out is:
body is: items:<TileMapLayer#58166610840>
body NOT a PlayerController
body is: terrain:<TileMapLayer#58149833623>
body NOT a PlayerController
body is: items:<TileMapLayer#58116277597>
body NOT a PlayerController
body is: terrain:<TileMapLayer#58099500384>
body NOT a PlayerController
mmm it is not clear to me is how checking what is dectected is just the player (class_name PlayerController)
Your code would work correctly, if only the area would properly detect the player. Right now, as you can see, it’s not detecting the player at all.
Check collision layers and masks on both your player and the area, as well as make sure the collision shapes are properly defined and that the area is actually touching the player (maybe the collision shape is too small and you think they should be touching, but they aren’t?)
You can turn on Visible Collision Shapes to help you debug the issues.