Resetting Game State
Article was written against Quantum version 1.1.8
If you want to reset the game, use this code (in quantum simulation code):
C#
// Only restart game on verified frames, to ensure no rollbacks occur.
// You need to set "Expose verified state to simulation" in DeterministicConfig.
if (frame.IsVerified) {
Log.Info("Restarting map from Quantum!");
frame.resetGameState();
// Dispatch event to Unity, you probably want to reload the map in Unity side on state restart.
frame.Events.MapRestart();
}
You need to add resetGameState()
to the Frame:
C#
public partial class Frame {
public void resetGameState() {
Native.Zero((byte*)_entities, sizeof(_entities_));
var physics = _globals->PhysicsSettings;
var rng = _globals->RngSession;
var map = _globals->Map;
var delta = _globals->DeltaTime;
Native.Zero((byte*)_globals, sizeof(_globals_));
_globals->DeltaTime = delta;
_globals->PhysicsSettings = physics;
_globals->RngSession = rng;
_globals->Map = map;
InitGen();
// First set the bitset for enabled systems, because we don't know how quantum operates
// internally, so it's better to be on the safe side. For example signals might not fire if
// a system is not enabled.
for (var idx = 0; idx < _systems.Length; idx++) {
if (_systems[idx].StartEnabled) BitSet256.Set(&_globals->Systems, idx);
}
for (var idx = 0; idx < _systems.Length; idx++) {
var system = _systems[idx];
system.OnInit(this); // OnInit is called once even if system is disabled.
if (system.StartEnabled) system.OnEnabled(this);
}
}
}
Back to top