TrueSync Manager
TrueSync Behaviour
"TrueSyncBehaviour" is a component class that inherits from Unity's standard MonoBehaviour, adding TrueSync's custom callbacks. These callbacks, together with helper methods and types aid the development of deterministic code.
Player prefabs normally have one or more scripts that inherit from TrueSyncBehaviour attached to them.
Inheriting from TrueSyncBehaviour
The code bellow shows a basic script that inherits from TrueSyncBehaviour and uses the most common callbacks (explained next).
C#
using UnityEngine;
using System.Collections;
using TrueSync;
public class GameScript : TrueSyncBehaviour {
public override void OnSyncedStart () {
// synchronous lockstep initialization
}
public override void OnSyncedInput() {
// called every lockstep frame only on the client of the player that owns this game object
}
public override void OnSyncedUpdate () {
// called every lockstep frame on all clients
}
}
OnSyncedStart
This method is used for deterministic initialization of game objects. It is guaranteed to be executed in the first frame of the lockstep simulation on all connected clients.
TrueSync also guarantees that the relative order in which all instances of "OnSyncedStart" is called among the game objects is preserved across all clients.
A common use of "OnSyncedStart" is to reposition a game object based on either a player property or a random number (which also needs to be deterministic). The code bellow moves the object to a random position on the X axis.
C#
public override void OnSyncedStart () {
tsTransform.position = new TSVector(TSRandom.range(-5, 5), 0, 0);
}
Notice the use of the custom property "tsTransform", which is a reference to the "TSTransform" component, a deterministic clone of Unity's standard transform;"TSVector", a deterministic version of Vector3; and TSRandom, TrueSync's custom seeded deterministic random number generator.
You can find more information on TrueSync's helper types and methods in the section on determinism.
OnSyncedInput
In a lockstep simulation, input is collected for the local player, queued for use by the local game objects, and distributed over the connected clients for advancing the remote simulation.
In TrueSync, you need to override "OnSyncedInput", collect user input from Unity and queue it using one of the available deterministic types. The code bellow queues a Unity input axis as a FixedPoint (FP) number.
C#
public override void OnSyncedInput() {
FP accell = Input.GetAxis ("Vertical");
TrueSyncInput.SetFP (0, accell);
}
The "TrueSyncInput" static class has one setter method for each of the available deterministic types (byte, int, FP, string, TSVector). A setter method takes two parameters: a unique key value to identify the input variable (byte); and the input value itself.
TrueSync is responsible for distributing the input both to the local copies of the game objects, and to the remote ones on all connected clients.
OnSyncedUpdate
"OnSyncedUpdate" is called every lockstep frame in all clients for all game objects that have at least one "TrueSyncBehaviour" attached to it.
It is used to update the game state using deterministic code, so the simulation results in the same state changes across all clients.
ATTENTION: never update relevant game state in Unity's standard Update callback. The reason is that Unity's normal update loop is not deterministic and not guaranteed to be executed at the same time in all connected clients as TrueSync's is.
The following code use the previously queued input to move the game object by calling the "Translate" method in its "TSTransform".
C#
public override void OnSyncedUpdate () {
FP accell = TrueSyncInput.GetFP (0);
tsTransform.Translate (0, 0, accell * TrueSyncManager.DeltaTime);
}
Notice the use of TrueSync's custom deterministic "DeltaTime" instead of Unity's standard non-deterministic delta time.
Other Callbacks and Features
A TrueSyncBehaviour also receives 3D and 2D physics callbacks from TrueSync, and has custom methods and attributes.
We recommend checking the API reference for a complete and comprehensive list.
Back to top