Component-Based Scripting
Design game entities as tables with behavior components in Lua.
Component-Based Scripting is a free Lua Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Lua Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is Component-Based Design?
Instead of deep class hierarchies, entities are composed of components — small, focused tables of data and behavior. An entity is just a collection of components.
Entity as a Table
An entity is a table with an id and a components sub-table. Components are added and removed at runtime.
local function newEntity(id)
return {id = id, components = {}}
end
local player = newEntity(1)Adding Components
Attach a component by setting a named key in the components table.
player.components.position = {x=0, y=0}
player.components.health = {hp=100, max=100}
player.components.sprite = {image="player.png", scale=1}Systems Process Components
A system processes all entities that have a required set of components. Systems iterate entities and update component data.
local function moveSystem(entities, dt)
for _, e in ipairs(entities) do
local pos = e.components.position
local vel = e.components.velocity
if pos and vel then
pos.x = pos.x + vel.dx * dt
pos.y = pos.y + vel.dy * dt
end
end
endComponent Scripts
Attach Lua functions as component behavior — a simple form of scripting per entity.
player.components.ai = {
update = function(self, entity, dt)
-- move towards target
end
}Component Registry
A registry maps component type names to factory functions, enabling components to be created by name (e.g., from a level file).
local registry = {}
function registry.register(name, factory)
registry[name] = factory
end
function registry.create(name, ...)
return registry[name](...)
endRemoving Components
Set the component to nil to remove it. The entity will then be ignored by systems that require it.
player.components.health = nil -- entity is "dead"World and Entity Manager
A World object stores all entities and systems, iterates systems each tick, and provides entity query APIs.
Querying Entities by Component
Provide a query function that returns all entities having all specified components.
local function query(world, ...)
local required = {...}
local result = {}
for _, e in ipairs(world.entities) do
local ok = true
for _, comp in ipairs(required) do
if not e.components[comp] then ok = false; break end
end
if ok then result[#result+1] = e end
end
return result
endPrefabs
A prefab is a template table of components. Instantiate new entities from prefabs by deep-copying the template.
Benefits over Inheritance
- No deep hierarchies.
- Behavior mixed at runtime.
- Easy to add/remove features.
- Data-driven from config files.
Component Design Question
In component-based design, what does a System do?
Recap: Component-Based Scripting
Entities are tables of components. Systems process entities with required components. This decoupled, data-driven architecture is more flexible than deep inheritance hierarchies and works naturally in Lua.
Frequently asked questions
Is the “Component-Based Scripting” lesson free?
Yes — the full text of “Component-Based Scripting” is free to read here on the web, and the Lua Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Lua Academy course, upgrade to CoddyKit PRO.
What will I learn in “Component-Based Scripting”?
Design game entities as tables with behavior components in Lua. 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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Component-Based Scripting” 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
- Event Systems in Lua
- Component-Based Scripting
- AI Behavior with State Machines
- Data-Driven Config and Hot-Reload