Passing object to variables causes NIL when code is run

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

(Sorry in advance if some of my terminology is weird/wrong. I’m self-taught. lol)

I had been working on this project for a while and everything was running relatively smoothly. I created a little script for running cutscenes in my game using sprites nested in a Node2D.
However… I’d been having trouble with Java on my computer, so when an update came out, I jumped on it, and when I booted up godot, the next time, my project gave me an error every time I tried to build it (i think the project file itself became corrupted), but the assets remained unchanged, so I decided to simply transfer them (the art, scenes, and scripts) to a new project.

Basically everything else works the same except for the script that handles the cutscenes, and I have no idea why.

There are a lot of objects that I reference through variables in this script, and it seems that when I use the variables to reference them, it no longer works. I have to use the $[relative_path] every time I need to do something and… frankly, it’s a pain.

The gist of it is that I have several sprites packed as scenes that I load dynamically into the cutscene. I load them all into a node2D called “ToyBox” that I can use to do simple animations (moving the whole stage at once, shaking the sprites, etc).

onready var toybox = get_node("ToyBox")

func load_toy(sprite:String): #loads a sprite using an absolute filepath
	var ts = load(sprite)
	var talksprite = ts.instance()
	toybox.add_child(talksprite)
	return talksprite #returns it so I can pass it to another variable elsewhere

Basically, it’s supposed to load a sprite, instance it, and make it a child of the toybox. However, when I run this code, it gives me the error “Invalid call. Nonexistent function ‘add_child’ in base ‘NIL’.”
I don’t know if I’m just passing the object to the variable incorrectly or what, but I’ve also tried doing it in the _ready function to the same result:

var toybox

func _ready():
	toybox = get_node("ToyBox")

load_toy() only works for me if i change “toybox.add_child()” to “$ToyBox.add_child()” (and it’s the same for the other nodes that I reference through variables as well).
When I run:

print(toybox,$ToyBox)

it returns the same object id tag for both:

[Node2D:1314][Node2D:1314]

so I think it must be set up correctly…

The only thing I can think of is that it’s running the code before it loads properly, so before the variable is set, but even when I change it to $ToyBox, there’s another similar piece of code that runs later in the scene (like, many seconds later) that gives me the same error.

Also, all of this was working properly (as far as I could tell) before I moved this stuff to the new project…

I’m using Godot 3.2.1 on windows10.

I think you should add some breakpoints and check the debugger to see the exact values of the variables. Could help.

exuin | 2021-05-14 16:55

Ah, thanks for suggesting. It hasn’t solved my problem, but I’d been avoiding actually figuring out how the debugger works until now, so this will probably make my life at least a little bit easier.

funwalker | 2021-05-15 01:47