4 min read

Creating Smooth Experience with Input Buffer

Creating Smooth Experience with Input Buffer

In our quest to create a casual and enjoyable gaming experience, we quickly realized the need to incorporate elements from more sophisticated games. At the heart of our game lies a clever mechanism known as the Input Buffer. This feature is designed to seamlessly queue up player inputs during ongoing actions, ensuring that no gaming moment goes unnoticed. Imagine your character engrossed in a dynamic attack animation, and almost at the end of it, you decide to execute a dodging move. In many games, such an action might be ignored until the current animation sequence concludes. However, in our game, we achieve better responsiveness thanks to the Input Buffer.

How it works

The concept behind the Input Buffer is to queue up user input during ongoing actions for a certain time period when it cannot be immediately acted upon. Then, as soon as the player regains control, it replays the most recent input.

Let's imagine a scenario where the player is pressing a button at relatively regular intervals. Each press is intended to initiate some action, like an attack or a jump.

Player pressing button in regular intervals

Without Input Buffering: The execution of actions would look like this disjointed sequence. Pay attention to the significant gaps between actions. The second input is ignored because it's registered while the first action is still ongoing.

Action execution without input buffering

With Input Buffering: During ongoing action the second input gets queued up by the Input Buffer which then replays it as soon as the action completes. The third input on the other hand is not replayed because the buffer interval expired before the action ended.

Action execution using input buffering

Solution

We went through several iterations and refinements (if you're interested, you can check the journey here). With time, our Input Buffer has evolved to be simpler and more versatile, seamlessly handling our needs. We've centered our logic around subscriptions. The idea is that when there's anyone listening to input events, we should simply pass through the input event. However, when there's no one listening, we buffer the input event for a defined period. The decision of whether the input buffer should replay the last unconsumed input in the next frame is only made when a new subscriber joins. This approach elegantly conceals the complexity of input buffering from other system components like character behaviors and more.

In plain terms, our solution can be summarized as follows:

  • At any given moment, it's behaviours' decision to listen (or not) to input events.
  • The Input Buffer delivers input events to all subscribers and marks the input as handled.
  • If there are currently no subscribers, the input is cached for a defined period.
  • When a new subscriber joins and unhandled input exists, the Input Buffer replays it (as if it happened now).

Sequence Diagram

Take a closer look at the step-by-step process illustrated in our sequence diagram. It's important to note that this diagram does not strictly adhere to a proportional time scale but tries to present what actions happen in the same frames.

Forwarding Input Events

When there are subscribers to input events, the Input Buffer simply forwards the event to all subscribers.

Simple forwarding when there are active subscribers

Replaying Last Input

When a new subscriber joins, we check if the last buffered input is still valid. If it is, we seamlessly replay the last input event in the next frame. It's worth noting that from the behaviour's perspective, the input appears to occur in that moment when, in fact, it was recorded some time in the past.

Replaying input if the subscriber joins while the buffer is not expired

Buffer Interval Expired

In cases where the last input was recorded too long ago to be replayed, it is simply not propagated to the behaviour.

When buffer expires it's simply not replayed

Conclusion

The Input Buffer stands as one of the many engineering marvels that contribute to an exceptional gaming experience in our upcoming title. It serves as an indispensable tool, ensuring that players' actions are promptly recognized, thereby enhancing gameplay precision and minimizing frustration.

As we continue to refine and perfect our game, we are thrilled about the positive impact the Input Buffer will have on players' experiences. Stay tuned for more exciting updates and insights into the realm of game development, where every line of code is crafted to elevate your gaming adventure to extraordinary heights.