Skip to content
Jérôme Leclercq edited this page Dec 8, 2020 · 1 revision

API: RegisterMetatable

Description:

Create and registers a new metatable with a name in Lua registry and returns it.

Prototype:

RegisterMetatable(name: string) -> mt: table

Parameters:

  1. name: The metatable identifier/name.

Returns:

  1. mt: The new metatable.

Remarks:

  1. An error is triggered if a metatable with that name already exists.

Example code

A code registering a Circle class.

local circlemeta = RegisterMetatable("circle")
circlemeta.__index = circlemeta

function circlemeta:__tostring()
	return "circle(" .. tostring(self.origin) .. "; " .. tostring(self.radius) .. ")"
end

function Circle(center, radius)
	AssertMetatable(center, "vec2")
	assert(type(radius) == "number", "vec2")

	local circle = {
		origin = center,
		radius = radius
	}
	return setmetatable(circle, circlemeta)
end

Clone this wiki locally