How to play a random animation from a list

– Edit: I think I made it look more complicated than it actually is. I can simplify my question as:

Provided that you can access an array of full-body anim sequences, how would you create an Animation BP that picks an anim sequence at random from the array, play it until it’s finished, and repeat?
Blending is a nice bonus, but I could do without.

(See below for some backgound)


In our project, our characters can play many attack animations. We have different classes of characters, and I want to keep a simple, generic base AnimBP, and be able to add animations easily when adding a new character class.

In practice, I would like to have one “Attack” anim state, deciding which animation to play before entering it, staying in the state until the anim is almost done playing, and blending back to the idle state.

Here is an example of what I don’t want to do:

What I tried instead is to add properties of type AnimSequence in the AnimBP, and feed them to the Play Anim node using the Sequence input pin. That way, instead of having to instanciate a Play node for each animation in the state and selectively blend between them, I can pick one at runtime and feed it to a single Play node.

10960-capture.png

I’m not sure if this is a recommended practice. It works most of the time, but I hit a few times an assert in AnimSequenceBase.cpp:

check( bPlayingBackwards ? (CurrentPosition <= PreviousPosition) : (CurrentPosition >= PreviousPosition));

I suspect that it happens when going out and back in the Attack state, while the previous attack animation is still playing, and I swap it with a new one.

So my questions:

  • Is using the “Sequence” input pin of the Play anim node the way to play variables animations in an anim graph ?
  • Is the anim graph evaluated in the game thread, and if not, how safe is it to change the input value at runtime ?
  • Would you recommend a more low-level approach, like a custom animation player that, given an array of AnimSequences, decides which one is best to play ? I don’t exclude creating a custom animation node.

I have looked at Montage and branch points, but as far as I understand it requires specifying the sections manually, and in my case each character class has a variable number of animations.

Again, the key difficulty here is that I’m looking for a generic solution where I can add animations at a later point without touching the base AnimBP.

May I ask if you have looked at the State Machine yet? I see you mention states, but I don’t think you refer to the state machine right?

Using states is a powerful tool to blend between animations, using booleans like you have in the images. Animations will be reset each time a state is entered too, so you do not have to worry about the animation not being rewinded.

Slightly off-topic; in your first image, you are using compare int to route your graph. I would recommend using Switch on Int instead. Those 6 compare nodes becomes one instead.

Hi, yes I am using states. Conceptually, I would really like having just one animation state for the attack animation. The actual animation used is mostly cosmetic.
Right now I have separate states for the different variations, and this doesn’t scale, it means I have to add a new state every time I add a new variation. Also, the logic to decide what variation to use become more and more complex, and is hard to implement as transitions rule.

(About the image, I didn’t actually do this, it’s a screenshot from another topic)

You could use an extra State Machine inside another State just for the different variations. Otherwise, one alternative is to use a Blend Pose by Int or Bool to jump between different pre-defined animations inside the Anim Graph.

Interesting, I haven’t investigated sub-state machines, I’ll look at this, thanks for the suggestion! Regarding Blend Pose, as far as I understand it only works if you have a fixed set of animations

I’m looking to do something very similar to this and I have run into the same issue where it will be too difficult to maintain the AnimBP as the number of animations increase; it just will not scale up well.

I’ll be looking into some solutions and I’ll report back in this question if I make any progress.

Have you made any progress with this? (I saw below that you’re investigating sub-states)

I ended-up writing a custom AnimGraph node inspired by the Play node, and having just one state for it. I can’t do it right now, but I can post more details on it later.

Ok, sure. If you could post it as an answer when you have time it would be much appreciated :slight_smile:

As mentioned above, I ended-up creating my own AnimNode. It takes an AnimSequence property and read it once only when starting to play. If the property is still not null at the end of the animation, it will loop. Otherwise it stops. It also calls an event on the AnimBP event graph (EndOfPlay event) before reaching the end of the animation. This is helpful to transition outside the current state.

It’s pretty specific to my need right now: QueuedSequencePlayer.zip

See these references for how to compile custom AnimGraph nodes:

See answer below

Thanks! I’ll have a look and see if I can figure this out :slight_smile:

Good to hear that it helped! Enter a name in the property (“MyEndEvent”) and create an event in the AnimBP with the same name. It will get called automatically.

Try setting a non-zeo value for End Of Play Event Time (couldn’t find better name…) it’s the time to send the event, in seconds BEFORE the end of sequence. The default transition is 0.2, you can try with that. Otherwise, try placing a breakpoint in the Update function

Thanks again! I got it all working using your code as a base. But how do I go about calling the “End of Play Event”?

I’ve tried that (and a few other things) but it hasn’t worked:

Inputting a non-zero value did the trick! Thanks for all your help :slight_smile:

I know this is a really old post but I did this and it worked for me. I created a bunch of animation Montages based on Death animations. I wanted each death to be unique so I made a variable made it an array and called it Death Montage and set it to Anim Montage. I then added all the death anim montages into the array. I then told it to Get the array and grab a random integer and pumped the get into the Play Anim Montage. Now when I kill a bad guy they die 1 of 6 ways.
I hope this help anyone having trouble with this.

Sorry por posting in old threads but like NeilLondon I found a solution and want to share in case that somebody else come across this issue. There is a node called “Random Sequence Player” where you can add to it all the animations that you want to use and assign them a number of “chance” that animation will play, in my case I want a 33% chance each. Everytime animation ends and has to play again (which is my case) it will pick randomly from the list. I hope this helps

1 Like