Skip to content
= edited this page Jan 19, 2013 · 2 revisions

#Systems

Systems are the actual brains of your game. They act on entities with certain components and modify the data on those components.

##How to use

To create a system, place a lua file into a folder named systems. This files name is the name of your system. This lua file returns a table with two methods defined on it, update and init.

lua_system:init()

The init function is responsible for telling the manager what entities the system will work on. This is done by returning a table of strings denoting component name.

lua_system:update((number)dt, (table)entity)

The manager calls this function once per entity that meets the conditions defined in init. In this function, one reads and modifies the variables in the components specified in init. Note that only the components specified in init will be available to be modified (this is subject to change).

Example System

--create empty system table
system = {}

--define the init method
function system:init()
  return { "Transform", "Input", "custom_mover" }
end

--define the update method
function system:update(dt, entity)
  --tables are always pass by reference, so:
  local position = entity.Transform.pos
  local keys = entity.Input.keys

  --convert W and S keys into a direction
  local x_direction = 0
  if keys[key.W] then
    x_direction = 1
  end
  if keys[key.S] then
    x_direction = x_direction - 1
  end

  --set the x value according to the direction and the speed defined on custom_mover
  position.x = position.x + custom_mover.speed * dt * x_direction
end

--return back the system
return system

Clone this wiki locally