Last week, I posted about how you might play an animation when a player initiates dashing. At the end of the post, I asked about how we might play a random animation instead of the same one over and over.
It’s actually rather simple, we use a method called .sample. Using this on an array, we can get one of the items, at random.
In our previous post we used this script to set the animation ID of the player:
$game_player.animation_id = 3
This would just always play the animation #3 every time dashing happened. But suppose we want to be able to choose from animations 3, 7, 11, 14, 15, and 28. First, make an array of those options.
anims = [3,7,11,14,15,28]
There’s no minimum or maximum for how many animations are in your array. It can be 2 or 200 – but have fun making 200 animations.
Then, just pick one at random, and use it for our animation_id.
x = anims.sample $game_player.animation_id = x
And that’s all it takes!