Garry's Mod Wiki



  any getmetatable( any object )


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.


1 any object
The value to return the metatable of.


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


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) = 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"


Using a Metatable to make functions easier

local ply = getmetatable("player") function ply:PrintData() local tbl = { self:Nick(), -- Using self is the same as having an argument in the function self:SteamID() -- but instead self is referring to the player the functions being ran on } return tbl end

Special Pages



Render Time: 24ms

DB GetPage 2
Generate Html 4
SaveChanges (1) 8
Render Body 0
Render Sidebar 7