0 votes

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")
    get_parent().add_child(nodeToLoad)

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

asked Sep 13 in Engine by Robster (626 points)
edited Sep 13 by Robster

1 Answer

+3 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()
    get_parent().add_child(nodeToLoad)

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.

answered Sep 13 by Zylann (10,122 points)
selected Sep 14 by Robster

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 How to use this Q&A? before posting your first questions.