![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
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 baseNil
.
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:
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?
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