Skip to content

Example script

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- 3D vector implementation.

Vector = {}
Vector.__tostring = function(self)
    return '[' .. tostring(self.x) .. ', ' .. tostring(self.y).. ', ' .. tostring(self.z) .. ']'
end

Vector_mt = {}
Vector_mt.__call = function(self, _x, _y, _z)
    local vec_t = {}
    setmetatable(vec_t, Vector)

    vec_t.x = _x
    vec_t.y = _y
    vec_t.z = _z

    return vec_t
end

setmetatable(Vector, Vector_mt)

-------------------------------------------------------

local my_vec = Vector(5, 12, 57)
print(tostring(my_vec)) -- [5, 12, 57]