Lua Scripting
How to write Lua scripts for use in OtS.
Scripting allows you to write custom Lua scripts to programatically manipulate voxel builds and block builds. OtS provides APIs for reading and writing voxels/blocks that can be executed to create a build or applied as a modifier on an existing build.
When scripts are applied as modifiers, you have access to the current state of the voxel/block structure via functions such as ots.voxels.get
and ots.blocks.get
.
When a script is applied
from scratch there will be no blocks unless you add them yourself.
Knowledge of basic Lua programming is required. If you need help getting started learning Lua, visit https://www.lua.org/start.html.
OtS includes a full Lua 5.4.7 interpreter which includes support for tables, functions, among other features. The standard libraries that are enabled include:
- basic library,
- math library,
- string library,
- table library,
- utf8 library.
Setting up VSCode
Using VSCode is not required. This guide was written for VSCode 1.99.3.
Lua files should be written in a traditional text editor and then imported into OtS. Currently OtS does not have a built-in code editor. Below are steps on how to get VSCode setup to recognise the OtS API to provide useful Intellisense information when writing your scripts.
Download VSCode, if you haven't already.
Go to Extensions and download the 'Lua' extension by sumneko to provide a language server for Lua as VSCode does not include one out-the-box.
Download the provided workspace file which includes type definitions for the OtS library.
In VSCode, go to File > Open Workspace from File... and select the downloaded workspace file.
You're ready to start scripting! Create a new file in that folder with the .lua extension and start writing your script.
For example scripts, jump to the examples or continue reading for the API overview.
Voxel Scripts
Voxel scripts operate on voxels and the API is provided in the ots.voxels
namespace.
API
The latest API version is v2. Older API versions will be marked as deprecated but will remain available for use.
Util type annotations
ots.voxels.set - Place a voxel at a location with a colour
ots.voxels.get - Get the voxel at a location
ots.voxels.get_all - Get a list of all voxels
ots.voxels.remove - Remove a voxel at a location
ots.voxels.remove_all - Remove all voxels
ots.voxels.for_each - Iterate over all voxels and call the provided function on each
Deprecated APIs
Examples
Basic operations
Loops
Functions
Custom modifiers
Block Scripts
Block scripts operate on blocks and the API is provided in the ots.blocks
namespace.
API
The latest API version is v2. Older API versions will be marked as deprecated but will remain available for use.
Util type annotations
ots.blocks.set - Place a block at a location with a colour
ots.blocks.get - Get the block at a location
ots.blocks.get_all - Get a list of all blocks
ots.blocks.remove - Remove a block at a location
ots.blocks.remove_all - Remove all blocks
ots.blocks.for_each - Iterate over all blocks and call the provided function on each
Deprecated APIs
Examples
Basic operations
Loops
Functions
Custom modifiers
Troubleshooting
Check your parenthesis are correct.
For example, functions such as ots.voxels.set
and ots.blocks.set
expect 2 arguments that are tables.
Check the type annotations and examples for help.
Positions should be integer values not floats
Functions that require positions require the values are whole numbers (integers) instead of numbers with decimal places (floats).
OtS will not automatically round the values for you. Your scripts will fail if you do not convert your floats to integers with functions like math.floor
.
For example: