How to bind Event to AnimMontageEnded?

I have a C++ animation class and can access it successfully

UMyAnimInstance * MyAnimInstance = Cast<UMyAnimInstance>(GetMesh()->GetAnimInstance());

want to bind the OnMontage ended event to another event, is it possible to get it since the event is located inside animation montage sequence.
19

UMyAnimInstance->OnMontageEnded.AddDynamic(this, &AMyAnotherClass::Event_OnMontageEnded); 

// how to get the OnMontageEndedEvent to bind with another event?

or I need to load the animation and get montage end event from it?

You can create you own delegate and add it to the call list of OnMontageEnded.

Generally if you just need a specific function to do something on end, it’s better to just bind it to an event on a uobject function like you have done already here

UMyAnimInstance->OnMontageEnded.AddDynamic(this, &AMyAnotherClass::Event_OnMontageEnded);

Something to note: The function AMyAnotherClass:Event_OnMontageEnded should have the same matching function signature as the delegate. The function should also be marked as a UFUNCTION() since you are trying to bind a dynamic delegate.

4 Likes

Thank You for replying, so I need to add the delegate event Event_OnMontageEnded in the list under MontageSequence or the delegate itself OnMontageEnded ?

as you can see this is not binding event to event, but binding event to an existing notify located in the montage sequence.

for example,
.h

UPROPERTY(Transient, DuplicateTransient)
UAnimMontage* MyMontage_;

UPROPERTY(Transient, DuplicateTransient)
bool bInterrupted_;

.cpp

OnMontageEnded.BindUFunction(this,FName(TEXT("Event_OnMontageEnded")));
MyAnimInstance->OnMontageEnded.AddUnique(OnMontageEnded);


void AMyPlayerController::Event_OnMontageEnded(UAnimMontage* Montage, bool bInterrupted)
{
	MyMontage_ = Montage;
	bInterrupted_ = bInterrupted;
    anotherFunction();
}

You can bind a C++ function declared with UFUNCTION() that will be called when OnMontageEnded is broadcast. This requires that the function have the same signature as the delegate

In your class header

UFUNCTION()
void HandleOnMontageEnded(UAnimMontage* Montage, bool Interrupted)

Generally speaking, it’s a good practice to just bind a C++ function to be called by a delegate.

You shouldn’t have to bind an event to and event in C++. Not all delegates are made equal in C++ so you would have to check the docs to see which ones will be compatible with the delegate you are trying to bind to if you are hard set on declaring another delegate and adding it to the invocation list of another.

2 Likes

If you guys get NotThreadSafeDelegate access error, one reason might be your binding in not inside UAnimInstance. Make it inside UAnimInstance and it may solve error.
I had the same problem and Binding inside UAnimInstance .cpp fixed that :slight_smile: