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?| Parameter | Type | Description |
|---|---|---|
object | any | The object to get the metatable of |
Example
lua
local mt = getrawmetatable(game)
print(mt) --> tablesetrawmetatable
Sets the metatable of an object, bypassing the __metatable metamethod.
lua
setrawmetatable(object: any, metatable: table?) -> void| Parameter | Type | Description |
|---|---|---|
object | any | The object to set the metatable on |
metatable | table? | 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| Parameter | Type | Description |
|---|---|---|
table | table | The table to modify |
readonly | boolean | true 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) -> booleanExample
lua
local mt = getrawmetatable(game)
print(isreadonly(mt)) --> truemakereadonly
Makes a table read-only. Equivalent to setreadonly(table, true).
Aliases: make_readonly
lua
makereadonly(table: table) -> voidExample
lua
local t = { key = "value" }
makereadonly(t)
print(isreadonly(t)) --> truemakewriteable
Makes a table writable. Equivalent to setreadonly(table, false).
Aliases: make_writeable
lua
makewriteable(table: table) -> voidExample
lua
local mt = getrawmetatable(game)
makewriteable(mt)
-- mt is now writable
makereadonly(mt)