0 votes

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 'getnode' in base 'null instance' on a null instance."
I tried get
node("../House") and getnode("/root/House") but they aren't working. But when I instanced the House scene as World's child and tried getnode("/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

in Engine by (19 points)

2 Answers

+1 vote
Best answer

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

by (305 points)
selected by

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...??

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

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().

+1 vote

enter image description here

Use debugger to find what is where

by (9,760 points)

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

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.