Invalid get index 'animation' (on base: 'AnimationPlayer').

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TonyRedgrave

I don’t know why this error appears:

Invalid get index ‘animation’ (on base: ‘AnimationPlayer’).

I am doing my first project on godot

extends KinematicBody2D
onready var anim: AnimationPlayer = get_node("anim")
var velocity: Vector2
export(int) var speed = 200
var facingDir = Vector2()
func get_input():
velocity = Vector2()
if Input.is_action_pressed("ui_right"):
	velocity.x += 1
	facingDir = Vector2(1,0)
if Input.is_action_pressed("ui_left"):
	velocity.x -= 1
	facingDir = Vector2(-1,0)
if Input.is_action_pressed("ui_down"):
	velocity.y += 1
	facingDir = Vector2(0,1)
if Input.is_action_pressed("ui_up"):
	velocity.y -= 1
	facingDir = Vector2(0,-1)
velocity = velocity.normalized() * speed

func _physics_process(delta):
  get_input()
  velocity = move_and_slide(velocity)


if velocity.x > 0:
	play_animation("right")
elif velocity.x < 0:
	play_animation("left")
elif velocity.y > 0:
	play_animation("up")
elif velocity.y < 0:
	play_animation("low")
elif facingDir.x == 1:
	play_animation("idle_right")
elif facingDir.x == -1:
	play_animation("idle_left")
elif facingDir.y == 1:
	play_animation("idle_low")
elif facingDir.y == -1:
	play_animation("idle_up")

func play_animation(anim_name):
  if anim.animation != anim_name:
	anim.play(anim_name)
:bust_in_silhouette: Reply From: jgodfrey

Like the error says, an AnimationPlayer doesn’t have a property named animation. Maybe you’re looking for the get_animation() function call?

I think the if statement is there to prevent playing an already playing animation.
get_animation() returns an animation if it exists otherwise a null.
So I think that the property current_animation is what he is looking for:

if anim.current_animation != anim_name:

LeslieS | 2023-02-13 19:01

Good call @LeslieS - thanks! :slight_smile:

jgodfrey | 2023-02-14 14:47