0 votes

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

Godot version v3.3.4.stable.official [faf3f883d]
in Engine by (54 points)
retagged by

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.

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.

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

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

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

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.

1 Answer

0 votes

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.

by (54 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.