Lua Academy · Lesson

Data-Driven Config and Hot-Reload

Load game data from Lua files and reload scripts at runtime.

Lesson 4 of 413 steps

Data-Driven Config and Hot-Reload is a free Lua Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, and your progress syncs across the web and the CoddyKit app. The Lua Academy course includes 4 lessons in total.

What Is Data-Driven Design?

Data-driven design separates what (data: stats, levels, layouts) from how (code: game logic). Data changes do not require recompilation.

Loading Config from a Lua File

A config file is a Lua script that returns a table. Load it with dofile or loadfile + pcall.

-- config.lua
return {
  playerSpeed = 200,
  gravity     = 980,
  maxEnemies  = 10,
}

Reading the Config

In the host script, load and use the config table.

local config = dofile("config.lua")
print(config.playerSpeed)  -- 200

Sandboxed Config Loading

For security, load config in a restricted environment so malicious code cannot call os.execute or other dangerous functions.

local function loadConfig(filename)
  local chunk, err = loadfile(filename)
  if not chunk then error(err) end
  local env = {math = math, table = table}
  setfenv and setfenv(chunk, env) or _ENV  -- 5.1 vs 5.2+
  return chunk()
end

Hot-Reload Overview

Hot-reload allows changing data (or code) while the game is running. This dramatically speeds up iteration: tweak a value, see the result immediately without restarting.

Detecting File Changes

Use os.difftime(os.time(), lastModified) or poll a file's modification time with a C extension or I/O trick to detect when a config file has changed.

Reloading the Config

On change, reload the file and update the live config table in-place so all references to the table see the new values.

local function hotReload(config, filename)
  local ok, newCfg = pcall(dofile, filename)
  if ok and type(newCfg) == "table" then
    for k, v in pairs(newCfg) do config[k] = v end
    print("Config reloaded")
  else
    print("Reload error:", newCfg)
  end
end

Reloading Lua Modules

Remove a module from package.loaded before calling require again to force a fresh load.

package.loaded["mymodule"] = nil
local mymodule = require("mymodule")

Level Data from Lua

Define levels as Lua tables: room layouts, enemy placements, trigger zones. Load at runtime with dofile for fast iteration.

-- level1.lua
return {
  name = "Forest",
  enemies = {
    {type="goblin", x=100, y=200},
    {type="troll",  x=300, y=150},
  },
  music = "forest_theme.ogg",
}

Validation

After loading config, validate required fields and types. Provide clear error messages for missing or malformed data.

Version Tags

Include a version field in config files. Reject files with incompatible versions to prevent silent errors from stale data.

Hot-Reload Question

How do you force Lua to reload a cached module?

Recap: Data-Driven Config and Hot-Reload

Load game data from Lua files with dofile/loadfile. Use sandboxed environments for untrusted configs. Implement hot-reload by polling file modification times and clearing package.loaded for module reloads.

Free to start

Learn Lua with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “Data-Driven Config and Hot-Reload” lesson free?

Yes — any 3 lessons of the Lua Academy learning path, including “Data-Driven Config and Hot-Reload”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Lua Academy course includes 4 lessons in total.

What will I learn in “Data-Driven Config and Hot-Reload”?

Load game data from Lua files and reload scripts at runtime. You practise Lua Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Lua Academy?

No prior experience is required. Lua Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

How long does the “Data-Driven Config and Hot-Reload” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Lua Academy lesson?

Yes. Every Lua Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Event Systems in Lua
  2. Component-Based Scripting
  3. AI Behavior with State Machines
  4. Data-Driven Config and Hot-Reload
← Back to Lua Academy