getmetatable

From Garry's Mod
Jump to: navigation, search
 getmetatable( )

Contents

Description

Returns the metatable of an object. This function obeys the metatable's __metatable field, and will return that field if the metatable has it set.

Use debug.getmetatable if you want the true metatable of the object.

Arguments

any object

The value to return the metatable of.

Returns

any

The metatable of the value. This is not always a table.

Examples

Example

Use a table's metatable and alter it.

print(getmetatable(Pupil).__index.GetName(Pupil))
-- getmetatable(Pupil) will return Pupil_meta.
-- Same as print(Pupil:GetName())
-- This is what the Lua interpreter basically does. (When __index is a table.)

getmetatable(Pupil).SetName = function(self, newName)
 self.name = newName
end
-- We're adding a new method to Pupil's metatable

print(getmetatable(Pupil).GetName(Pupil))
-- Still the same, because Pupil_meta.__index is Pupil_meta.

Output:

"John Doe"
Personal tools
Navigation