Chunk module

For working with chunks.

A chunk represents a 32 tile2 on a surface in Factorio.

See also

Usage

local Chunk = require('stdlib/area/chunk')

Functions

from_position (position) Gets the chunk position of a chunk where the specified position resides.
get_data (surface, chunk_pos[, default_value]) Gets the user data that is associated with a chunk.
get_index (surface, chunk_pos) Calculates and returns a stable and deterministic integer ID of a chunk from a given chunk position.
set_data (surface, chunk_pos, data) Associates the user data to a chunk.
to_area (chunk_pos) Gets the area of a chunk from the specified chunk position.

Functions

# from_position (position)

Gets the chunk position of a chunk where the specified position resides.

Parameters:
  • position : (Position) a position residing somewhere in a chunk
Returns: Usage:
local chunk_x = Chunk.from_position(pos).x
# get_data (surface, chunk_pos[, default_value])

Gets the user data that is associated with a chunk.

The user data is stored in the global object and it persists between loads.

Parameters:
  • surface : (LuaSurface) the surface on which the user data is looked up
  • chunk_pos : (ChunkPosition) the chunk position on which the user data is looked up
  • default_value : (Mixed) the user data to set for the chunk and returned if the chunk had no user data (optional)
Returns:
  • (nil or Mixed) the user data OR nil if it does not exist for the chunk and if no default_value was set
# get_index (surface, chunk_pos)

Calculates and returns a stable and deterministic integer ID of a chunk from a given chunk position.

The chunk ID will not change once it is calculated.

Parameters: Returns:
  • (int) the chunk ID
# set_data (surface, chunk_pos, data)

Associates the user data to a chunk.

The user data will be stored in the global object and it will persist between loads.

Parameters:
  • surface : (LuaSurface) the surface on which the user data will reside
  • chunk_pos : (ChunkPosition) the chunk position to associate with the user data
  • data : (nil or Mixed) the user data to set OR nil to erase the existing user data for the chunk
Returns:
  • (nil or Mixed) the previous user data associated with the chunk OR nil if the chunk had no previous user data
# to_area (chunk_pos)

Gets the area of a chunk from the specified chunk position.

Parameters: Returns: