How to access TileMap functions from an Area2d node?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Kindavacant

I’m stupid. I made a tilemap that’s an actual map with roads. Added an Area2D node to as a child for ‘the player’ to move around the tilemap. (The tilemap is huge btw)

I need to know the tiles the ‘player’ is currently on and potential tiles they can move to. So how can I access TileMap functions like get_cell_source_id(0,Vector2i(xx,yy)) from within the Area2d node?

Feel free to ignore: I don’t understand instancing or making classes. Used to Pascal, all units can access all others as long as the particular unit was declared in the ‘Uses’ clause at the start of the current unit. If that helps. In this case I’d declare Uses Area2d, TileMap. Then in the unit assign the tilemap to the Tilemap and could use both units functions at the same time.

:bust_in_silhouette: Reply From: LeslieS

You can do this in more than one way.
To access the parent node you can use get_parent().
(Slightly) Similar to uses is adding a variable to the Area2D node var parent_node and setting that from the Tilemap node.

extends TileMap 
onready var area_node = $Area2D

func _ready(): 
    area_node.parent_node = self      

I can promise you that if you choose to ignore classes and instancing you are heading in a poor direction and will struggle mightily.
The very nodes you are using are instances of classes. It is crucial to learn it IMO.

Thank you so much for this info, wish I could buy you a beer.

In Area2d(which has all the movement code) get.parent().get_source_id works! Whoo-hoo! The other @onready var involving $Area2D also works in the TileMap script to call Area2d things like ‘position’. Couldn’t get the @onready stuff to work in Area2d to call Tilemap stuff but I’ll figure that out when more things click.

I taught myself Borland Turbo Pascal 5.1 back in Win95 days but ignored the .1 which added Object Oriented Programming as it made no sense and I was programming for DOS anyways. Windows is all OOP event based and I’m too old to just ‘get-it-quick’ anymore. Appreciate your time.

Kindavacant | 2023-01-17 00:39