How do I play animation using gdscript

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

When I use .play() It says " Invalid call. Nonexistent function ‘play’ in base ‘GDNativeClass’."

func get_input():
var vy = velocity.y
velocity = Vector3()
if Input.is_action_pressed("move_forward"):
	velocity += transform.basis.z * speed
	anim.play("leg_movement")
if Input.is_action_pressed("move_backward"):
	velocity -= transform.basis.z * speed
	anim.play("leg_movement")
if Input.is_action_pressed("move_right"):
	velocity -= transform.basis.x * speed
	anim.play("leg_movement")
if Input.is_action_pressed("move_left"):
	velocity += transform.basis.x * speed
	anim.play("leg_movement")

You left out some code. What have you assigned to anim?

kidscancode | 2020-05-26 17:43

var anim = AnimationPlayer

Reganazer | 2020-05-27 15:27

:bust_in_silhouette: Reply From: sphixy011

if you use gd script

you can use $AnimatedSprite.play(“the name of your animation”)

change AnimatedSprite by the name of your node

:bust_in_silhouette: Reply From: kidscancode

When you write

var anim = AnimationPlayer

You’re assigning anim to the AnimationPlayer class. This is why you get an error.

What you want is to assign it to your specific animation player node. You can do this with get_node("AnimationPlayer") or the shortcut $AnimationPlayer. In addition, you need to make sure the node is ready first:

onready var anim = $AnimationPlayer