Class CoherenceInputSimulation<TState>
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
coherenceBridge
public CoherenceBridge coherenceBridge
Field Value
Properties
AllClients
All instances of the connected CoherenceClientConnections, including a local one.
protected IList<CoherenceClientConnection> AllClients { get; }
Property Value
CoherenceClient
A network client used for communication with the Replication Server.
protected IClient CoherenceClient { get; }
Property Value
CurrentSimulationFrame
protected long CurrentSimulationFrame { get; }
Property Value
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
FixedTimeStep
A fixed time step as set in the NetworkTime. By default set to fixedDeltaTime.
protected float FixedTimeStep { get; }
Property Value
LocalClient
Instance of the local CoherenceClientConnection.
protected CoherenceClientConnection LocalClient { get; }
Property Value
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
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
CoherenceClientConnectionA client that has joined.
OnClientLeft(CoherenceClientConnection)
Called when a CoherenceClientConnection leaves the session.
protected virtual void OnClientLeft(CoherenceClientConnection client)
Parameters
client
CoherenceClientConnectionA 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
longFrame to which we're rolling back.
state
TStateState 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
CoherenceClientConnectionThe 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
longFrame 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)
Returns a CoherenceClientConnection by its ClientId.
protected bool TryGetClient(ClientID clientId, out CoherenceClientConnection client)
Parameters
clientId
ClientIDThe ClientId.
client
CoherenceClientConnectionA 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 CoherenceBridge.IsSingleton checked
* Creating new CoherenceBridge instance
public event CoherenceBridgeResolver<CoherenceInputSimulation<TState>> BridgeResolve