Wiki

Clone wiki

Core / CodeaUserdefinedTypes

Back to Beyond the Codea in-app reference


FunctionIconType-Small.png Codea's user-defined types

Lua has eight basic types: nil, boolean, number, string, function, userdata, thread, and table. Codea extends Lua with the following user-defined types:

Key for metatable in Lua's registeryType constructor(s)Use
bodyphysics.body(shapeType, ...)Represents a dynamic rigid body.
codeaimageimage(width, height); image(data)Represents a raster image.
codeasoundbuffersoundbuffer(data, format, freq)Represents a sound buffer containing arbitrary audio data.
colorcolor(r, g, b, a)Represents a colour.
contactNoneRepresents a collision between two bodies. Such a value is passed to the global function collide(physics.contact).
jointphysics.joint(type, ...)Represents a joint that constrains two rigid bodies together.
matrixmatrix(a11, a21, a31, ..., a24, a34, a44)Represents a 4 x 4 matrix.
meshmesh()Represents a triangle mesh.
shadershader()Represents a shader.
touchNoneRepresents a touch. Global variable CurrentTouch refers to such a value.
vec2vec2(x, y)Represents a two-dimensional vector.
vec3vec3(x, y, z)Represents a three-dimensional vector.
vec4vec4(x, y, z, w)Represents a four-dimensional vector.

Extending Codea's user-defined types

With some exceptions in previous versions of Codea, you can extend Codea's user-defined types by adding keys and values to the metatable of the userdata. For example:

do
    local mt = getmetatable(vec4())
    mt["unpack"] = function (self) return self.x, self.y, self.z, self.w end
end

v1 = vec4(1, 2, 3, 4)
print(v1:unpack()) -- generates output: 1   2   3   4

By default, indexing the userdata returns the value of the corresponding key in the userdata's metatable.

Exceptions prior to Codea version 1.4.5 are the userdata returned by the color() function or referred to by the CurrentTouch global variable. In those cases, by default, indexing the userdata returns nil. For example:

do
    local mt = getmetatable(color())
    mt["unpack"] = function (self) return self.r, self.g, self.b, self.a end
end

c1 = color(255, 255, 0, 255)
print(type(c1["unpack"]) -- generates output: nil
print(c1:unpack()) -- generates an error

This can be overcome by enhancing the function referenced by the __index key in the userdata's metatable. For example:

-- Enhance the function referenced by __index
do
    local mt = getmetatable(color())
    local oldLget = mt["__index"]    -- preserve old Lget
    mt["__index"] = function (t, k)
        local v = oldLget(t, k)      -- try the old Lget
        if v then return v end       -- if it works, then return the result
        return mt[k]                 -- otherwise, look in the metatable
    end
end

Note this isn't required from Codea version 1.4.5.

Once __index is enhanced, other extensions should then work. For example:

do
    local mt = getmetatable(color())
    mt["unpack"] = function(self) return self.r, self.g, self.b, self.a end
end

c1 = color(255, 255, 0, 255)
print(c1:unpack())            -- generates output: 255   255   0   255

Updated