🎓 A1Learner
🔍
0 of 49 lessons completed

Lua Host Configuration

Host configuration is settings data that a host loads into its own table.

Settings land in a host table

The config chunk assigns a title. The host reads it from its own table.

local settings = {}
local config = load('title = "Garden"', "config", "t", settings)
config()
print(settings.title)

Several settings land together

The config chunk assigns two settings. The host prints both on one line.

local settings = {}
local config = load([[
title = "Harbour"
limit = 3
]], "config", "t", settings)
config()
print(settings.title, settings.limit)

Wild code cannot run

The config chunk tries to print. Print is missing from its environment, so the run fails.

local settings = {}
local config = load('print("wild")', "config", "t", settings)
local ok = pcall(config)
print(ok)
print(settings.title)

Example

local settings = {}
local config = load([[
title = "Harbour"
limit = 3
dark = true
]], "config", "t", settings)
config()
print(settings.title, settings.limit, settings.dark)

Key points

  • Model: the host side here is modelled in Lua and no C code runs in this lesson.
  • Configuration: configuration is data that shapes how a program behaves.
  • Capturing environment: a capturing environment is a plain table the host passes to a config chunk.
  • Landing: assignments in the config chunk land as fields in that table.
  • Wild code: code that needs a missing name fails instead of running wild.
  • Host read: the host reads the settings from its own table after the chunk runs.

In this example, the config chunk assigns three settings. Each assignment lands as a field in the host table. The host then prints all three fields on one line.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

A config chunk sets a title. Where does the host read it?