0 votes

Hello

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 'getglobalpos' 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()

else:
    pos = p_params[0].get_global_pos()
in Engine by (12 points)
recategorized by

1 Answer

+1 vote

This mistake seems to be conected by bad paths from nodes. If your "interactpos1" no de and your "interactpos2" 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.

by (341 points)
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()

Something like this

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!

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.