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

Blocks API

Mods register new block types and read existing ones through the blocks library.

Constants: render mode

Defines a block rendering step.

NameDescription
blocks.RENDER_NONEThe engine draws no geometry (e.g. air)
blocks.RENDER_SOLIDThe engine draws the block as opaque, cullable geometry
blocks.RENDER_ALPHAThe engine draws the block as translucent geometry

Constants: block face

Defines a block face used for culling and other grid-aligned operations.

NameDescription
blocks.FACE_NORTHNorth face
blocks.FACE_SOUTHSouth face
blocks.FACE_EASTEast face
blocks.FACE_WESTWest face
blocks.FACE_TOPTop or upper face
blocks.FACE_BOTTOMBottom or lower face
blocks.FACE_UPSame as blocks.FACE_TOP
blocks.FACE_DOWNSame as blocks.FACE_BOTTOM

Constants: tool conditionals

Defines a tool category used for block interactions.

NameDescription
blocks.TOOL_NONEBare hands
blocks.TOOL_BLADEA bladed tool (eg. sword, knife)
blocks.TOOL_XBLADEA multi-bladed tool (eg. scissors)
blocks.TOOL_AXEAxe-like tool
blocks.TOOL_HOEHoe-like tool
blocks.TOOL_SHOVELShovel-like tool
blocks.TOOL_HAMMERHammer-like tool
blocks.TOOL_PICKPickaxe-like tool

Constants: category bits

Freeform grouping tags checked at runtime with blocks.has_tag. Any block can carry any number of tags via the tags registration field.

NameDescription
blocks.TAG_GASNon-solid, passable block (eg. air)
blocks.TAG_ROCKStone-family block. Used by tools and worldgen that check for rock
blocks.TAG_SOILSoil-type block. Dirt, mud, and similar blocks go here
blocks.TAG_TURFTurf-type block. Grass and similar blocks go here
blocks.TAG_XFOILNon-solid foliage: grass, bushes, and similar
blocks.TAG_SFOILSolid foliage: leaves and similar
blocks.TAG_WOODWooden blocks

Constants: schedule source

The engine issues a scheduled tick after a specific timeout, not at random. Scheduled ticks can come from different sources.

NameDescription
blocks.TICK_RANDOMRandom tick
blocks.TICK_NEIGHBOURNeighbouring block changed
blocks.TICK_SCRIPTEDScripted tick

Constant: null block

blocks.NULL_BLOCK is an empty, undefined, or otherwise invalid block ID. Treat it as a void block for gameplay and gamedev.

Functions

Function: blocks.get(name) -> integer

Retrieves a numeric block stem ID from a namespaced block ID.

Arguments

  • name is a namespaced block ID, eg. mymod:coolblockname

Return value

  • Numeric block ID on success
  • blocks.NULL_BLOCK if the block is missing or the namespace ID has an invalid form

Notes

  • If a block defines states, the return value is a stem ID. The engine does not render, collide with, or raycast a stem placed in the world.

Function: blocks.has_tag(id, tag) -> boolean

Checks if a block has a specified tag.

Arguments

  • id is a numeric block ID
  • tag is a tag constant, eg. blocks.TAG_SOIL

Return value

  • true if the block has the specified tag, false otherwise

Function: blocks.is_replaceable(id) -> boolean

Checks if a block ID can be overwritten without breaking it first (empty cell, or replaceable = true at registration).

Arguments

  • id is a numeric block ID (stem or variant), or blocks.NULL_BLOCK for an empty cell

Return value

  • true if another block may occupy the cell, false otherwise

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

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

Registers a new block in the registry.

Arguments

  • name is a namespaced block ID, eg mymod:coolblockname
  • def is a block definition table (see below)
  • prototype (3-argument form) is a base definition shared across a family of blocks (eg. all stone variants). def merges on top of prototype. On conflict, fields in def win.

Return value

Returns the numeric block 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:myblock~1). Loading stays deterministic for the same mod list and order.

Block definition

