I am currently working on a project that uses tilemaps to create a background on to which I have various Node2D based sprites that navigate the cells of the tilemap in a grid based fashion.
So for the grid scene the root node is just of type Node and I have a tilemap node whose children are the Node2D nodes that represent characters (arranged as instanced scenes). The Node2D sprite scenes have a Node2D node as the root node with either Area2D or KinematicBody2D as children (and also associated collisions and sprites as children). The root node extends Node2D.
To achieve grid based movement I use the Tilemap class functions
map_to_world but I require their usage in the instanced Node2D scenes, so I have created a variable
Grid using the
onready keyword that is set to
get_parent() and then I call the parent functions from the Node2D scenes using the dot operator -
This method works fine when the root node script for the Node2D scene has it's parent as the Tilemap node, but I also have a Node2D scene where the root node is a Node2D and the children are Node2D nodes that represent different enemy characters.
So I created a script in the root node that uses the
get_parent() method from before and in the child node scripts I tried using
onready var Grid = get_parent().Grid to retrieve the
Grid variable from the root script. But this does not seem to work and in the child nodes it seems the
Grid variable is
Null. So I worked around this by using
var Grid = get_parent().Grid in
_ready() following a slight delay and this works. However, I am thinking I am definitely over-complicating things and I think there is a cleaner solution here?
Tilemap # Parent
Node2D # Child of Tilemap, "interface" script here.
Node2D # Child of Node2D, functional scripts are here
I come from a C and ASM background and relatively new to OOP and game dev, really enjoying Godot but I want to make sure I make correct use of the excellent Node based design!