Where to start when making an input buffer system?

Hello there,

Here’s how I’d probably approach it; Separating physical input from game actions by buffering the actions instead of acting on input directly. The input layer just says “jump was pressed”; a buffer holds that request for a short window, and gameplay polls it when the conditions are met.


A wrapper that holds a countdown timer alongside the action it represents:

class_name BufferedAction
extends RefCounted

var kind: StringName
var time_left: float

func _init(p_kind: StringName, p_window: float) -> void:
    kind = p_kind
    time_left = p_window

A buffer that owns the buffered actions and their lifecycle:

class_name ActionBuffer
extends RefCounted

var _actions: Array[BufferedAction] = []

func push(kind: StringName, window: float) -> void:
    # dedupe: if it's already buffered, just refresh the timer
    for a in _actions:
        if a.kind == kind:
            a.time_left = window
            return
    _actions.append(BufferedAction.new(kind, window))

# success path: find + remove
func try_consume(kind: StringName) -> bool:
    for i in range(_actions.size()):
        if _actions[i].kind == kind:
            _actions.remove_at(i)
            return true
    return false

# just remove expired ones here
func tick(delta: float) -> void:
    for i in range(_actions.size() - 1, -1, -1):
        _actions[i].time_left -= delta
        if _actions[i].time_left <= 0.0:
            _actions.remove_at(i)

# if we need fully clear the buffer
func flush() -> void:
    _actions.clear()

Then push on input and consume in physics:

extends CharacterBody2D

@export var jump_buffer_window: float = 0.15
var _buffer := ActionBuffer.new()

func _unhandled_input(event: InputEvent) -> void:
    if event.is_action_pressed("jump"):
        _buffer.push(&"jump", jump_buffer_window)

func _physics_process(delta: float) -> void:
    # consume before tick when conditions are met
    # so a tight window can't expire
    if is_on_floor() and _buffer.try_consume(&"jump"):
        _jump()
    _buffer.tick(delta)

func _jump() -> void:
    pass  # velocity.y = jump_velocity ...