• Godot Help
  • Working on a top down game and getting a "Invalid get index" error

I've been following a really quick tutorial by a guy named MizizizizTutorials about how to do a top down game. the vid is 17 minutes long, and i've pretty much followed everything to the letter. When I just have the player in everything works fine, but its when I add my Zombie asset that the whole game/debugger crashes.

If there is anymore information you need please feel free to ask!

im not exactly sure why its doing it. This is the code I've put in, and I've copied it line for line in the tutorial:

extends CharacterBody2D

@onready var ray_cast_2d = $RayCast2D

@export var move_speed = 100
@onready var player : CharacterBody2D = get_tree().get_first_node_in_group("player")

var dead = false

func _physics_process(_delta):
    if dead: 
        return
    
    var dir_to_player = global_position.direction_to(player.global_position)
    velocity = dir_to_player * move_speed
    move_and_slide()
    
    global_rotation = dir_to_player.angle() + PI/2.0
    
    if ray_cast_2d.is_colliding() and ray_cast_2d.get_collider() == player: 
        player.kill()
        
func kill():
    if dead:
        return
    dead = true
    $Deathsound.play()
    $Graphics/Dead.show()
    $Graphics/alive.hide()
    $CanvasLayer/DeathScreen.show()
    $CollisionShape2D.disabled = true
    z_index = -1

Im a complete novice when it comes to coding with GDscript, and I have a small amount of knowledge in python.

When I run it there is a small yellow arrow pointing to this line of code:

var dir_to_player = global_position.direction_to(player.global_position)
its also feeding me this error message:

        Invalid get index 'global_position' (on base: 'null instance') 
  • TheFollyWorks replied to this.
  • TheFollyWorks Fix the posted code formatting first so we can see what is happening.

    In your posted code, player is initialized like this:
    @onready var player : CharacterBody2D = get_tree().get_first_node_in_group("player")
    This line assumes that the node is assigned to group "player". If there are not nodes in that group - it'll return null, and the variable player will remain undefined.

    TheFollyWorks
    I have no idea why only parts of the code are being put into the little boxes, sorry for any inconvinence

    • xyz replied to this.

      TheFollyWorks Put your code between ``` tags

      The error means that player is undefined. Since you define it by getting the first node in the group "player" check that the player node is indeed in that group.

        xyz Im assuming that you mean in my main game scene when referring to group?

        • xyz replied to this.

          TheFollyWorks Fix the posted code formatting first so we can see what is happening.

          In your posted code, player is initialized like this:
          @onready var player : CharacterBody2D = get_tree().get_first_node_in_group("player")
          This line assumes that the node is assigned to group "player". If there are not nodes in that group - it'll return null, and the variable player will remain undefined.

            TheFollyWorks

            xyz Ok, so I figured I out. I didnt actually make a player group XD, so I looked up how to do it and was able to get it to work.