Scripting and Configuration Languages

46.1 Lua as a Configuration Format

-- game_config.lua (data + logic: more powerful than JSON/INI)
window = { width = 1280, height = 720, vsync = true }
difficulty = os.getenv("HARD") and "hard" or "normal"
levels = {}
for i = 1, 10 do levels[i] = { enemies = i * 5, time_limit = 120 - i * 5 } end

46.2 Building a Domain-Specific Language (DSL)

-- Declarative DSL using table-call syntax
local entities = {}
local function entity(name)
    return function(props)
        props.name = name
        entities[#entities + 1] = props
    end
end

-- dsl_level.lua (loaded with a restricted env that exposes `entity`)
entity "player" { x = 0, y = 0, hp = 100 }
entity "orc"    { x = 50, y = 20, hp = 30, loot = { "gold", "axe" } }

for _, e in ipairs(entities) do print(e.name, e.x, e.y, e.hp) end

46.3 Hot Reloading

-- Reload a module at runtime (games, servers)
local function reload(name)
    package.loaded[name] = nil
    local ok, mod = pcall(require, name)
    if not ok then print("reload failed: " .. mod); return nil end
    return mod
end
-- Libraries: lurker + lume.hotswap (LÖVE), hot reload in Defold, Neovim's :luafile / :source