Why would get_node() (appear to) return Null when the node is definitely there?

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

Hi there, I’ve got a node (A). In scripting, I instance another scene and add it to A by calling add_child(). Let’s call the child B. The _enter_tree and _ready notifications are received on B (I can confirm by printing messages there).

After B is added during a run, the relevant part of the hierarchy looks like this:

  • A (KinematicBody2D)
  • Animation (AnimatedSprite)
  • CollisionShape2D
  • B (Node2D)

Later, from I want to access a sibling of this node (an AnimatedSprite named Animation) from within B, so I do this:

var parent = get_parent()  # Works -> returns "A"

# The following works. It prints A's three children, including "Animation"
for c in parent.get_children():
	print(c.name)

# None of these works...
var anim = parent.get_child(0) # returns Null
anim = parent.get_node("Animation") # also returns Null
anim = get_node("../Animation") # also returns Null

In my Remote (debugging) view, I can see all the nodes are in the tree, as described above.

Any suggestions on how I should proceed to debug/investigate further?

Thanks,
Glen.


UPDATE: It turns out it actually is working, and appears to be a debugger issue… it does return a valid AnimatedSprite object on which I can access properties and methods. However, in the debugger, the anim variable shows as Null. This makes for confusion when you’re a relatively new user, since you step through the code and it appears to be failing… When accessing other children in the same way (e.g. parent.get_node("CollisionShape2D") it does shows a “proper” returned object in the debugger).

Seems odd to me… Let’s rule out one option, though. You are stepping through code. Do you check the variable’s value when the execution is on that line, or after it has passed? The value is null until the line has been executed.

aXu_AP | 2021-10-17 19:12

Thanks, but I’m definitely looking at the value after executing the line. I’ve also seen this behaviour before, where it “appears” to return Null for certain nodes (when inspecting in the debugger with tooltip/hovering or in the variables inspector) but subsequent calls obviously are modifying the right object. It seems like an issue in the debugger.

totalgee | 2021-10-17 22:11

I was covering all the bases here, so thanks for keeping it chill :slight_smile:

I haven’t run into this situation myself, but then again, I’m not the most seasoned Godot user around. I tried to search for similiar issues at Github, but found only one comment about it. I was going to link it here, but then noticed it was written by you :smiley:

Does the problem happen only in one project or do you run into it on fresh code as well? If you can find the steps to reproduce the bug you should open an issue (maybe check 3.4 beta if it has been fixed already).

aXu_AP | 2021-10-18 07:57

BTW, I just tried to create a simple test project to repro (and possibly report) this issue, using a similar nodes and instancing setup. However, in the example project, this null variable issue in the debugger doesn’t appear…so there’s something more complicated going on with my project.

totalgee | 2021-10-18 08:08

:bust_in_silhouette: Reply From: totalgee

As I mentioned, so far I’m not able to reproduce this “null showing up in debugger when the variable holds a valid object” bug reliably on a simple scene (I still do see it from time to time in production code). So…closing this one as “answered” (by me). If I ever find reliable repro steps, I’ll log an issue on GitHub.

Thanks,
Glen.