Attempt to call function 'get_global_position' 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 ItzKillerTTV

Hello everyone. I recently moved all the code of a weapon inside the player script because I want it to interact with rng and I don’t know how to do it otherwise. I have a bow that shoots where the mouse is at, I use a get_global_position. The thing is, now that I’ve changed everything, when I try to shoot the project crashes with the error message you read on the title. I think that the issue itself is related to the fact that the code part that makes the weapon face towards the mouse is in the other script, if you know how to fix this, please let me know.

Here’s the code from the bow (I just want it to face the mouse):

extends Node2D
func _process(delta: float) -> void:

And here’s the part of the player script that shoots the arrows, etc… :

extends Character
 var arrow = preload("res://Arrowfinal.tscn")
 var rubberarrow = preload("res://RuberArrow.tscn")`

if reroll == 1:
    if Input.is_action_just_pressed("ui_attack") and current_weapon == 1 and can_fire:
        var rubberarrow_instance = rubberarrow.instance()
        rubberarrow_instance.position = $Arrowpoint.get_global_position()
        rubberarrow_instance.rotation_degrees = rotation_degrees
        rubberarrow_instance.apply_impulse(Vector2().normalized(), Vector2(arrow_speed, 0).rotated(rotation))
        can_fire = false
        yield(get_tree().create_timer(fire_rate), "timeout")
        can_fire = true

In the above code, there’s only one reference to get_global_position(). There, the problem is that it’s being called on the $Arrowpoint node, which (according to the error), is null.

For that code to work, the node containing that script must have a direct child named Arrowpoint. I assume it does not.

You mention that you’ve refactored the code because you want to interact with an rng. While I don’t see any evidence of that above, you should be able to find another way to interact wtih your rng. For example:

  • Create a single RNG in a global singleton and use it everywhere in yoru game
  • Create a local RNG at the point where you want/need it

jgodfrey | 2022-10-04 22:09

Hey, thanks for answering. The issue with moving the arrowpoint for it to be a child of the player node is that the point itself will inherit the direction from the player and not from the bow. Is there any way I can fix this?

Also don’t worry about the rng, I think I managed to sort it out, I just didn’t include it in the post because I think it’s irrelevant and I don’t want to bother anyone.

ItzKillerTTV | 2022-10-04 22:58

I’m not suggesting that your rearrange your scene tree to match the script. I’m only pointing out what the tree would need to look like for that $Arrowpoint node reference to resolve correctly in the posted code.

It’s really hard to provide any useful info without understanding how your scene tree is currently organized…

jgodfrey | 2022-10-04 23:26

Indeed, it appears that a missing child node named Arrowpoint is probably the source of the error

godot_dev_ | 2022-10-05 13:56