Is there a way to randomly select animation to play for animated sprite

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

I not sure if there is a way to randomly select animation to play for animated sprite. I want the enemy scene to play different animated sprites so the same scene could be used for a number of different enemies. Thank you.

:bust_in_silhouette: Reply From: njamster

Add the following script to your AnimatedSprite:

extends AnimatedSprite

func _ready():
	randomize()
	play_random_animation()
	
func play_random_animation():
	var animations = frames.get_animation_names()
	var animation_id = randi() % animations.size()
	var animation_name = animations[animation_id]
	play(animation_name)

It works, thank you very much.

Idleman | 2020-07-27 01:31