# lua

a lightweight embeddable scripting language. - Lua / online / Lua Guide

Pico-8

Pico-8 lua is not standard

  • it has some addition (+=, if() oneline, ..)
    • all, add - some which can be emulated on standard lua
  • it lack some standard fonction (string.*n table.*) which may exist (or not in a different form).

  • z8lua / github - a fork of Lua that implements the PICO-8 dialect.
    • to install: git clone + make => z8lua binary
    • does not support require? as such difficult to use for unit-testing - though It should be possible to patch it to support require 🚧
  • Going from Lua 5.2 to PICO-8’s Lua

Lua Langage

Setup

$ sudo apt install lua5.2   # this is the closest to PICO-8 v0.2.5g

Table

Table can be treated as an array (list-like) or as a key-value dictionary.

-- Array
t = {1, 2, 3}

-- of dictionnary: Simple object-like table
Person = {
    name = "Alice",
    age = 25
}

Appending to a table

-- Array
table.insert(t, 4)  -- use add() in pico8

-- or assign to next index
t[#t + 1] = 5

for i, v in ipairs(t) do
    print(i, v)
end
-- for dictionnary just assign the key
Person["c"] = 3
Person.d = 4  -- shorthand for t["d"]

for k, v in pairs(Person) do
    print(k, v)
end

Removing Element

tables don’t have a fixed “remove” operator, but PICO-8 provides two handy functions:

Remove by valuedel(tbl, value) This removes the first occurrence of the given value.

t = {10, 20, 30, 20}

del(t, 20)   -- removes the first 20
-- t = {10, 30, 20}

Remove by indexdeli(tbl, index) This removes the element at a specific position.

t = {10, 20, 30, 40}

deli(t, 2)  -- removes element at index 2
-- t = {10, 30, 40}

Notes Both del() and deli() automatically shift later elements down so there are no gaps.

If the value isn’t found (for del) or the index is out of range (for deli), nothing happens.

Enum

Lua doesn’t have built-in enums like C, C#, or Java.

The closest to a C-style enum is simple table constants:

local Colors = {
    RED = 1,
    GREEN = 2,
    BLUE = 3
}

print(Colors.RED)   -- 1

static

Do not exist, see discussion

Currying

Object / Struct

see Meta-table

Operators Tutorial

Unit Testing

busted

The most widely used Lua test framework.

Syntax could be lighten with Moonscript. 🚧

Setup

$ sudo apt-get install luarocks
$ sudo apt install liblua5.2-dev  # depending on lua -v
$ sudo luarocks install busted
$ busted --version # validate

Example

describe("math", function()
  it("adds numbers correctly", function()
    assert.is_equal(4, 2 + 2)
  end)
end)

Then run with

$ busted . # By default, it looks for *_spec.lua files in your project.

VSCode 🚧

see Busted Test Explorer by Vivien Henriet

Busted error: spawn /usr/local/bin/busted ENOENT
Busted exited with code -2

see also

Written on October 31, 2023, Last update on April 10, 2025
lua lang pico8 vscode