I am trying to set up my game to detect when my player character (CharacterBody2D
) is moving over an NPC (AnimatedSprite2D
with Area2D
underneath it). Both have a shape etc and the layers and the masks all have the first bit set.
I have connected the OnEnter(Node2D body)
and OnExit(Node2D body)
methods to the Area2d
body_enter
and body_exit
signals respectively via the UI.
The issue is that my methods for the signals don't fire but when I print GetOverlappingBodies()
, I can see my player appearing and disappearing again.
The Signals for my Area2d, which is called NPCArea:
The node tree of NPC:
The node tree of player:
The root node is a CharacterBody2d
, which should be detected by the area.
public partial class NpcArea : Area2D
{
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
if (Input.IsActionPressed("interact"))
{
var overlap = GetOverlappingBodies();
GD.Print(overlap.Count);
foreach (var node in overlap) {
GD.Print(node.Name);
}
}
}
public void OnEnter(Node2D node)
{
Debug.Print("something entered:");
Debug.Print(node.Name);
if (node.Name.ToString().StartsWith("Player"))
{
var parent = GetParent<Npc>();
var bubble = parent.GetNode<BoxContainer>("OnEnterBubble");
bubble.Visible = true;
}
}
public void OnExit(Node2D node)
{
Debug.Print("something exited:");
Debug.Print(node.Name);
if (node.Name.ToString().StartsWith("Player"))
{
var parent = GetParent<Npc>();
var bubble = parent.GetNode<BoxContainer>("OnEnterBubble");
bubble.Visible = false;
}
}
}
And my log output after running a test (notice that the Debug.Print
is missing):
1
Layer0
1
Layer0
1
Layer0
1
Layer0
1
Layer0
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player
2
Layer0
Player