FieldTypeRequiredDefaultDescription
renderintegernoblocks.RENDER_NONEOne of blocks.RENDER_XXXX constants
albedotabledepends{}Albedo textures to attach to the block model
maskstableno{}Mask textures to attach to the block model
animatedbooleannofalseIf true, the engine treats the albedo slots as animation frames instead of world-position random frames
model_namestringdependsN/DBlock model name for this variant
model_offsetnumber[3]depends{0, 0, 0}Offset of the resolved block model
model_facingintegernoblocks.FACE_NORTHOne of blocks.FACE_XXXX. Sets where the model’s own north face points, and rotates the whole resolved block model
bcoll_namestringdependsN/DBlock collision shape for this variant
bcoll_offsetnumber[3]depends{0, 0, 0}Block collision offset
bcoll_facingintegernoblocks.FACE_NORTHOne of blocks.FACE_XXXX. Rotates the resolved collision shape the same way model_facing rotates the model. You can set it independently of model_facing. The collision shape does not need to match the visual, though it usually should
fluid_namestringnoN/DFluid name that shares the grid cell with the block
fluid_levelintegernofluid full_levelFluid level when fluid_name is present, in 1/16ths of a block. When omitted, the engine uses the fluid’s full_level
fluid_facesintegerno0Bitmask of faces a fluid may enter or leave this variant through, made of blocks.FACE_* bits. 0 means no face
fluid_fallingbooleannofalseIf true, the engine treats the fluid in this cell as falling. The cell uses the flowing texture at every level, and the flow gains a strong push along the fluid gravity
healthintegerno0Base hit points needed to break the block
soundstringnoN/DSound set for this block
emissionintegerno0Emission light value, from 0 to 15. The engine clamps values outside the range
dissipationintegerno0Opacity of the block, from 0 to 15. Light loses this many levels when it passes through. The engine clamps values outside the range. The engine treats a RENDER_SOLID block with 0 dissipation as fully opaque. The block absorbs light across every cell face that its collision bounds touch, so a shape whose bounds fill the cell is opaque even when its geometry has a notch
collidablebooleannotrueIf true, the engine uses the collision shape to stop entity movement. A fluid does not pass through the block
selectablebooleannotrueIf true, a raycast can hit the block for selection and placement
frictionnumberno0.6Surface friction. A lower value makes the surface more slippery. The value must be greater than zero
speednumberno1.0Walk speed multiplier on this surface. The value must not be negative
bouncenumber[3]no{0, 0, 0}Restitution on the X, Y and Z axes. The engine reflects the movement along a contacted axis and scales it by this value
tagsinteger[]no{}Block tags
toolsinteger[]no{}Tool bits that may drop this block, made of blocks.TOOL_* values
replaceablebooleannofalseIf true, other blocks can be placed into this cell without breaking it first
statestableno{}Blockstates table
variantstable[]no{}Variants table
on_tickfunctionnonilScheduled tick handler
on_placefunctionnonilPlacement handler. Can allow or deny placement
on_breakfunctionnonilBreak handler
on_interactfunctionnonilInteraction handler

Where callbacks run

CallbackRuns on
on_place, on_breakBoth sides. The client predicts, the server decides
on_tickServer only
on_interactServer only. The engine does not call it yet

Movement response

collidable controls collision with entities and the flow of fluids. A block with collidable = false does not stop movement, and a fluid passes through it. selectable controls selection and placement. A raycast can hit a selectable block even when the block is not collidable, as a bush is. A block without a collision shape is not selectable, because the raycast needs the shape.

The surface fields apply to an entity in walk mode:

  • friction scales the horizontal velocity and the walk acceleration. The default 0.6 matches the walk friction of the engine. A lower value makes the surface slippery like ice. A higher value stops the entity faster.
  • speed multiplies the horizontal walk input. The value 0 removes the ground input.
  • bounce gives the restitution for a contact along each axis. The engine reflects the movement along the contacted axis and multiplies it by the value. The value 0 stops the movement, and 1 keeps its speed.

The touch and touch_coeffs fields and the TOUCH_* constants no longer exist. An old script that still sets them loads, and the engine ignores the values.

Textures

Albedo

Each texture slot is a list of textures. The engine uses them as position-randomized variations, or as animation frames when animated is true.

Masks

Each texture slot can have a mask texture that the engine uses for certain purposes. Each color channel of an RGBA mask texture can drive per-face logic, and more channels will gain this role in the future.

