Godot can't find the node (from another scene) even though the path is correct...??

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

So, I just wanted to make a “build” function that creates an instance of the building (given as parameter to this func. and right now its only ‘House’). I made another scene ‘House.tscn’ and a kinematicBody2D node ‘House’, but that requires its scene path to create its instance on clicking the left mouse button.So I created a variable in House’s script that stores the scene path(“res://Scenes/House.tscn”).But to get that variable in my ‘World’ script when I enter the correct node path it gives me error “Attempt to call function ‘get_node’ in base ‘null instance’ on a null instance.”
I tried get_node(“…/House”) and get_node(“/root/House”) but they aren’t working. But when I instanced the House scene as World’s child and tried get_node(“/House”) it worked. I used the get path func. in House’s script and that gave me ‘/root/House’ .
I’m new to godot (this is my first que on the forum) , so I may be doing something ridiculously wrong without knowing it.!
error in World script
House script
The complete World script

:bust_in_silhouette: Reply From: volzhs

Use debugger to find what is where

Wow i was wondering if there exists such a thing, thanks …

touqeer11 | 2017-07-26 14:49

:bust_in_silhouette: Reply From: BE-Never Games

I think I know what your problem is. See, with your current setup, the house scene is not existent in your scene tree. All scenes have a parent viewport node called “root” but they are still separate scenes wich is why your “world” scene can not find it in the tree.
In order to make it work you need to do something like this:

var house = load("res://Scenes/House.tscn") # load scene from file
var node = house.instance() #Create a new instance of the house

add_child(node) #Add the actual node to the scene tree

And it is also a pretty good idea to only load the scene ones by calling:

onready var house = preload("res://Scenes/House.tscn) #This loads the scene once at execution before the game is running

Then you can simply instance and add the node later in your “build” function.

I hope this helped you! :slight_smile:

Thanks a lot, its working now but what if i want to get variables from such a node(m thinking about having that ‘House’ scene more nodes for diff. kinds of buildings )…how will it be done…??

touqeer11 | 2017-07-26 15:36

Oh that is simple. Once you have instanced the scene as a node you have access to all of the variables on said scene!

BE-Never Games | 2017-07-26 18:53

If you export a variable as a “PackedScene” you can even select the scene file in the inspector and don’t need to call load() or preload() on it, just instance().

Warlaan | 2017-07-27 20:47

:bust_in_silhouette: Reply From: LordBoots

ran into this issue multiple times, in I passed self through the _ready():method which seems to have worked for me in most of these cases