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.

godotengine.org | Twitter

0 votes

I want to create a class called Pawn that is always a child of a TileMap. Inside this class I want to create a variable called map_pos which is very handy to store the tile on which the node is standing. However, when I try to get the parent node, I can't call any method of the TileMap class, throwing an error when getting the map_pos. It says:

Invalid call. Nonexistent function world_to_map on base Nil.

Complete code:

extends Node2D
class_name Pawn

var map_pos: Vector2 setget, get_map_pos

onready var grid: TileMap = get_parent()


func get_map_pos() -> Vector2:
    return grid.world_to_map(position)

Thank you very much for reading it all, any bit of help is appreciated :)

in Engine by (28 points)

Strange. Can you show how your scene tree looks?

You just gave me the clue I was looking for!

My scene tree looks like this:

Game
Camera2D
TileMap
    Player
    Mobs
        Mob1
        Mob2

Because my Mobs are also instances of Pawn they try to access their parent which is an empty Node2D (for organizational purposes) and not its grandparent. For now I'm going to use an absolute path instead of get_parent(), although, is there another way?

well you can use get_parent() on your parent but that is a bit odd

1 Answer

0 votes
by (113 points)
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.