LiquidContainer
A class in Root.Township, Alta.Liquid.
Holds a whole number of units of one liquid (a bucket, a flask, a pond). Only the server changes the contents; the level and liquid are synced, saved, and shown through an optional surface visual. Pours in progress show as fractional amounts.
- Listed in Stats, effects, heat and liquid
- Inherits or implements
GeneratedNetworkEntityBehaviour,ILiquidContainer,ILiquidContainerInternal,ISavable<LiquidContainerSaveData>,ISavableSave<LiquidContainerSaveData>,ISavableSaveBase,IEntityReplaceHandler,IBlockGenericDecrafting,IInterfereWithDocking - Derived types
StewContainer,GlobalLiquidContainer,LiquidContainerSaveData
Constructors
| Member | What it is |
|---|---|
LiquidContainer() | Unity makes components itself; use AddComponent or spawn a prefab instead of new. |
Fields
19 private and internal fields
| Member | What it is |
|---|---|
private bool canAddTo | Whether liquid may be poured in. Checked by CanAddTo before AddFilter. |
private bool canRemoveFrom | Backing field for CanRemoveFrom; saved with the container. |
private int containerPriority | Containers with a lower priority can't fill into ones with the same or higher priority. |
private int containerSize | How many units the container holds. |
private LiquidContent content | Backing field for ContainedLiquid. |
private int currentContentLevel | Units of liquid in the container; saved and synced. |
private readonly HashSet<LiquidTransition> inTransitions | Pours currently coming into this container. |
private bool isAllowingGradualConsumption | Backing field for IsAllowingGradualConsumption, which LiquidGradualDrinker checks before drinking from it bit by bit. |
private bool isInfiniteSource | When true the container never empties or fills: level reads as int.MaxValue and adds and removes always succeed. |
private Distribution liquidDistribution | Liquids the container may spawn filled with, picked at random on generation with a random amount. |
private static readonly Logger logger | Logger for this class. |
private int oldLevel | The level when ContentLevelChanged last fired. |
private float oldTotalLiquidAmountShown | The shown amount last reported, to detect changes. |
private LiquidTransition outTransition | The transition for liquid leaving this container, made in Awake. |
private readonly List<LiquidTransition> sortedInTransitions | Reused list for sorting incoming pours by progress. |
private LiquidSurfaceVisual surfaceVisual | The liquid surface renderer. Without one the update loop is never turned on. |
private MethodSync syncLiquidAmount | Sync message for the level and in-flight amount. |
private MethodSync syncLiquidContent | Sync message for the liquid type. |
private float totalTransitionsAmount | Liquid currently in flight: incoming pour progress minus the outgoing pour. |
Properties
| Member | What it is |
|---|---|
bool CanRemoveFrom { get; set; } | Whether liquid can be taken out. Saved. |
LiquidContent ContainedLiquid { get; protected set; } | The liquid inside, or null when empty. |
int ContainerPriority { get; } | A container only pours into containers of lower priority (checked by LiquidContainerTrigger). Default 1. |
int ContainerSize { get; } | Number of units it holds. |
int CurrentContentLevel { get; } | Units of liquid inside. int.MaxValue for an infinite source. |
bool IsAllowingGradualConsumption { get; } | Whether drinking can take the liquid out bit by bit. |
bool IsEmpty { get; } | True with no liquid. Never true for an infinite source. |
bool IsFull { get; } | True at full capacity. Never true for an infinite source. |
virtual float ReceiveTransitionMultiplier { get; } | Speed factor for pours into this container. 1 by default. |
int SpaceLeft { get; } | Units of free space. int.MaxValue for an infinite source. |
LiquidSurfaceVisual SurfaceVisual { get; } | The visual that shows the liquid's surface, or null. |
float TotalLiquidAmountShown { get; } | Level plus the progress of pours in and out, for display. |
2 private and internal properties
| Member | What it is |
|---|---|
private bool Alta.Crafting.IBlockGenericDecrafting.CanDecraft { get; } | Only empty containers can be decrafted. |
private LiquidTransition Alta.Liquid.ILiquidContainerInternal.OutTransition { get; } | The transition for liquid leaving this container. |
Events
| Member | What it is |
|---|---|
event LiquidFillFilter AddFilter | Lets other components refuse a liquid before it is added. |
event LiquidTypeChangedHandler ContainedLiquidChanged | Raised with the old and new liquid when it changes. |
event LiquidContainerAmountChangedHandler ContentLevelChanged | Raised with the old and new level when the level changes. |
event LiquidContainerHandler TotalAmountShownChanged | Raised when the shown amount changes, including during pours. |
Methods
| Member | What it is |
|---|---|
bool CanAddTo(LiquidContent liquid) | True if adding is allowed and no AddFilter handler refuses this liquid. |
static bool FillInto(ILiquidContainer from, ILiquidContainer to, int amountToFill, bool isForced = false) | Server: moves units from one container to another if they are compatible and there is room. Static. |
bool FillIntoContainer(ILiquidContainer otherContainer, int amountToFill, bool isForced = false) | Server: pours units from this container into another. |
void ForceEmpty() | Removes all liquid, ignoring CanRemoveFrom. |
override void Initialize() | Sets up the level and content sync messages. |
override void InitializeAsServer(SpawnData spawnData) | Server: syncs the starting level. |
virtual bool IsCompatibleWithOtherContainer(ILiquidContainer otherContainer) | True unless both containers hold different liquids. The global container always matches. |
override void OnConnected(Player player) | Server: sends the level and liquid to a joining player. |
protected virtual void Awake() | Sets up the pour-out transition. Disables updates if there is no surface visual. |
protected override void Generate(SpawnData spawnData) | On first spawn, fills a random amount of a liquid picked from the distribution, if one is set. |
protected virtual void UpdateContentRenderer() | Sets the surface visual's fullness from the shown amount. |
22 private and internal methods
| Member | What it is |
|---|---|
private bool Alta.Liquid.ILiquidContainerInternal.AddLiquid(LiquidContent containedLiquid, int amountToAdd, bool isForced) | Server: adds an amount of a liquid, capped at the container size. Takes the liquid's type if empty. Infinite sources accept anything. |
private void Alta.Liquid.ILiquidContainerInternal.AddLiquidInTransition(LiquidTransition liquidTransition) | Starts tracking a pour coming in; if empty, takes the source's liquid type so the visual shows it, and enables the update loop. |
private bool Alta.Inventory.IInterfereWithDocking.CheckAllowed(IInterfereWithDocking other) | Docking check: two containers may stack only if both are empty, or have the same level and the same liquid. |
private void CheckToDisableUpdateLoop() | Turns the update loop off and refreshes the visual once no pours are coming in. |
private void ClearContainedLiquidDueToFailedTransition(LiquidTransition failedTransition) | If an incoming pour is cancelled and nothing is shown, clears the liquid type taken from it. |
private void EnableUpdateLoop() | Server: turns the update loop on when liquid starts pouring out, if there is a surface visual. |
private void OnContainedLiquidChanged(LiquidContent oldContent, LiquidContent newContent) | Server sends the new liquid type to the chunk; raises ContainedLiquidChanged and updates the surface colour. |
private void OnContentLevelChanged() | Server sends the level to the chunk; refreshes the visual and raises ContentLevelChanged if the level changed. |
private void OnValidate() | In the editor, refreshes the visual and clears the liquid type if the level is 0. |
private bool Alta.Liquid.ILiquidContainerInternal.RemoveLiquid(int amountToRemove, bool isForced) | Server: removes an amount; clears the liquid type when it reaches 0. Infinite sources never run out. |
private void Alta.Liquid.ILiquidContainerInternal.RemoveLiquidInTransition(LiquidTransition liquidTransition) | Stops tracking an incoming pour and refreshes or disables the update loop. |
private void Alta.Networking.IEntityReplaceHandler.ReplacedWith(NetworkEntity entity, ReplaceAvoidFlags replaceFlags) | When the entity is replaced, moves the liquid (or emptiness) into the replacement's container. |
private void SerializeContentLevel(Stream stream) | Writes or reads the level as an int from 0 to containerSize. |
private void SerializeLevelAndSettingsIntoStream(Stream stream) | Writes or reads canAddTo, canRemoveFrom and the level. Not called in this build. |
private void SerializeLiquidContent(Stream stream) | Writes or reads the liquid: none, a custom mix as data, or a preset by hash. Infinite sources with none are given water. |
private void Alta.Liquid.ILiquidContainerInternal.SetContent(LiquidContent liquidContent) | Sets the liquid type directly without changing the level. |
private void SyncLiquidAmount(IPlayer player, Stream stream) | Sync handler for the level and in-flight pour amount; clients update their visual and raise events. Rejected from clients. |
private void SyncLiquidContent(IPlayer player, Stream stream) | Sync handler for the liquid type. Rejected from clients. |
private void Update() | Server: recomputes the in-flight pour amount and the visual each frame while pours are active. |
private bool UpdateTotalLiquidAmountShown() | If the shown amount changed, raises TotalAmountShownChanged and (server) sends the amount to the chunk. |
private bool UpdateTotalTransitionAmount() | Adds up incoming pour progress (largest first, as many as there is room for) minus the outgoing pour. |
private void UpdateTransitionAmountAndRenderers(bool isForced) | Recomputes the in-flight amount on the server and refreshes the visual if it changed or when forced. |
Used in
Members of other types that take, return or hold a LiquidContainer: