G.setmetatable

From GMod Wiki

Revision as of 18:32, 10 September 2011 by Smokey (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Function
Syntax setmetatable( Table Table Name [, Table Meta Table] )
Where is this used?
Description:
Sets a meta table. If Meta Table is Nil , removes the metatable of the given table.
Returns: Table
Part of Library: Global Functions
Realm: NewerShared.png
BBCode Link: [b][url=http://wiki.garrysmod.com/?title=G.setmetatable]G.setmetatable [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]



Example

DescriptionCreates a metatable that overloads the add operation on a table to use a custom field.
Used onNewerShared.png
Code
mt = { __add = function( a, b ) return  { num = a.num + b.num } end }
x = { num = 3 }
y = { num = 5 }
 
setmetatable( x, mt )
setmetatable( y, mt )
 
z = x + y
 
print( z.num )
Output8
Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox