How to get reference to the Node collided with (2D)

Godot Version

4.2.2

Question

Hello!

I’m making a top-down, tile based 2D game a la RPG Maker. There is a TileMap, Player (Area2D), and several types of Event nodes (all Area2D, examples such as NPC, sign, door, chest, etc.)

So when the Player collides with an Event (in this example, a door) I want to execute specific code on the Player script. For this it seems right to use “On_Area_Entered” and each node has its own CollisionShape2D. So then the Player moves onto an Event tile, it receives the signal and a reference to the specific Area2D that it collided with. Awesome!

Now then, I added custom code to an Event - I created an Event node called “Door” that extends Area2D and has a variable called “locked” which is returned with a function called “CheckIfLocked()”

But I can’t access that function! I can get the “Door” node but then it says “Nonexistent function of base Area2D”. Weird, because if I print the node that area_entered returns, it doesn’t give me a generic base class, it gives me THAT specific Door.

Any idea what I’m doing wrong?

Thank you!

Could you share a screenshot of maybe your scene tree, the door script, and the colliding _on_area_entered player function?

Is the CheckIfLocked() function from a child of the Event?

Here is it.

And just for the heck of it, I tried type casting “other” as Door, which returned null (makes sense but sometimes this worked for derived classes in Unity). So it seems weird like somehow the print function knows the reference is “Door” but the type returned is just an Area2D generic base class?

I notice you also have a Event.gd are you sure the door has that Door.gd script? That is printing Door:<Area2D####> because it is named Door.

Yes, Door has attached the Door.gd script.

The Event script was leftover from my original plan which was to have a class called “Event” from which derives different types like Door, Chest, NPC, etc. That way when the player hits spacebar, they can call a generic “interact” function which is implemented differently based on the Event type (eg. Door teleports player, NPC shows text, and so on)

Anyway, I deleted the Event script just to make sure it was not interfering and there’s not change.

UPDATE:

Ended up restructuring everything to circumvent this problem.

Instead of using “area_entered”, etc. I gave the TileMap a Dictionary of all MapEvents, so when the player presses spacebar, it asks TileMap to check if the player is currently standing on a MapEvent tile, and if so, run MapEvent.Interact()

Works well! But perhaps signals that I don’t understand the Godot editor very well.

1 Like