Table of Contents

Class CoherenceInputSimulation<TState>

Namespace
Coherence.Toolkit
Assembly
Coherence.Toolkit.dll

CoherenceInputSimulation<TState> helps building a deterministic simulation by taking care of most complexities related to keeping a simulation state, handling predictions, rolling back state, resimulating state, etc..

public abstract class CoherenceInputSimulation<TState> : MonoBehaviour

Type Parameters

TState

Type that holds the simulation state for a given frame.

Inheritance
CoherenceInputSimulation<TState>

Constructors

CoherenceInputSimulation()

protected CoherenceInputSimulation()

Fields

PauseAutomatically

If set to true, the simulation will automatically handle the ShouldPause changes, stopping the fixed simulation updates as needed.

public bool PauseAutomatically

Field Value

bool

coherenceBridge

public CoherenceBridge coherenceBridge

Field Value

CoherenceBridge

Properties

AllClients

All instances of the connected CoherenceClientConnections, including a local one.

protected IList<CoherenceClientConnection> AllClients { get; }

Property Value

IList<CoherenceClientConnection>

CoherenceClient

A network client used for communication with the Replication Server.

protected IClient CoherenceClient { get; }

Property Value

IClient

CurrentSimulationFrame

protected long CurrentSimulationFrame { get; }

Property Value

long

Debugger

Input debugger that stores various information about the input system for each frame. Upon disconnect the information is dumped to a specified location defined by the XXX (by default to a file).

protected CoherenceInputDebugger Debugger { get; }

Property Value

CoherenceInputDebugger

FixedTimeStep

A fixed time step as set in the . By default set to fixedDeltaTime.

protected float FixedTimeStep { get; }

Property Value

float

LocalClient

Instance of the local CoherenceClientConnection.

protected CoherenceClientConnection LocalClient { get; }

Property Value

CoherenceClientConnection

SimulationEnabled

If set to false, the simulation code won't be run, however:

* Frame progression is not stopped

* The OnBeforeSimulate() is still called

Can be used for example to wait for all players before starting the simulation.
protected bool SimulationEnabled { get; set; }

Property Value

bool

StateStore

Stores the simulation states used for rollbacks in case of a misprediction.

protected SimulationStateStore<TState> StateStore { get; }

Property Value

SimulationStateStore<TState>

Remarks

Used internally by the CoherenceInputSimulation<TState>, manual use recommended only in advanced cases.

Methods

CreateState()

Called at the end of the simulation loop to get a snapshot of the simulation that can be later used in the Rollback(long, TState) if needed.

protected abstract TState CreateState()

Returns

TState

Full simulation state.

Destroy()

Called after the simulation cleanup in the Unity's OnDestroy callback.

protected virtual void Destroy()

OnBeforeSimulate()

Called as a first thing in a given simulation loop, before any of the simulation or rollback code. Can be used for example to control the SimulationEnabled flag based on the players readiness.

protected virtual void OnBeforeSimulate()

OnClientJoined(CoherenceClientConnection)

Called when a new CoherenceClientConnection joins the session (including a local one).

protected virtual void OnClientJoined(CoherenceClientConnection client)

Parameters

client CoherenceClientConnection

A client that has joined.

OnClientLeft(CoherenceClientConnection)

Called when a CoherenceClientConnection leaves the session.

protected virtual void OnClientLeft(CoherenceClientConnection client)

Parameters

client CoherenceClientConnection

A client that has left.

OnConnected()

Called when we successfully connect to the Replication Server.

protected virtual void OnConnected()

OnDisconnected()

Called when we disconnect from the Replication Server.

protected virtual void OnDisconnected()

OnPauseChange(bool)

Called when a pause or unpause happens. See the PauseAutomatically and ShouldPause for more information.

protected virtual void OnPauseChange(bool isPaused)

Parameters

isPaused bool

OnStart()

Called after the simulation initializes in the Unity's Start callback.

protected virtual void OnStart()

Rollback(long, TState)

Called when the simulation should be rolled back to a given state. The state should be used to set simulation state back to how it was as of the toFrame.

protected abstract void Rollback(long toFrame, TState state)

Parameters

toFrame long

Frame to which we're rolling back.

state TState

State as of the toFrame.

SetInputs(CoherenceClientConnection)

Called when the local client should set inputs using the CoherenceInput.

protected abstract void SetInputs(CoherenceClientConnection client)

Parameters

client CoherenceClientConnection

The local client which should set inputs.

Simulate(long)

Called when a simulation must happen for a given simulationFrame. This is where the core of the simulation logic should be implemented.

protected abstract void Simulate(long simulationFrame)

Parameters

simulationFrame long

Frame for which the simulation is run.

Remarks

This method is called for both standard simulation and resimulation.

Start()

Used internally, DON'T hide it by defining another Start function - use the OnStart() instead.

protected void Start()

TryGetClient(ClientID, out CoherenceClientConnection)

protected bool TryGetClient(ClientID clientId, out CoherenceClientConnection client)

Parameters

clientId ClientID

The ClientId.

client CoherenceClientConnection

A client for given ID or null if none was found.

Returns

bool

True if the client with a given ID was found.

Events

BridgeResolve

Called by the Entity/Component to find the correct CoherenceBridge instance. The order of CoherenceBridge resolution is as follows:

* CoherenceBridge provided by the per-instance CoherenceBridgeResolver<T> callback

* CoherenceBridge provided by the global BridgeResolve callback

* CoherenceBridge within the same scene as this Entity/Component

* CoherenceBridge with a checked

* Creating new CoherenceBridge instance

public event CoherenceBridgeResolver<CoherenceInputSimulation<TState>> BridgeResolve

Event Type

CoherenceBridgeResolver<CoherenceInputSimulation<TState>>