Syntax
getmetafield(target: any, field: string): any | nilArguments
| Name | Type | Description |
|---|---|---|
target | any | Value whose metatable is inspected. |
field | string | Metafield name to read. |
Returns
| Name | Type | Description |
|---|---|---|
fieldValue | any | nil | Raw metafield value when present; otherwise nil. |
Description
Returns a raw metafield value from the target metatable. target may be any value, and field must be a string. The function does not call the metafield, does not invoke __index lookup, and returns nil when the metatable or field is missing.
Call it with 2 parameter(s): target, field. The argument table explains which values are required and which ones only refine the behavior.
It returns fieldValue (any | nil). Use the returns table to separate successful values from nil results and recoverable errors.
Example
Read one raw metafield value from a value's metatable.
local target = {}
local meta = {
__index = function()
return "fallback"
end,
}
setrawmetatable(target, meta)
local field = getmetafield(target, "__index")
print(type(field))
print(getmetafield(target, "__newindex"))