Invalid get index 'position' (on base: 'String') error, when trying to make an enemy that follows the player

Godot Version

4

Question

I followed a tutorial on how to make enemies follow players on youtube. It was made for godot 3 and many little things had to be changed. Now all error messages except for one have disappeared.

Invalid get index ‘position’ (on base: ‘String’).

I saw elsewhere that someone suggested someone else with a similar problem to add:

func _ready():
for node in get_tree().get_nodes_in_group(“Player”):
player = node

I tried to add it but made no difference.

I would guess your player isn’t in a group named “Player”

To access a node you must use the dollar sign

@onready var player = $/root/game/player

This may still not result in finding the player, It’s a bad idea to search for parent nodes on ready, the group method isn’t bad but you need to add your player to a group named “Player” then you can use this shorter function

func _ready() -> void:
    player = get_tree().get_first_node_in_group("Player")
1 Like

Thanks a lot for helping out. I will head to sleep but will read about groups tomorrow and see if I can figure this out!

I added player to the group but the script still doesn’t work. Scene and group are both named “player”.

Line 12 gets the error message: Invalid get index ‘position’ (on base: ‘null instance’).

Did I miss something?

Your player doesn’t seem to be ready when your enemy is. You can wait a frame for the entire scene to be ready

func _ready() -> void:
    await get_tree().process_frame
    player = get_tree().get_first_node_in_group("Player")

It still displays the same error message.


I tried using a different tutorial as well but that one displays it as well.

What does “Invalid get index ‘position’ (on base: ‘Nil’)” even mean?

it means you are trying to get a ‘position’ from a variable that holds nothing.

In this case “global_position” is what you are trying to access, and “target_to_chase” doesn’t hold anything, so it gives that error.

target_to_chase.global_position

Did you assign your enemy “Node2D” a character in the inspector? If you are using @export, which is a good idea, then you will have a new property to assign, named “Target To Chase”. Click the Node2D again and check your Inspector.


I don’t know why the group method didn’t work unless the group assigned does not match the group you have in the script.

Yes, I assigned it to the player within the scene where they both are. I was not able to assign it in the scene where only the enemy is, as it could only access nodes that were present in the scene. So within the scene I’m trying to load, the enemy is set to chase the player.

Both group and player and all that is “player” with same caps.

I think maybe it would be better if I return to this problem in a while when I know more.

The player character needs to be in a navigation region 2d.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.