Can't call parent method

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

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 :)

Strange. Can you show how your scene tree looks?

Lerg-exe | 2020-08-06 14:26

You just gave me the clue I was looking for!

My scene tree looks like this:


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?

UserWaitingForGodot | 2020-08-06 15:01

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

Lerg-exe | 2020-08-07 08:27

:bust_in_silhouette: Reply From: sarthakroy

This will help.