Fooling around with Godot after a long absence, to remind myself of some of its functionality, I created simple bunch of nodes and some scripts. In one of my scripts I was trying to access a node with get_node('/root/Game/Grid/Block0')
. It seemed so simple, but it would just not work. I fiddled around with get_tree().get_root()
, and various get_node()
and get_child()
combinations, but I just kept getting null returns.
So I eventually decided to just print out the entire node tree at that point in my code, to see I was missing something. I immediately noticed that there was a mysterious /root/@Game@2
path as well as /root/Game
. I then realised that I had a script name "Game" (Game.gd) at the top level, as well as my main scene's base Node2D named "Game". So it seems that Godot renamed one of them internally behind the scenes, and it happened to the the one with no children (the script). Good to know, but terribly confusing for a while. I have renamed my Node2D to something more unique and less generic, and the node path lookup works as expected now.
So anyhow if anyone else wants to quickly dump their node tree to debug output to see what if anything is lurking in there that you can't see in the gui, this little recursive function will do it:
func debug_print_tree(depth, node):
print ("%*s %s %s" % [depth * 2, "", node, node.name])
for c in node.get_children():
debug_print_tree(depth+1, c)
Just call it something like this: debug_print_tree(0, get_tree().get_root())