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.
- Listed in Global namespace: A to B
- Inherits or implements
NetworkEntityBehaviour,ISavable<ButtonCounterSaveData>,ISavableSave<ButtonCounterSaveData>,ISavableSaveBase - Derived types
ButtonCounterSaveData - Nested types
AmountChangedHandler,CounterMode,CountSyncMode,TextRendererSettings
Constructors
| Member | What it is |
|---|---|
ButtonCounter() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
18 private and internal fields
| Member | What it is |
|---|---|
private IPlayer activePlayer | The player allowed to change the counter in single client mode. Backing field for ActivePlayer. |
private int amount | The current count; saved. Backing field for Amount. |
private int cap | Extra upper limit below maximum, set by code. Backing field for Cap. |
private float changingFinished | Time after which the delayed sync can go out. |
private float changingWaitDuration | Seconds without a change before a delayed sync is sent. |
private PhysicalButton down | Physical button that decrements. |
private AltaCoroutine downButtonPressed | Unused. |
private ButtonCounter.TextRendererSettings[] labels | Text renderers that show the amount, each hidden below its own minimum. |
private int maximum | Highest amount. Backing field for Maximum. |
private int minimum | Lowest amount. Backing field for Minimum. |
private ButtonCounter.CounterMode mode | Who owns the count: the server or a single client. Backing field for Mode. |
private MethodSync sync | Method sync for the amount. |
private ButtonCounter.CountSyncMode syncMode | Whether to sync on every change or once changes stop. Backing field for SyncMode. |
private MethodSync syncPlayer | Method sync for the active player. |
private ThrottleCounter throttleCounter | Optional throttle input that changes the amount by a delta. |
private PhysicalButton up | Physical button that increments. |
private AltaCoroutine upButtonPressed | Unused. |
private AltaCoroutine waitingForChangeToFinish | The running WaitWhileChanging coroutine, if any. |
Properties
| Member | What 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
| Member | What it is |
|---|---|
event ButtonCounter.AmountChangedHandler AmountChanged | Raised whenever the amount changes. |
Methods
| Member | What 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
| Member | What 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: