Skip to content

Metatable

Functions for manipulating metatables and controlling table read-only status. These bypass the __metatable metamethod.

getrawmetatable

Returns the metatable of an object, bypassing the __metatable metamethod.

lua
getrawmetatable(object: any) -> table?
ParameterTypeDescription
objectanyThe object to get the metatable of

Example

lua
local mt = getrawmetatable(game)
print(mt) --> table

setrawmetatable

Sets the metatable of an object, bypassing the __metatable metamethod.

lua
setrawmetatable(object: any, metatable: table?) -> void
ParameterTypeDescription
objectanyThe object to set the metatable on
metatabletable?The new metatable, or nil to remove

Example

lua
local t = {}
setrawmetatable(t, {
    __index = function(_, key)
        return key .. " not found"
    end
})
print(t.hello) --> "hello not found"

setreadonly

Sets or clears the read-only flag on a table.

lua
setreadonly(table: table, readonly: boolean) -> void
ParameterTypeDescription
tabletableThe table to modify
readonlybooleantrue to make read-only, false to make writable

Example

lua
local mt = getrawmetatable(game)
setreadonly(mt, false)
-- Now you can modify mt
mt.__namecall = newcclosure(function(...)
    return mt.__namecall(...)
end)
setreadonly(mt, true)

isreadonly

Returns true if the table is read-only.

lua
isreadonly(table: table) -> boolean

Example

lua
local mt = getrawmetatable(game)
print(isreadonly(mt)) --> true

makereadonly

Makes a table read-only. Equivalent to setreadonly(table, true).

Aliases: make_readonly

lua
makereadonly(table: table) -> void

Example

lua
local t = { key = "value" }
makereadonly(t)
print(isreadonly(t)) --> true

makewriteable

Makes a table writable. Equivalent to setreadonly(table, false).

Aliases: make_writeable

lua
makewriteable(table: table) -> void

Example

lua
local mt = getrawmetatable(game)
makewriteable(mt)
-- mt is now writable
makereadonly(mt)