Pool deck
A dealer that hands out networked copies from an ST Networked Pool. Click the dealer to deal one copy; Return last and Return all put copies back. Every player sees the same copies out, also players who join later.
- The dealer is an object with an ST Networked Pool, a collider to click and
PoolDeck. Set the pool's Template to an inactive token next to the dealer, andPoolDeck's Pool to the pool. - Any player can deal.
Interactmakes the player the pool's owner, and the deal happens inOnOwnershipTransferredonce ownership has arrived (or at once when they already own it). - The buttons are UI Buttons whose On Click calls
ReturnLastandReturnAll. - The copies are laid out in a row in
Start, the same way on every device, so nothing about their positions is sent. Give the template an ST Pickup when players should move the copies, and keep it (so all the copies) under a parent at 0,0,0, so their positions sync correctly. - The label counts the copies out with
activeCount, which is the same for everyone.
PoolDeck.cs
using StageTime.SDK;
using TMPro;
using UnityEngine;
/// <summary>
/// Deals networked copies from an ST Networked Pool: click the dealer (Interact) to deal one copy, and hook ReturnLast
/// and ReturnAll to Buttons' On Click. Any player can deal: the script first makes them the pool's owner and deals
/// once ownership has arrived (OnOwnershipTransferred). Everyone sees the same copies out, also players who join later.
/// Put the script on the GameObject with the ST Networked Pool (and a collider to click). The copies are laid out in a
/// row from the template, Spacing apart, the same way on every device.
/// </summary>
public class PoolDeck : StageTimeBehaviour
{
public STNetworkedPool pool;
public float spacing = 0.3f;
public TMP_Text countLabel;
private int pendingDeals; // deals asked for before this player owned the pool
private bool pendingReturnAll;
private int shownCount = -1;
private void Start()
{
Vector3 first = pool.GetCopy(0).transform.localPosition;
for (int i = 1; i < pool.capacity; i++)
pool.GetCopy(i).transform.localPosition = first + new Vector3(i * spacing, 0f, 0f);
}
private void Update()
{
int count = pool.activeCount;
if (count == shownCount || countLabel == null) return;
shownCount = count;
countLabel.text = count + " / " + pool.capacity + " out";
}
public override void Interact()
{
pendingDeals++;
TakeOwnershipThenAct();
}
public void ReturnLast()
{
if (!Networking.IsOwner(gameObject)) return; // only the dealer returns single copies
for (int i = pool.capacity - 1; i >= 0; i--)
{
GameObject copy = pool.GetCopy(i);
if (!pool.IsSpawned(copy)) continue;
pool.Return(copy);
return;
}
}
public void ReturnAll()
{
pendingReturnAll = true;
TakeOwnershipThenAct();
}
public override void OnOwnershipTransferred(StageTimePlayer player)
{
if (player.isLocal) Act();
else
{
pendingDeals = 0; // someone else took the pool first
pendingReturnAll = false;
}
}
private void TakeOwnershipThenAct()
{
if (Networking.IsOwner(gameObject)) Act();
else Networking.SetOwner(Networking.LocalPlayer, gameObject);
}
private void Act()
{
if (pendingReturnAll)
{
for (int i = 0; i < pool.capacity; i++) pool.Return(pool.GetCopy(i));
// Still out right after SetOwner (Return was ignored): OnOwnershipTransferred tries again.
if (pool.activeCount == 0) pendingReturnAll = false;
}
while (pendingDeals > 0)
{
if (pool.activeCount >= pool.capacity)
{
pendingDeals = 0; // every copy is out
return;
}
// Null right after SetOwner (ownership is on its way): OnOwnershipTransferred deals then.
if (pool.TryToSpawn() == null) return;
pendingDeals--;
}
}
}