ChannelDesignationDescription
RedTint maskThe shader multiplies the tint color with this value
GreenUnusedN/A
BlueUnusedN/A
AlphaUnusedN/A

Facing rotation

model_facing and bcoll_facing each define one 90-degree-step rotation of the whole model or collision shape. They set where the north face of the model points. By default, the north face points world north.

ValueRotationImage
blocks.FACE_NORTHNone
blocks.FACE_SOUTH180 degrees around Y
blocks.FACE_EAST-90 degrees around Y
blocks.FACE_WEST+90 degrees around Y
blocks.FACE_TOP-90 degrees around X
blocks.FACE_BOTTOM+90 degrees around X

Drops table

drops = {
  {
    when = { effects = { "silk_touch" } },
    items = {
      { name = "stone", count = 1 }
    }
  },
  {
    -- entries without a `when` always match; put fallback
    -- entries last since matching stops at the first hit
    items = {
      { name = "cobblestone", count = 1 }
    }
  }
}

NOTE: this is planned. The engine stores the entries and their conditions, but nothing selects a drop yet, and no effect state exists that a when clause could match.

States table

states = {
  <state_name> = {
    default = "<default_state>",
    hint = { "<value_a>", "<value_b>", ... }  -- optional
  }
}

The engine stores blockstate values as hashed strings. You can write any value via world.set_state. hint does not restrict this at runtime.

NOTE: this is planned. hint is stored but never checked, so a typo such as orientation = "bottum" passes without a warning. The check is meant to compare every when clause against the union of the hint lists and warn at load time.

State object

FieldTypeRequiredDefaultDescription
defaultstringyesN/DDefault blockstate value
hintstring[]no{}Optional list of valid values for registration-time validation

Variants

variants = {
  {
    when = { <state_name> = "<state_value>" },
    overrides = {
      model_name = "slab_top",
      bcoll_offset = { 0, 8, 0 },
      health = 2,
      replaceable = true,
      drops = { ... }
    }
  }
}

overrides is a table of registration fields that merges on top of the base definition when when matches the current blockstate values. It accepts the fields of a definition except tools, states, variants and the callbacks.

Target table

Some callbacks pass a target table for the block the initiator looked at (the raycast hit). In on_place, this is the block adjacent to the placement cell.

The figure shows these fields on one hit against the top face.

FieldTypeDescription
stemintegerNumeric block stem ID of the hit block
faceintegerHit surface direction
ninumberSurface normal X/I component
njnumberSurface normal Y/J component
nknumberSurface normal Z/K component
lxnumberHit point X component
lynumberHit point Y component
lznumberHit point Z component
bxintegerHit block position X component
byintegerHit block position Y component
bzintegerHit block position Z component
rxnumberBlock-local hit position X component
rynumberBlock-local hit position Y component
rznumberBlock-local hit position Z component

The on_interact handler receives the same table without stem, bx, by and bz.

Occupant table

The on_place handler receives an occupant table for the block currently in the placement cell.

FieldTypeDescription
idintegerNumeric block ID in the placement cell. blocks.NULL_BLOCK when empty
replaceableintegerWhether the placement cell can be occupied: 1 when empty or when the mod registered the existing block with replaceable, 0 otherwise

on_place handler

on_place = function(bx, by, bz, target, occupant, actor)
  if occupant.replaceable == 0 then
    return nil -- cell is occupied by a non-replaceable block
  end

  -- permits placement with these initial states
  return { <state_name> = "<state_value>", ... }
end

Returning nil blocks placement. Returning a table (empty or not) permits it. Table entries become initial blockstate values for states not covered by their default.

When the callback permits placement, the new block overwrites whatever was in the cell. The engine does not break replaceable occupants, and they do not drop items.

on_break handler

on_break = function(bx, by, bz, actor)
  -- Actions to do before the block is broken
end

Returning true keeps the placement cell as the callback left it. Otherwise the engine clears it to blocks.NULL_BLOCK after the callback returns. Callbacks that return nil or any other value keep the default behaviour.

on_interact handler

on_interact = function(bx, by, bz, target, actor)
  -- Actions to do when someone interacts with the block
end

on_tick handler

on_tick = function(bx, by, bz, source)
  -- Actions to do when the block is ticked via scheduling
  -- source is one of blocks.TICK_XXXX constants
end