Skip to main content

ButtonCounter

A class in Root.Township, the global namespace.

A networked number set with up and down buttons (or a throttle) and shown on text labels, such as a quantity picker. Either the server or a single active player owns the changes; the value is saved.

Constructors​

MemberWhat it is
ButtonCounter()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

18 private and internal fields
MemberWhat it is
private IPlayer activePlayerThe player allowed to change the counter in single client mode. Backing field for ActivePlayer.
private int amountThe current count; saved. Backing field for Amount.
private int capExtra upper limit below maximum, set by code. Backing field for Cap.
private float changingFinishedTime after which the delayed sync can go out.
private float changingWaitDurationSeconds without a change before a delayed sync is sent.
private PhysicalButton downPhysical button that decrements.
private AltaCoroutine downButtonPressedUnused.
private ButtonCounter.TextRendererSettings[] labelsText renderers that show the amount, each hidden below its own minimum.
private int maximumHighest amount. Backing field for Maximum.
private int minimumLowest amount. Backing field for Minimum.
private ButtonCounter.CounterMode modeWho owns the count: the server or a single client. Backing field for Mode.
private MethodSync syncMethod sync for the amount.
private ButtonCounter.CountSyncMode syncModeWhether to sync on every change or once changes stop. Backing field for SyncMode.
private MethodSync syncPlayerMethod sync for the active player.
private ThrottleCounter throttleCounterOptional throttle input that changes the amount by a delta.
private PhysicalButton upPhysical button that increments.
private AltaCoroutine upButtonPressedUnused.
private AltaCoroutine waitingForChangeToFinishThe running WaitWhileChanging coroutine, if any.

Properties​

MemberWhat it is
IPlayer ActivePlayer { get; set; }The player allowed to change it in single client mode. The server syncs it.
int Amount { get; set; }The current value. Setting it clamps it, updates the labels and raises AmountChanged.
int Cap { get; set; }Extra upper limit on top of Maximum.
int Maximum { get; }Highest allowed value (250 by default).
int Minimum { get; }Lowest allowed value (1 by default).
ButtonCounter.CounterMode Mode { get; }Who owns changes: the server, or a single client (the active player).
ButtonCounter.CountSyncMode SyncMode { get; }Whether changes sync at once or after the buttons stop being pressed for a moment.

Events​

MemberWhat it is
event ButtonCounter.AmountChangedHandler AmountChangedRaised whenever the amount changes.

Methods​

MemberWhat it is
void Decrement()Lowers the amount by one, if this side owns changes.
void ForceAmount(int amount)Sets the amount and syncs it, if this side owns changes (or on the server with no active player).
void Increment()Raises the amount by one, if this side owns changes.
override void Initialize()Hooks the buttons and throttle and sets up the sync messages.
void Load(ButtonCounterSaveData data, ISerializeChildren childSerializer)Restores the saved amount.
override void OnConnected(Player player)Server: sends the amount and active player to a joining player.
ButtonCounterSaveData Save(ISerializeChildren childSerializer)Saves the amount.
9 private and internal methods
MemberWhat it is
private void ChangeBy(int delta)Adds a delta from the throttle counter, if this side may change the amount.
private void CheckSync(ButtonCounter.CountSyncMode mode)Sends the amount to chunks when the given mode matches the sync mode.
private bool IsCorrectMode()Whether this side owns the counter: the server in server mode, or the active player in single client mode.
private void OnChanged()Clamps the amount, updates labels, raises AmountChanged and syncs now or after changes stop.
private void PlayerChanged()Server: syncs the active player; on clients, enables the throttle only for the active player.
private void Sync(IPlayer player, Stream stream)Network handler for the amount; the server accepts changes only from the active player in single client mode.
private void SyncPlayer(IPlayer player, Stream stream)Network handler for the active player's ID.
private void UpdateText()Writes the amount to every label.
private IEnumerator WaitWhileChanging() (iterator)Coroutine that waits until no change has happened for changingWaitDuration, then syncs in delayed mode.

Used in​

Members of other types that take, return or hold a ButtonCounter: