Elapsed Time
Introduction
There are two ways to calculate the elapsed time in Quantum:
- based on the Frame.Number; and,
- based on the accumulated deltaTime.
Time based on the Frame Number
The simplest way calculate the time lapsed since the start of a session is based on frame.Number
:
frame.Number * frame.deltaTime;
or
frame.Number / frame.UpdateRate
Both calculations will return a time in seconds.
Note: frame.Number
STARTS at RollbackWindow
, not at 0!
This will accurately and deterministically track the total time lapsed, i.e. it will be identical in all game clients at the same tick number.
Extending the Frame
To facilitate access to the result, it is possible to implement the following snippet in the Frame's partial definition of Frame.User.cs
:
C#
namespace Quantum {
unsafe partial class Frame {
public FP ElapsedTime {
get {
return DeltaTime * (Number - SessionConfig.RollbackWindow);
}
}
}
}
From Unity
From Unity it is possible to access the SimulationTimeElapsed
property in the Deterministic Session.
C#
QuantumRunner.Default.Game.Session.SimulationTimeElapsed
Note: This returns a double based on the predicted frame number and the simulation delta time. It does not does not take the RollbackWindow into consideration.
Accumulate Time as a Global Variable
If you need to change deltaTime at runtime, or want to pause a game and pick-up it later, you will have to track the accumulated deltatime manually.
C#
// add a global variable to your qtn-file
global {
FP ElapsedTime;
}
// create a system
public unsafe class TimeSystem : SystemMainThread {
public override void Update(Frame f) {
f.Global->ElapsedTime += f.DeltaTime;
}
}
Note: The precision required by FP will eventually lead to inaccuracies. A way to improve precision is to keep track of ticks instead of time; these can be counted using an Int32.
Back to top