Skip to main content

Scoreboard

ScoreBoard keeps per-slot scores in a synced array and talks to a separate ScoreBank script (below) through a field, method calls with results, and GetProgramVariable / SetProgramVariable. See Other scripts.

ScoreBoard.cs
using StageTime.SDK;
using TMPro;

/// <summary>
/// Script API v3 demo. "+1 My Slot" adds a point to the local player's slot (player id % 4) of a [Synced] int[4], calls
/// ScoreBank.AddPoints(1) (method with a parameter and a result), writes ScoreBank.lastSlot by name
/// (SetProgramVariable) and shows bank.total (cross-script field read) and the Counter station's private [Synced]
/// count (GetProgramVariable).
/// </summary>
public class ScoreBoard : StageTimeBehaviour
{
public TMP_Text label;
public ScoreBank bank;
public Counter counter;

[Synced] private int[] scores = new int[4];

private int lastResult;

private void Start()
{
Show();
SendCustomEventDelayedSeconds("_Refresh", 1f);
}

public void AddToMySlot()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
int slot = Networking.LocalPlayer.playerId % scores.Length;
if (slot < 0) slot = -slot;
scores[slot] += 1;
if (bank != null)
{
lastResult = bank.AddPoints(1);
bank.SetProgramVariable("lastSlot", slot);
}
Show();
}

public void ResetScores()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
for (int i = 0; i < scores.Length; i++) scores[i] = 0;
if (bank != null) bank.ResetBank();
lastResult = 0;
Show();
}

public override void OnDeserialization()
{
Show();
}

/// <summary>Picks up the Counter station's count once a second (it does not notify this script).</summary>
public void _Refresh()
{
Show();
SendCustomEventDelayedSeconds("_Refresh", 1f);
}

private void Show()
{
if (label == null) return;
string text = "Scores (slot = player id % 4)\n";
for (int i = 0; i < scores.Length; i++) text += $"Slot {i}: {scores[i]}\n";
if (bank != null)
{
int last = (int)bank.GetProgramVariable("lastSlot");
text += $"Bank total: {bank.total} (AddPoints returned {lastResult}, last slot {last})\n";
}
if (counter != null) text += $"Counter station: {(int)counter.GetProgramVariable("count")}";
label.text = text;
}
}
ScoreBank.cs
using StageTime.SDK;

/// <summary>
/// ScoreBoard's helper on the same station: a total that another script changes through a public method with a
/// parameter and a result (AddPoints), plus a field ScoreBoard writes by name (SetProgramVariable). Both are synced:
/// AddPoints takes ownership first, so whoever presses the button can change them.
/// </summary>
public class ScoreBank : StageTimeBehaviour
{
[Synced] public int total;
[Synced] public int lastSlot = -1;

public int AddPoints(int amount)
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
total += amount;
return total;
}

public void ResetBank()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject);
total = 0;
lastSlot = -1;
}
}