ECS for Lua
Go to file
Calvin Rose 821914795d
Merge pull request #25 from yangruihan/master
fix README.md example error
2020-11-03 07:18:04 -06:00
rockspecs Add new rockspec. 2016-08-10 19:45:58 -04:00
spec Fix #22 - manage systems before setSystemIndex 2020-08-12 08:28:57 -05:00
.gitignore Ignore .rock. 2015-04-26 11:55:07 +08:00
.travis.yml Update .travis.yml 2019-10-13 00:14:12 -05:00
config.ld typo fix 2016-04-27 13:55:10 +02:00
init.lua Fix init.lua 2019-07-23 00:40:11 -03:00
LICENSE.txt Update Copyright Date. 2016-02-29 19:19:34 -05:00
README.md fix README.md example error 2020-11-03 19:35:44 +08:00
test.lua Fix #22 - manage systems before setSystemIndex 2020-08-12 08:28:57 -05:00
tiny-ecs-scm-0.rockspec Fix scm rockspec. 2015-06-07 20:05:56 -04:00
tiny.lua Fix #22 - manage systems before setSystemIndex 2020-08-12 08:28:57 -05:00

tiny-ecs

NOTE

Although there have been almost no commits in several years, this project is not abandoned. tiny-ecs is, for most intents and purposes, finished, and no bugs have been brought to my attention in a while. New issues on GitHub will be addressed.

Build StatusLicense

Tiny-ecs is an Entity Component System for Lua that's simple, flexible, and useful. Because of Lua's tabular nature, Entity Component Systems are a natural choice for simulating large and complex systems. For more explanation on Entity Component Systems, here is some basic info.

Tiny-ecs also works well with object-oriented programming in Lua because Systems and Entities do not use metatables. This means you can subclass your Systems and Entities and use existing Lua class frameworks with tiny-ecs, no problem. For an example on how to use tiny-ecs with object-oriented Lua, take a look at the demo branch, specifically the systems and entities subdirectories.

Overview

Tiny-ecs has four important types: Worlds, Filters, Systems, and Entities. Entities, however, can be any Lua table, and Filters are just functions that take an Entity as a parameter.

Entities

Entities are simply Lua tables of data that gets processed by Systems. Entities should contain primarily data rather than code, as it is the System's job to do logic on data. Henceforth, a key-value pair in an Entity will be referred to as a Component.

Worlds

Worlds are the outermost containers in tiny-ecs that contain both Systems and Entities. In typical use, only one World is used at a time.

Systems

Systems in tiny-ecs describe how to update Entities. Systems select certain Entities using a Filter, and then only update those select Entities. Some Systems don't update Entities, and instead just act as function callbacks every update. Tiny-ecs provides functions for creating Systems easily, as well as creating Systems that can be used in an object oriented fashion.

Filters

Filters are used to select Entities. Filters can be any Lua function, but tiny-ecs provides some functions for generating common ones, like selecting only Entities that have all required components.

Example

local tiny = require("tiny")

local talkingSystem = tiny.processingSystem()
talkingSystem.filter = tiny.requireAll("name", "mass", "phrase")
function talkingSystem:process(e, dt)
    e.mass = e.mass + dt * 3
    print(("%s who weighs %d pounds, says %q."):format(e.name, e.mass, e.phrase))
end

local joe = {
    name = "Joe",
    phrase = "I'm a plumber.",
    mass = 150,
    hairColor = "brown"
}

local world = tiny.world(talkingSystem, joe)

for i = 1, 20 do
    world:update(1)
end

Use It

Copy paste tiny.lua into your source folder. For stability and consistent API, please use a tagged release or use LuaRocks.

Luarocks

Tiny-ecs is also on LuaRocks and can be installed with luarocks install tiny-ecs.

Demo

Check out the demo, a game originally written for Ludum Dare 32 with the theme 'An Unconventional Weapon'. The demo uses LÖVE, an amazing game framework for Lua.

Testing

Tiny-ecs uses busted for testing. Install and run busted from the command line to test.

Documentation

See API here. For the most up-to-date documentation, read the source code, or generate the HTML locally with LDoc. See the original forum thread here.