In this lesson, you will learn how to create an input queue system that captures player commands during animations to ensure fluid, consecutive combat attacks.
Why Do We Need Input Buffering?
When a player mashes the "Attack" button while their character is still performing an animation, the input is often ignored because the character is locked in an "animating" state. An input buffer acts as a temporary holding pen: it records the player's intent to attack and executes that command the exact moment the current animation finishes. This makes your combat feel "snappy" rather than unresponsive.
The Logic of the Queue
Think of the input buffer as a small, short-lived memory slot. When the player presses the button, we don't just ask, "Can I attack right now?" Instead, we follow this flow:
- Input Received: Store the action (e.g.,
EActionType::LightAttack) in a variable. - Timestamping: Record the current time to expire the buffer if the player waits too long (preventing "ghost" attacks).
- The Check: Once your current Animation Montage finishes (via an
AnimNotify), check if the buffer has a valid, non-expired action waiting. - Execution: If a command exists, play the next animation immediately.
Implementation Snippet
You can store the buffered action in your Character class like this:
// In your Character header
UPROPERTY()
EActionType BufferedAction = EActionType::None;
float BufferExpirationTime = 0.2f;
float LastInputTime = 0.0f;
// Call this when the button is pressed
void AMyCharacter::BufferInput(EActionType Action)
{
BufferedAction = Action;
LastInputTime = GetWorld()->GetTimeSeconds();
}
When your attack animation triggers a BlueprintNativeEvent or AnimNotify indicating it has finished, you simply check if GetWorld()->GetTimeSeconds() - LastInputTime < BufferExpirationTime. If the buffer is still "fresh," you trigger the BufferedAction immediately. By clearing the buffer after the attack starts, you ensure the command is consumed and the system is ready for the next player input.
Key Takeaway: An input buffer transforms missed clicks into queued intentions, making your combat feel intuitive and responsive to the player’s rhythm.