I need help with some code,

Godot Version

4.2.2

Question

I got an error saying; invalid get index ‘global_position’ (on base:timer).

extends CharacterBody2D

@export var patrol_points : Node
@export var speed : int = 1500
@export var wait_time : int = 3

@onready var animated_sprite_2d = $AnimatedSprite2D
@onready var timer = $Timer

const GRAVITY = 1000

enum State { Idle, Walk }
var current_state : State
var direction : Vector2 = Vector2.LEFT
var number_of_points : int
var point_positions : Array[Vector2]
var current_point : Vector2
var current_point_position : int
var can_walk : bool


func _ready():
	if patrol_points != null:
		number_of_points = patrol_points.get_children().size()
		for point in patrol_points.get_children():
			point_positions.append(point.global_position)
		current_point = point_positions[current_point_position]
	else:
		print("No patrol points")
	
	timer.wait_time = wait_time
	
	current_state = State.Idle


func _physics_process(delta : float):
	enemy_gravity(delta)
	enemy_idle(delta)
	enemy_walk(delta)
	
	move_and_slide()
	
	enemy_animations()


func enemy_gravity(delta : float):
	velocity.y += GRAVITY * delta


func enemy_idle(delta : float):
	if !can_walk:
		velocity.x = move_toward(velocity.x, 0, speed * delta)
		current_state = State.Idle


func enemy_walk(delta : float):
	if !can_walk:
		return
	
	if abs(position.x - current_point.x) > 0.5:
		velocity.x = direction.x * speed * delta
		current_state = State.Walk
	else:
		current_point_position += 1

		if current_point_position >= number_of_points:
			current_point_position = 0

		current_point = point_positions[current_point_position];

		if current_point.x > position.x:
			direction = Vector2.RIGHT
		else:
			direction = Vector2.LEFT
		
		can_walk = false
		timer.start()
	
	animated_sprite_2d.flip_h = direction.x > 0


func enemy_animations():
	if current_state == State.Idle && !can_walk:
		animated_sprite_2d.play("idle")
	elif current_state == State.Walk && can_walk:
		animated_sprite_2d.play("walk")

func _on_timer_timeout():
	can_walk = true

I could use some help if possible! Thanks!

Is it possible that one of the patrol_points child nodes is a timer?
It would be easier to find the source of the problem if you stated on which line the mentioned error occurred.

2 Likes

Oh sorry, it shows a yellow line on line 28 point_positions.append(point.global_position)

As FencerDevLog said, probably you’re getting a timer node that is a child of your node somewhere, you can do some checks to make sure the node you get is the correct one

func _ready():
	if patrol_points != null:
		number_of_points = patrol_points.get_children().size()
		for point in patrol_points.get_children():
			# Here you can do this check in two ways:

			# Check if the node has a variable called "global_position"
			if "global_position" in point:
				point_positions.append(point.global_position)

			# Or check if this node is or inherits CanvasItem class (Node2D 
			# and Control, the two classes that has the global_position property
			# inherits CanvasItem class)
			if point is CanvasItem:
				point_positions.append(point.global_position)

		current_point = point_positions[current_point_position]
	else:
		print("No patrol points")
	
	timer.wait_time = wait_time
	
	current_state = State.Idle

Thanks it works!