Invalid get index 'current_animation' (on base: 'AnimatedSprite2D").

Godot Version

Godot 4.2.2 (on steam)


Godot Newbie here. Im trying to make my sprite switch to its attacking animation using AnimatedSprite2D. I’ve tried searching up this exact error but I couldn’t find anything that was to my knowledge exactly what I was looking for. Error is coming from line 39. Any other alternitives for this without needing to use an animation tree?

extends CharacterBody2D

@export var speed: float = 330
@export var accel: float = 270
@onready var _animated_sprite = $AnimatedSprite2D
var isAttackingLeft = false;
var isAttackingRight = false;
var isAttackingUp = false;
var isAttackingDown = false;

#player movement
func _physics_process(_delta: float) -> void:
	var direction: Vector2 = Input.get_vector("left", "right", "up", "down")
	velocity.x = move_toward(velocity.x, speed * direction.x, accel)
	velocity.y = move_toward(velocity.y, speed * direction.y, accel)

func _process(_delta):
	if Input.is_action_pressed("left") && isAttackingLeft == false:"run_left")
	elif Input.is_action_just_released("left"):"idle_left")
	if Input.is_action_pressed("right") && isAttackingRight == false:"run_right")
	elif Input.is_action_just_released("right"):"idle_right")
	if Input.is_action_pressed("up") && isAttackingUp == false:"run_up")
	elif Input.is_action_just_released("up"):"idle_up")
	if Input.is_action_pressed("down") && isAttackingDown == false:"run_down")
	elif Input.is_action_just_released("down"):"idle_down")
	elif Input.is_action_just_pressed("attack") and _animated_sprite.current_animation == "idle_left":
		isAttackingLeft == true"attack")

The error on line 39 is likely due to an incorrect comparison operator (==) being used instead of the assignment operator (=). Or, you used : instead of &&. Additionally, perhaps you want to reset isAttackingLeft to false after the animation is played.

I think you probably want to change this:

elif Input.is_action_just_pressed("attack") and _animated_sprite.current_animation == "idle_left":
		isAttackingLeft == true

To this, with assignment = rather than equality ==:

elif Input.is_action_just_pressed("attack") and _animated_sprite.current_animation == "idle_left":
		isAttackingLeft = true

Still getting the same error

Invalid get index ‘current_animation’ (on base: 'AnimatedSprite2D").

_animated_sprite.current_animation == "idle_left":
AnimatedSprite2D does not have a ‘current_animation’ property.
Where are you getting that from?

PS: It does have an animation property defined in the docs:

The current animation from the sprite_frames resource