Error Invalid get index on base: string

Godot Version

4.2.2

Question

How can i fix this error

@export var Entity_health = 5
@export var unit_alignment : Alignment
@export var speed = 100
@export var player = (“res://Scenes/PLayer.gd”)

Get the gravity from the project settings to be synced with RigidBody nodes.

var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _physics_process(delta):
# Add the gravity.
if not is_on_floor():
velocity.y -= gravity * delta

enum Alignment {ALLY, NEUTRAL, ENEMY}

func _ready():
if unit_alignment == Alignment.ALLY:
print(“I will Protect you”)

func _process(delta):
if player.velocity.x or player.velocity.z != 0:
player_position()
follow_player()
func follow_player():
position += (player.position - position)/ speed

func player_position():
print(player.position)

Please use ``` at the beginning and end of your code:

@export var Entity_health = 5
@export var unit_alignment : Alignment
@export var speed = 100
@export var player = (“res://Scenes/PLayer.gd”)

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _physics_process(delta):
   # Add the gravity.
   if not is_on_floor():
      velocity.y -= gravity * delta

enum Alignment {ALLY, NEUTRAL, ENEMY}

func _ready():
   if unit_alignment == Alignment.ALLY:
      print(“I will Protect you”)

func _process(delta):
   if player.velocity.x or player.velocity.z != 0:
      player_position()
      follow_player()

func follow_player():
   position += (player.position - position)/ speed

func player_position():
   print(player.position)

Which line is the error related to?

the line is 27

Well, that’s probably how it’s fixed (load added on line 6):

@export var Entity_health = 5
@export var unit_alignment : Alignment
@export var speed = 100
@export var player = load(“res://Scenes/PLayer.gd”)

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting(“physics/3d/default_gravity”)

func _physics_process(delta):
   # Add the gravity.
   if not is_on_floor():
      velocity.y -= gravity * delta

enum Alignment {ALLY, NEUTRAL, ENEMY}

func _ready():
   if unit_alignment == Alignment.ALLY:
      print(“I will Protect you”)

func _process(delta):
   if player.velocity.x or player.velocity.z != 0:
      player_position()
      follow_player()

func follow_player():
   position += (player.position - position)/ speed

func player_position():
   print(player.position)
1 Like

For sure that line is the problem and yours is one possible solution (We don’t have enough info to be sure).
Its pretty rare that one loads a player script rather than a scene.
Usually a player object is CharacterBody2D + sprite + camera + hitbox +etc.
It may be he wants to load the scene:

@export var player_scene = load(“res://Scenes/Player.tscn”)   
#and then somewhere that scene would need to be instantiated.
var player = player_scene.instantiate()

But based on the fact that it is an export and some of the code further on, it could be that he just wants to drag the node into the script in which case it would be
@export var player = $Player

1 Like

im very new and teaching myself of i may be doing things very inefficiently. if thwir is a better way lmk. im trying to have the mob follow the player when they move and this is a slime

I believe you mean to @export the player, and select them from the inspector. Try this line for your player.

@export var player: CharacterBody3D

also its 3d does this matter? also what else can i include to make it easier to understand

3D right sorry, I’ve edited my post.

If you are still getting errors showing your scene tree would help so we can know where the player is relative to the enemy

yes i am

Thanks for showing a little of the scene tree. As with all exported variables you will need to set a value for it in the Inspector. Select your Slime node and “Assign…” the player property there.

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