Skip to content

Testing, Replays and Determinism

A match is fully determined by its rules, its seed and its commands. The core uses whole numbers only, keeps its own seeded random number generator in the game state, and never reads the clock or frame time. Keep your own nodes, rules and commands the same way:

  • draw random numbers from context.State.Rng, never UnityEngine.Random or System.Random;
  • avoid floating point maths in rules;
  • do not depend on the iteration order of dictionaries or hash sets;
  • keep no state outside the GameState.

GameState.Hash() fingerprints the whole state. Equal states always have equal hashes, which makes “did these two runs end the same?” a one-line check.

MatchHost.Record keeps the seed, each player’s deck and every accepted command, including the computer’s. MatchRecord.Replay(rules) plays it back to the same state; pass a command count to stop part way. Save records as bytes with MatchRecord.ToBytes, or use Save Replay in the Match Debugger, and attach them to bug reports.

If a replay is refused part way, the rules have changed since it was recorded, and the error says which command no longer works.

The kit’s rules run without a scene, so most tests can be plain edit mode tests:

[Test]
public void FireballKillsASmallCreature()
{
var rules = myGame.Compile();
var match = new Match(rules, new[] { myDeck.ToSetup(), theirDeck.ToSetup() }, seed: 1);
match.Start();
// Submit commands with match.Submit(playerId, command) and check match.State.
}

match.Fork() gives an independent copy to try things on. To test a whole session, including what each player is sent, create a MatchHost with a LocalTransport and connect MatchClients to it, as the kit’s own SessionTests do.

Tests/EditMode (the rules engine), Tests/Definitions (assets and editor tools) and Tests/PlayMode (the sample’s scenes) run in the Test Runner. The online tests in Integrations/NetcodeForGameObjects/Tests run when Netcode for GameObjects is installed. They use an in-memory profile store, so they never touch your save files.