+1 vote

Hi all,

Godot OSX 2.1.4 Stable Official

I'm trying to load an instance like so:

func _on_ButtonIntro_pressed():
    var nodeToLoad = preload("res://test.tscn")

and I'm getting the following errors:

enter image description here
enter image description here

Can anyone offer advice as to what this might be? Right now I'm stalled :-(

By the way, I can run that test.tscn by itself, just not instanced from my main game page.
Line 158 as shown in the error is the get_parent().add_child(nodeToLoad) line above.

I've looked here https://github.com/godotengine/godot/blob/master/scene/main/node.cpp#L704 and I have NO idea what that stuff means :-)

in Engine by (838 points)
edited by

1 Answer

+4 votes
Best answer

You didn't instance the node.
preload gives you a Resource, in this case it's a PackedScene resource. You must use .instance() to create an instance of that scene, and only then you can add it to the tree:

func _on_ButtonIntro_pressed():
    var res = preload("res://test.tscn")
    var nodeToLoad = res.instance()

FYI, it says the p_child parameter is null because the engine fails to cast your resource to a node, because nodes aren't resources.

by (29,120 points)
selected by

The error message is misleading :/, it could also say that the parameter (type) is "illegal"... or something.

Well that was embarrassing. :-)

Thanks so much. I can't believe I didn't see that. I got wrapped up in the error.

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 Frequently asked questions and 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 [email protected] with your username.