lua

A programming language that is used in several game engines, like PICO-8 and LÖVE2D.

Warning

  • Tables indices start at 1
  • Only false and nil have boolean value false. Everything else is true (even "0" or an empty string)
  • Different is "~="

Useful

-- Inserting an element at the end of a table
table.insert(table, element)

-- Inserting an element at the beginning of a table
table.insert(table, 1, element)

OOP

-- Defining an Entity class

Entity={}
Entity.__index=Entity

-- Constructor
function Entity:new(x, y)
    local p={}
    setmetatable(p, self)
    p.x = x or 0
    p.y = y or 0
    p.vx = 0
    p.vy = 0
    return p
end

-- Methods
function Entity:update()
    -- updating thing
end

function Entity:draw()
    -- drawing the entity
end

-- Instancing

entity_A = Entity:new()

-- Inheritance

-- Player
PLAYER = Entity:new()
PLAYER.__index=Entity

-- We define our own update method
function PLAYER:update()
    -- add player specific things
    -- call parent method
    Entity.update(self)
end

Links