• Godot Help
  • Invalid get index 'global_position' (on base: 'null instance')

xyz
At line 30 I think, but the error only shows as Invalid get index 'global_position' (on base: 'null instance')

  • xyz replied to this.

    Pomelo The editor should highlight the exact line. Lines in the posted code are not numerated so we don't see which line is line 30. You can't expect people to count lines in your code. So take a look at the editor and post the content of the problematic line.

      If line 30 contains the expression player.global_position, then the problem is that player is not a valid reference.

      Add this:

      func _ready() -> void:
              print_debug("_ready, player=", player)

      Add this at the beginning of func on_timer_timeout():
      print_debug("on_timer_timeout, player=", player)

      Add this at the beginning of func get_random_position():
      print_debug("get_random_position, player=", player)

      What is the output?

        DaveTheCoder What is the output?

        This may not even be executed because someone is likely calling get_random_position() prior to node being ready. Likely a _ready() of a sibling that initializes first. Or there is some mess up with unintentional signal connections. Hard to tell without seeing the scene structure.

        That's true, but you have to start somewhere.

        I edited my post to add some more print's to deal with those cases.

        Pomelo No lines highlighted

        Are you sure? Because every time an exception is thrown, the execution is stopped and the stop line is highlighted by a yellow arrow. The top entry in stack frames window will also specify at which line in which script the execution stopped. Double clicking on that will bring you to that line in the code editor.

        Ok, I just fixed it

        I had to add a

        func _ready():
        	player = get_node("/root/Main/Player")

        And it started working

        • xyz replied to this.

          Pomelo That means that your player node is not in the group "player", as your code it (unsuccessfully) trying to get a reference to it via the group membership: @onready var player = get_tree().get_first_node_in_group("player")