This document is about: QUANTUM 2
SWITCH TO

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, you could 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 you have access to 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.

You can multiply the current frame number minus the RollbackWindow with the the delta time of a tick.

Accumulate Time as a Global Variable

If you need to change Deltatime at runtime, or want to suspend 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