Download

Animation Montages On Interrupt, On Blend Out, On Completed in C++

Hi all,

I’ve done some Googling, but I can’t find out how to detect the On Blend Out, On Completed and On Interrupted events when playing an animation montage in C++. I see a lot of examples where people will play a montage and then set a timer based on the play length of the montage so it triggers once the montage completes, but ideally I need something that can also pick up when a montage is interrupted or starting to blend out and be more precise that way.

Is there an easy way, like in blueprints, to simply say “if the montage is interrupted, then run this code” or “if the montage starts to blend out then do this, and then when it’s fully complete do that”? All examples I try to find of On Interrupted or On Blend Out events just point me back to blueprints which isn’t helpful.

Thanks,

Richard

Ok so I figured it out in the end. Do it like this:

In your animation instance header file:



// The parameters are mandatory
void FunctionToExecuteOnAnimationBlendOut(UAnimMontage* animMontage, bool bInterrupted);
void FunctionToExecuteOnAnimationEnd(UAnimMontage* animMontage, bool bInterrupted);


In the animation instance main body:



void UMyAnimInstance::FunctionToExecuteOnAnimationBlendOut(UAnimMontage* animMontage, bool bInterrupted)
{
   if (bInterrupted)
   {
      UE_LOG(LogTemp, Warning, TEXT("MY ANIMATION WAS INTERRUPTED!"));
   } else {
      UE_LOG(LogTemp, Warning, TEXT("MY ANIMATION IS BLENDING OUT!"));
   }
}

void UMyAnimInstance::FunctionToExecuteOnAnimationEnd(UAnimMontage* animMontage, bool bInterrupted)
{
   UE_LOG(LogTemp, Warning, TEXT("MY ANIMATION HAS COMPLETED!"));
}

void UMyAnimInstance::PlayAnAnimationMontage(UAnimMontage* montageToPlay)
{
   if (montageToPlay)
   {
      Montage_Play(montageToPlay);

      FOnMontageEnded BlendOutDelegate;
      BlendOutDelegate.BindUObject(this, &UMyAnimInstance::FunctionToExecuteOnAnimationBlendOut);
      Montage_SetBlendingOutDelegate(BlendOutDelegate, montageToPlay);

      FOnMontageEnded CompleteDelegate;
      CompleteDelegate.BindUObject(this, &UMyAnimInstance::FunctionToExecuteOnAnimationEnd);
      Montage_SetEndDelegate(CompleteDelegate, montageToPlay);
   }
}


Just remember to bind the delegates AFTER you have called Montage_Play, otherwise it won’t work.

Hopefully this will be handy for someone in the future, as I didn’t find a whole lot of information on how to use this.

Just wanted to say this worked for me. Thanks!

Would you mind posting how you are loading / setting up your animations in C++? We have some code but it is ugly and I think it could be more streamlined.