Attempt to call function 'get_global_pos' in base 'null instance' on a null instance.

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


So, I am trying to renovate a script from Escoria point-and-click extension for Godot. It is getting position of selected node defined as an item based on its “interact pos” child node of Position2D type. It now supports only one “interact pos” node at a time.

What I am trying to achieve is to support two of these. Instead of “interact pos” it is checking for “interact pos list” node of Node2D type, which has “interact pos 1” and “interact pos 2” nodes of Position2D as children.

Those two variables are distances from player to selected node’s “interact pos”. I am comparing them and then setting pos value to the one, that is closer to player.

Unfortunately, it keeps getting error. Attempt to call function ‘get_global_pos’ in base ‘null instance’ on a null instance. Does anybody know a solution? Thanks in advance!

func interact(p_params):

var pos
if p_params[0].has_node("interact_pos"):
	pos = p_params[0].get_node("interact_pos").get_global_pos()

elif p_params[0].has_node("interact_pos_list"): #THIS IS WHERE THE ERROR COMES
	var interact_pos_1_to_player = get_node("player").get_global_pos().distance_to(p_params[0].get_node("interact_pos_1").get_global_pos())
	var interact_pos_2_to_player = get_node("player").get_global_pos().distance_to(p_params[0].get_node("interact_pos_2").get_global_pos())
	if (interact_pos_1_to_player < interact_pos_2_to_player):
		pos = p_params[0].get_node("interact_pos_1").get_global_pos()
	elif (interact_pos_2_to_player < interact_pos_1_to_player):
		pos = p_params[0].get_node("interact_pos_2").get_global_pos()

	pos = p_params[0].get_global_pos()
:bust_in_silhouette: Reply From: 807

This mistake seems to be conected by bad paths from nodes. If your “interact_pos_1” no de and your “interact_pos_2” node are children of another node maybe you should include that node in the route. Try this. If do this don’t fix bug we try other thinks.

elif p_params[0].has_node("interact_pos_list"): #THIS IS WHERE THE ERROR COMES
    var interact_pos_1_to_player = get_node("player").get_global_pos().distance_to(p_params[0].get_node("interact_pos_list/interact_pos_1").get_global_pos())
    var interact_pos_2_to_player = get_node("player").get_global_pos().distance_to(p_params[0].get_node("interact_pos_list/interact_pos_2").get_global_pos())
    if (interact_pos_1_to_player < interact_pos_2_to_player):
        pos = p_params[0].get_node("interact_pos_list/interact_pos_1").get_global_pos()
    elif (interact_pos_2_to_player < interact_pos_1_to_player):
        pos = p_params[0].get_node("interact_pos_list/interact_pos_2").get_global_pos()

807 | 2017-08-13 16:13

Something like this

807 | 2017-08-13 16:14

Works just fine! Thank you! I thought that it will automatically look for anything called “instance pos 1” or “2”, but it has to have a whole path. Good to know. Thank you!

Kubees | 2017-08-13 17:37