Table of Contents

Class InputBuffer<T>

Namespace
Coherence.Toolkit
Assembly
Coherence.Toolkit.dll
public class InputBuffer<T> : IInputBuffer, IInputBufferDebug where T : struct

Type Parameters

T
Inheritance
InputBuffer<T>
Implements

Constructors

InputBuffer(int, int, bool, EqualityComparer<T>)

public InputBuffer(int bufferSize, int inputDelay = 3, bool requiresSubsequentFrames = false, EqualityComparer<T> comparer = null)

Parameters

bufferSize int
inputDelay int
requiresSubsequentFrames bool
comparer EqualityComparer<T>

Properties

Delay

Delay applied to the stored inputs.

public int Delay { get; set; }

Property Value

int

LastAcknowledgedFrame

Last frame received that didn't trigger a misprediction, or -1 if nothing was acknowledged yet. Might be greater than LastConsumedFrame.

public long LastAcknowledgedFrame { get; }

Property Value

long

LastConsumedFrame

Last frame that was fetched from the buffer and was not predicted.

public long LastConsumedFrame { get; }

Property Value

long

LastFrame

Last (latest) frame number for which an input was stored, received or predicted. -1 if nothing was added to the buffer yet.

public long LastFrame { get; }

Property Value

long

LastInput

Last (latest) known input, or default if none was added to the buffer yet.

public T LastInput { get; }

Property Value

T

LastReceivedFrame

Last frame number that was received from the remote input producer, or -1 if nothing was received yet.

public long LastReceivedFrame { get; }

Property Value

long

LastSentFrame

Last frame number that was dequeued for sending, or -1 if nothing was sent yet.

public long LastSentFrame { get; }

Property Value

long

MispredictionFrame

Frame at which last prediction failure has been detected.

public long? MispredictionFrame { get; }

Property Value

long?

Remarks

Value is nulled-out as soon as the input is retrieved for that or subsequent frames. For this reason this property should be inspected before any input retrieving method in a given frame in order to check whether the simulation correction is required.

Size

Size of the buffer. Dictates how many frames worth of inputs can be stored. When buffer reaches its capacity a pause is requested through the ShouldPause(long, long).

public int Size { get; }

Property Value

int

Methods

AddInput(in T, long)

Adds an input to the buffer for a given frame. If the Delay is set, the frame is offset by its value. This method should be used by the owner of the input; receiving side should use the ReceiveInput(in T, long) method.

public bool AddInput(in T input, long frame)

Parameters

input T
frame long

Returns

bool

True if input differs from the last input in the buffer.

DequeueForSending(long, out long, out T, out bool)

Dequeues next input from the buffer that hasn't been sent yet. Progresses the internal LastSentFrame counter.

public bool DequeueForSending(long currentFrame, out long inputFrame, out T input, out bool differsFromPrevious)

Parameters

currentFrame long
inputFrame long
input T
differsFromPrevious bool

Returns

bool

True if there is a fresh input to send and the input has changed its value since last sending.

ReceiveInput(in T, long)

public bool ReceiveInput(in T input, long frame)

Parameters

input T
frame long

Returns

bool

Reset()

Clears the input buffer and resets its state.

public void Reset()

ShouldPause(long, long)

If true, the buffer cannot do any more predictions. Client should pause the game and stop polling inputs to not lose any data required for a rollback.

public bool ShouldPause(long currentFrame, long commonReceivedFrame)

Parameters

currentFrame long

Current simulation frame.

commonReceivedFrame long

Common (lowest) received frame for all inputs in the simulation.

Returns

bool

TryGetInput(long, out T, bool)

Retrieves input from the buffer for a given frame.

public bool TryGetInput(long frame, out T input, bool clearPredictionMark = true)

Parameters

frame long
input T
clearPredictionMark bool

Returns

bool

False if frame is outside of the buffer range or if no input was added to the buffer yet.

Events

OnStaleInput

Called when an input is received that is older than the oldest input in the buffer.

public event StaleInputHandler OnStaleInput

Event Type

StaleInputHandler