Skip to main content

Check that saved data survives a rejoin

ClientSim saves the PlayerData and PlayerObjects of each player to files in your project. Ending a session stops it from saving, so the next session in the same scene starts from what the last one saved, like a player who leaves and rejoins. That lets one test check that data survives a rejoin.

Steps​

  1. Start a session and change the data:

    yield return Session.Start();
    PlayerData.SetString("coins", "10");
  2. Wait a frame. ClientSim has saved the change by then:

    yield return null;
  3. End the session and start a new one, the rejoin:

    Session.End();
    yield return Session.Start();
  4. Assert on what the new session restored:

    Assert.That(PlayerData.TryGetString(Networking.LocalPlayer, "coins", out string coins), Is.True);
    Assert.That(coins, Is.EqualTo("10"));

Keep the whole rejoin inside one test. For tests without an [Order] attribute, NUnit says "execution order is indeterminate", so a test that saves and a later test that reads would pass or fail depending on that order.

Starting from no saved data​

Saved data stays on disk after a test, so a later test in the same scene starts from it too. When a test needs none, delete it before starting the session:

ClientSimSession.DeleteSavedData();
yield return Session.Start();

DeleteSavedData deletes what ClientSim saved for the active scene, for every player. In your world scene, those files are also the ones you get when you press Play. What tests save in the Unity Test Framework test scene, the kit deletes when the run ends.