Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Fluids API

Each block can define a fluid it holds and a fill level.

Constants: fluid gravity

Defines the direction in which the fluid level decreases.

NameDescription
fluids.GRAVITY_DOWNThe fluid sticks to the bottom face of a block
fluids.GRAVITY_UPThe fluid sticks to the top face of a block

The figure shows the two cases. The fluid rests on the bottom face or on the top face of its cell.

Constant: null fluid

fluids.NULL_FLUID is an empty or non-existent fluid ID.

Functions

Function: fluids.add(name, def) -> integer

Registers a new fluid in the registry.

Arguments

  • name is a namespaced fluid ID, eg mymod:mywater
  • def is a fluid definition table (see below)

Return value

Returns the numeric fluid ID.

Notes

Rename rule: on conflict, the engine adds a ~N suffix to the encroaching name. N is the smallest integer, starting at 1, that gives a free id (e.g. mymod:mywater~1). Loading stays deterministic for the same mod list and order.

Function: fluids.get(name) -> integer

Looks up a registered fluid by name.

Arguments

  • name is a namespaced fluid ID, eg mymod:mywater

Return value

  • The numeric fluid ID
  • fluids.NULL_FLUID if the fluid does not exist, or the registries have not committed yet

Fluid definition

FieldTypeRequiredDefaultDescription
gravityintegeryesN/DOne of fluids.GRAVITY_XXXX constants
albedotableyesN/DFluid albedo textures. still and flowing are both required, and masks is optional
maskstableno{}Fluid mask textures
full_levelintegeryesN/DLevel that counts as a full cell, from 1 to 15. The surface height is that many 1/16ths of a block
physicstableyesN/DMovement values inside the fluid (see below)
opaquebooleannofalseIf true, fluid quads use the blocks.RENDER_SOLID step instead of the fluid step
tintstringnonilNamespaced tint id
fog_densitynumberno1.0When the client camera is inside this fluid, the engine divides fog distance by this value. The engine clamps it to at least 1.0
fog_colornumber[3]no{1,1,1}When the client camera is inside this fluid, the engine replaces fog color with this value

Fluid physics

The physics table holds the movement values that the engine uses while an entity is inside the fluid. Every field is required.

FieldTypeDescription
dragnumberMultiplier on the velocity of the entity each fixed tick
gravity_scalenumberShare of gravity that acts on the entity
input_scalenumberScale of the horizontal input
swim_speednumberUpward movement that the jump input adds each fixed tick
climb_speednumberUpward movement when the entity jumps against a wall
flow_scalenumberScale of the fluid flow that pushes the entity. The engine multiplies it by the submersion

The built-in water uses these values:

physics = {
    drag = 0.8,
    gravity_scale = 0.0625,
    input_scale = 0.02,
    swim_speed = 0.04,
    climb_speed = 0.3,
    flow_scale = 0.014,
}

Fluid textures

  • The engine assumes that every fluid animates. It does not randomize texture frames by world position.
  • Fluids define only two hard-coded texture slots: still and flowing.
  • Texture syntax otherwise matches block definitions, both for albedo and for mask textures.