Notifies vs Timers for Animation Montage

Hullo,

I’ve got another animation question.
I want to run an animation montage from C++, which I can now largely do, but I want to run a C++ function when it finishes.
There are two primary ways to do this.

I can do it with a timer with something like the following:

void AMGCharacter::OnPrimaryAttackPressed()
{
	bIsPrimaryAttacking = true;
	float duration = PlayCombatAnimation(PrimaryAttack);
	PlayingAnimation = PrimaryAttack;
	GetWorldTimerManager().SetTimer(TimerHandle_OnCombatAnimationFinished, this, &AMGCharacter::StopCurrentCombatAnimation, duration, false);
	bIsCombatAnimating = true;
}

Or I can do it with a notification.
To do it with a notification right now, I can make a notification in the animation and then wire a Blueprint to call a C++ function to finish the animation.

Two questions:

  1. If I define a custom UAnimNotify type, such as UAttackEndedAnimNotify, how would I get the pointer to this notify, if there is one, from the animation montage and set a delegate for the call on notify? Or should I set up for this notify to always get the mesh, then the character, then cast to my character type (is this possible), and always run a particular function form there?
  2. If I can set up the notification from C++, would it still be preferably to use the timer method? This is how it’s done in the ShooterGame. It’s much simpler, but I’m not sure it would be better (or at least I’m not sure what I’d be missing out on).

Hi, take a look at my answer here it may guide you !

Best regards.

Hey Joris, thanks for the response! Your example is very useful, but I don’t see in that thread where timers vs notifiers are compared, and I don’t see how to get a pointer to the notify from PlayAnimMontage or PlayAnimation. Do you have a comparison for the two methods and know how to get a pointer to the nofity from the pawn class calling the animation to play?

Yeah that’a a partial response :D.
Just like that I would say that you will encounter synchronization issues with timers, cause obviously timers will have a “hard coded” length while notification callbacks will fired at any time so you don’t have to think about it.

About “How to get notifies pointer” try something like GetMesh()->GetAnimInstance()->GetCurrentActiveMontage()->GetAnimNotifies()