[BEGINNER] get_node returning NUL, but path is correct.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By quizzcode

Hello guys !
I’m new to godot and to pyton. I have been following many tutorials, so far so good.
This morning I encountered my first “I can’t solve this” situation.

I’m trying to instance “mob” in order to manage it simple path.
But i keep on getting “null instance on null instance”.

Here is the part of my MAIN :

extends Node2D
onready var mob = preload("res://mob.tscn") 

var mobs_remaining = 0

func _ready():
	#Level start, let's start the timer :
	mobs_remaining = 5 
	#Define how many mobs on this wave
func _on_mob_spawn_timer_timeout():
	var 	mob_instance = mob.instance()
	#define STARTING position and DESTINATION
	mob_instance.position = $start_position.position
	mob_instance.destination = $end_position.position
	#Define the path it will follow :
	var path = $nav.get_simple_path($start_position.position, $end_position.position)

Now, my path to mob.tscn is res://mob.tscn -

And here is my error feed, opened.

I think I’m probably calling the node wrongly.
I even tried : onready var mob = preload(“res://mob.tscn”) .instance() and same thing.

Any idea ? thanks guys !

:bust_in_silhouette: Reply From: Zylann

So the log says get_node fails at line 25 of main.gd. Assuming you copied the same contents, it would be mob_instance.set_path(path)… except there is no get_node there, so I assume lines don’t match. Instead it would be one of the $ node accesses in this line, am I right?:

var path = $nav.get_simple_path($start_position.position, $end_position.position)

It means either nav, start, or end_position is not a valid path. I can’t really tell much otherwise.

I noticed though, that you used instance() to create an new instance of the mob, but you are not adding it to the tree (at least in the code you posted) so it won’t appear.

onready var mob = preload(“res://mob.tscn”) .instance()

That would not solve your problem, it actually makes it more confusing. If you want to instance several mobs, don’t do it here, do it when you actually spawn them.

Hey there,
Thank you for getting back to me.
You’re right about the error line :

var path = $nav.get_simple_path($start_position.position, $end_position.position)

This is how my nodes are names and organized.!

enter image description here

So the spelling is right, the nodes are there and present.

nav/end and start _position

So it seems that all 3 path appear to be there and correctly called or am I missing something obvious ?

Is there anything I could show you to help identify the issue ?
Thank you for your time :slight_smile:

quizzcode | 2020-05-01 20:16

So it seems that all 3 path appear to be there and correctly called

No they aren’t. The actual path from main to nav is TileMap/nav, not just nav.

var path = $TileMap/nav.get_simple_path($start_position.position, $end_position.position)

Zylann | 2020-05-01 20:20

Thank you !
I did not know that the source path needed to be treated like “folder” address.
Thank you !
I will keep that in mind in the future.

All the best

quizzcode | 2020-05-01 20:44