Post

How to get MontageInstance correctly

How to get MontageInstance correctly

Terms

Montage

refers to UAnimMontage Object

MontageInstance

refers to FAnimMontageInstance Object

Get the MontageInstance

GetInstanceForMontage - Find first matching MontageInstance

Finding from UAnimInstance::MontageInstances

GetActiveInstanceForMontage - Find the active MontageInstance

Finding from UAnimInstance::ActiveMontagesMap

Simple Logic Flow

MontageInstanceLifetime

You can see that after playing a montage,

the newly generated montage instance can be obtained through any of the interfaces above,

After stopping/blending out, the montage instance, is no longer considered active,

After the playback is complete/the montage is actually stopped, it cannot be obtained by any means.

But in a short period of time, when the same montage is played multiple times, things start to get a little more complicated,

Suppose that when playing, bStopAllMontages is true, which is stopping the old playing montage.

For GetInstanceForMontage

Since the engine implementation is return the first montage instance whose asset is the incoming montage,

Then, if there are multiple montage instances blending out with the same montage asset, you can only get the blending out one through this interface, not the “active instances” you may need, because the active instance always get added to the end of the array. GetActiveInstanceForMontage should be used in this case

GetMontageInstanceForID - Finding with InstanceID

In addition, engine provides this interface to support searching specific instance using FAnimMontageInstance::InstanceID which is unique within the process

Suggestion

Because the GetActiveInstanceForMontage interface is queried through a map instead of traversing an array, it is the fastest.

If you just need to find a montage instance that is playing and not blending out/active, query it using the montage pointer and the GetActiveInstanceForMontage interface.

These is only one active montage instance at most, because montage and montage instance is one-to-one mapped with map.

ActiveMontageMap

For inactive montage searches, you can consider caching FAnimMontageInstance::InstanceID for accurate searches to avoid the problem of finding the wrong object when playing the same montage multiple times

This post is licensed under CC BY 4.0 by the author.