The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

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 getcellsource_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.

Godot version 4beta11
in Engine by (15 points)
edited by

1 Answer

+1 vote
Best answer

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.

by (376 points)
selected by

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

In Area2d(which has all the movement code) get.parent().getsourceid 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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.