In this lesson, you will learn how to synchronize your C++ logic with animation timelines using the UAbilityTask_PlayMontageAndWait task.
When building complex abilities, simply firing off an animation isn't enough; you often need to trigger a gameplay effect or apply damage at a specific frame (like the peak of a sword swing). The PlayMontageAndWait task acts as a bridge between the Animation System and the Gameplay Ability System (GAS), pausing your ability’s execution flow until the animation reaches specific points or concludes.
Coordinating Logic with Animation
The task manages the lifecycle of a UAnimMontage. It provides several delegates that notify the ability of the montage's state. When you call this task, the ability remains "active" while the montage plays, preventing subsequent logic from firing prematurely.
// Within your ActivateAbility()
UAbilityTask_PlayMontageAndWait* Task = UAbilityTask_PlayMontageAndWait::CreatePlayMontageAndWaitProxy(
this,
NAME_None,
MyMontage
);
// Bind to delegates to handle outcomes
Task->OnCompleted.AddDynamic(this, &UMyAbility::OnCompleted);
Task->OnInterrupted.AddDynamic(this, &UMyAbility::OnInterrupted);
Task->Activate();
Handling Interruption
Crucially, your ability must gracefully handle the OnInterrupted delegate. In GAS, abilities can be canceled due to movement, stun effects, or higher-priority tags. If an ability is interrupted while a montage is playing, the PlayMontageAndWait task automatically stops the montage on the character, but your C++ code must ensure that any associated cleanup—like clearing pending timers or removing gameplay tags—is triggered inside the OnInterrupted callback.
By using these delegates, you ensure that your gameplay logic stays perfectly in sync with the visual fidelity of your animations. If the player is stunned mid-swing, the OnInterrupted event triggers immediately, allowing you to stop the ability and reset your character's state, preventing the "hanging" of animation logic.
Key Takeaway: The PlayMontageAndWait task synchronizes animation execution with ability logic, requiring dedicated delegate handling to ensure stability when abilities are interrupted by gameplay tags or external events.