How does this work

That is just a common way to write OOP-style code in Lua.

The function stored inside VoxelModel are the methods for the objects. Because the VoxelModel table is a metatable for them, the created objects will automatically inherit those methods.

In the example below, the entire class t is also the metatable for any objects of t class. This means those objects will inherit the foo() function and it becomes accessible from the objects themselves, allowing you to call foo by doing object.foo(object) or object:foo()

local t = {}
t.__index = t --t is a metatable and also the class table

t.foo = function(self) --this is a method for the object
    print(self.value)
end

local object = setmetatable({}, t) --create a new object as {}, and then use the class table as the metatable
object.value = 123
object:foo() --> this will print 123

--you can verify that foo() is indeed inherited by printing the function
print(object.foo)
--and then compare the memory address with the one inside the metatable:
print(t.foo)
--they should be the same!

You can learn more specifically about that in many other threads:


And then here’s a guide on the type annotations which the module also has: