Given an USkeletalMeshComponent, how to set animation state in c++?

I have access to a USkeletalMeshComponent. This means that I can get the corresponding UAnimInstance. This gives me access to a const ptr to the state machine.

I want to change the state of the animation state machine in c++ to avoid writing complex logic in blueprint. I’d imagine that this would be a fairly common operation as writing complex algorithms in visual language can be messy.

Please help,
Thanks!

The animation state machine always uses some kind of condition to change state.

It’s frequently the case that your event graph in the animation blueprint will pull its desired state variables from the controlling pawn (character) in Tick, and the state machine transitions will then happen automatically based on this state.

It’s much more robust to build your state transitions using this mechanism, than trying to put a bunch of hard-coded logic into C++. For a great example of custom animations, check out one of the downloadable Paragon characters. They don’t have all of the state machine bits of the full game, because they don’t come with all of the trappings of the full game, but they still give good insight into how artists build these things.

State machines and trigger conditions based on variable values, are more robust, and more flexible, than C++ code, and easier to debug and work with. Plus your animators can often build these machines themselves, without necessarily involving your engineers, which means they can iterate faster, which means higher quality art in a given time budget.

1 Like

I didn’t realise having ABP pull pawn state for transition condition is the correct pattern.
Thank you for the detailed answer!