Saved data (PlayerData)
PlayerData keeps values for the local player in your world between visits: progress, high scores, unlocks,
settings, the last checkpoint. The data belongs to the player and your world. It follows the player to any device,
and other worlds can't read it.
public TMP_Text label;
private int best;
void Start()
{
// Saved data is loaded before Start, so this is the value from the last visit (0 the first time).
int visits = PlayerData.GetInt("visits") + 1;
PlayerData.SetInt("visits", visits);
best = PlayerData.GetInt("best");
label.text = "Visit " + visits + ", best " + best;
}
public void Finish(int score)
{
if (score <= best) return;
best = score;
PlayerData.SetInt("best", best);
}
How it works
- Loaded before
Start. StageTime loads the player's data while the room is joining, so everyGetinStartalready returns the saved value. There is no "data loaded" event to wait for. - Saved for you. A
Setchanges the value at once. StageTime saves changed keys a few seconds later (at most every 10 seconds), and again when the player leaves the world or StageTime goes to the background. You never call Save. - Only your own data. A script reads and writes the local player's data. To show other players' scores, sync them
with
[Synced]fields or network events. - Per world. Each world has its own data. Every version of your world shares it, so keep your keys stable between updates.
- Not secret, not trusted. The player's device writes the data, so don't use it for anything that must not be cheated.
API
| API | |
|---|---|
PlayerData.SetBool(key, value) … SetColor, SetIntArray … | Stores a value. Returns false if it was refused (see Limits) |
PlayerData.GetInt(key) | The value, or the type's default (0, false, "", zero vectors, identity rotation, white) |
PlayerData.GetInt(key, defaultValue) | The value, or defaultValue when the key has no value |
PlayerData.HasKey(key) | True when the key has a value |
PlayerData.DeleteKey(key) | Removes one key |
PlayerData.DeleteAll() | Removes everything your world saved for the player |
PlayerData.isAvailable | True when the data loaded and changes are being saved |
PlayerData.usedBytes / limitBytes | Space used and allowed (102,400 bytes) |
PlayerData.keyCount | How many keys have a value |
PlayerData.lastError | Why the last value was refused or the last save failed |
Types: bool, int, float, double, string, Vector2, Vector3, Vector4, Quaternion, Color, int[],
float[] and string[], each with its own Set and Get (SetVector3, GetStringArray and so on).
- A key holds one type.
GetInton a key that holds a string returns the default. - Arrays are copied: changing the array after
SetIntArraydoesn't change the saved value. CallSetIntArrayagain.GetIntArrayreturns a new array (empty when the key has no value). - Use a
doublefor times fromSTTime.utcNow, for example the last daily reward.
// A daily reward: once per UTC day.
public void ClaimDaily()
{
if (PlayerData.GetInt("lastClaimDay", -1) == STTime.utcDay) return;
PlayerData.SetInt("lastClaimDay", STTime.utcDay);
PlayerData.SetInt("coins", PlayerData.GetInt("coins") + 100);
}
When saving isn't available
PlayerData.isAvailable is false when the data couldn't be loaded within 5 seconds (for example offline), or saving
has stopped. Reads then return defaults, and values you set last until the player leaves. Your world keeps working; it
just won't remember this visit. You can tell the player:
void Start()
{
if (!PlayerData.isAvailable) notice.text = "Progress won't be saved this visit.";
}
OnPlayerDataSaveFailed
OnPlayerDataSaveFailed() runs on the local player's device, on every script that has it, when a value was refused
or a save failed. Read PlayerData.lastError for the reason. If a script keeps setting a refused value (every
frame, say), the event runs at most once every 10 seconds for the same reason; lastError is always up to date.
public override void OnPlayerDataSaveFailed()
{
notice.text = "Not saved: " + PlayerData.lastError;
}
When StageTime's servers refuse a save (the player's space is full, for example), the values stay for this visit and later changes are still saved. Setting one of the refused keys again, even to the same value, sends it again.
Saving stops for the rest of the visit (and isAvailable becomes false) when the player deletes their saved world
data in Settings while in your world.
Limits
| Limit | Value |
|---|---|
| Space per player per world | 100 KB (102,400 bytes), measured as the saved JSON text |
| Keys | 1,024 per player per world |
| Key length | 1 to 64 bytes (UTF-8) |
| Strings | 16,384 characters |
| Text (keys and strings) | no \0 character and no half of a surrogate pair (whole emoji are fine) |
| Arrays | 65,536 elements, and they must fit in the 100 KB |
| Numbers | finite only (no NaN or infinity) |
| Space per player across all worlds | 5 MB |
A Set that breaks a limit is refused: it returns false, the old value stays, and OnPlayerDataSaveFailed runs.
A number takes about 30 bytes plus its key, so 100 KB holds a lot of scores and flags. Big text or long arrays use
it up fastest.
Testing
- Play Mode (local preview) keeps the data in
<persistentDataPath>/worlddata-preview/<scene>.jsonand saves it two seconds after a change. Delete the file to start over. - Build & Test keeps it in StageTime's local folder,
worlddata-local/<player>/<world id>-<hash>.json(the hash is of where the world was loaded from, so two test worlds never share data), one folder per signed-in player, so two test clients keep separate data. Settings → Info → World saved data → Delete all clears it. - Published worlds (uploaded to StageTime) save to StageTime's servers, for guests and signed-in players alike. If StageTime's servers are busy, a save is tried again a little later; nothing is lost while the player stays.
Players' control
Players see how much space their saved world data uses in Settings → Info and can delete all of it there.
See the Save board example.