Lua Scripting
How to write scripts to edit/create voxels or blocks.
Scripting allows you to write custom Lua scripts to manipulate voxels or blocks.
Knowledge of basic Lua programming is required. If you need help getting started learning Lua, visit https://www.lua.org/start.html.
Lua scripts can be applied in a few places. They can be applied as custom voxel/block modifiers or if you are using the Project workflow then they can also be used to create a new Voxels or Blocks objects.
The editor provides 2 different APIs. One for reading/writing voxels and another for reading/writing blocks. Scripts using the voxels API can only be applied to Voxels objects. Scripts using the blocks API can only be applied to Blocks objects.
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.
API
Voxel Scripts
Block Scripts
Examples
Voxel Scripts
Basic operations
Loops
Functions
Custom modifiers
Block Scripts
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: