node.cpp error?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

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

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

:bust_in_silhouette: Reply From: Zylann

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.

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

bruteforce | 2017-09-13 14:09

Well that was embarrassing. :slight_smile:

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

Robster | 2017-09-14 11:38