Save board
A board that remembers each player between visits with PlayerData: how many times
they came, their best score, and how long ago their last visit was (with STTime.utcDay).
Click it to score a point. Wire ResetScore and ForgetMe to UI buttons for a new round and for deleting the
player's saved data. OnPlayerDataSaveFailed shows why a save failed. Put it on a TextMeshPro label.
SaveBoard.cs
using StageTime.SDK;
using TMPro;
/// <summary>
/// A board that remembers each player between visits with <c>PlayerData</c>: how many times they came, their best
/// score, and how long ago their last visit was. Click it (Interact) to score a point; <c>ResetScore</c> starts a new
/// round and <c>ForgetMe</c> deletes everything this world saved for the player (wire them to UI buttons).
/// </summary>
public class SaveBoard : StageTimeBehaviour
{
public TMP_Text label;
private int visits;
private int best;
private int score;
private string status = "";
private void Start()
{
// PlayerData is loaded before Start, so this is the saved value (0 on the first visit).
visits = PlayerData.GetInt("visits") + 1;
PlayerData.SetInt("visits", visits);
best = PlayerData.GetInt("best");
int today = STTime.utcDay;
int lastDay = PlayerData.GetInt("lastDay", -1);
PlayerData.SetInt("lastDay", today);
if (lastDay < 0) status = "First visit. Welcome!";
else if (lastDay == today) status = "Back again today";
else status = "Last visit " + (today - lastDay) + " days ago";
Refresh();
}
public override void Interact()
{
AddPoint();
}
public void AddPoint()
{
score++;
if (score > best)
{
best = score;
PlayerData.SetInt("best", best); // saved a few seconds later, and when the player leaves
}
Refresh();
}
public void ResetScore()
{
score = 0;
Refresh();
}
public void ForgetMe()
{
PlayerData.DeleteAll();
visits = 0;
best = 0;
score = 0;
status = "Saved data deleted";
Refresh();
}
public override void OnPlayerDataSaveFailed()
{
status = "Not saved: " + PlayerData.lastError;
Refresh();
}
private void Refresh()
{
if (label == null) return;
string saving = PlayerData.isAvailable
? "Saved: " + PlayerData.usedBytes + " of " + PlayerData.limitBytes + " bytes"
: "Saving is off in this world";
label.text = $"Visits: {visits}\nBest: {best}\nScore: {score}\n{status}\n{saving}";
}
}