Metatables And OOP: Simplified


Introduction to Metatables in Roblox Lua

Metatables are a powerful feature in Lua that allow you to modify the behavior of tables using metamethods. Want to make a table behave like a string? No problem! Want to make two tables add together like numbers? You can do that too! Need to use a table like a function? It’s possible with metatables!

But do you really need to learn metatables? While metatables aren’t necessary for every project, they can greatly improve efficiency and offer flexibility when implementing Object-Oriented Programming (OOP) in Lua. However, instead of using metatables directly for OOP, consider using something better, simpler, and more powerful like Class++, which handles metatables internally but offers a cleaner interface for OOP.


Why Use Metatables?

Here are some common use cases for metatables:

  • Retrieving values for keys that don’t exist in a table
  • Customizing behavior when assigning values to keys
  • Making a table callable like a function
  • Overloading operators like addition (+), subtraction (-), and comparisons (<, >)
  • Converting a table to a string with custom formatting
  • Accessing and modifying the metatable of an existing table
  • Finalizing or cleaning up tables before they are garbage collected
  • Measuring the length of tables
  • Using tables as iterators for custom loops

For OOP, it’s best to avoid using metatables directly. Instead, try Class++, which simplifies object creation and management while still using metatables under the hood.


Should You Always Use Metatables?

Although metatables are a powerful tool in Lua, they are not essential for all tasks. Lua provides many simpler mechanisms to achieve similar outcomes. Some developers even argue that metatables should be used sparingly due to their complexity. If you’re curious about the downsides of metatables, check out this interesting article by elttob: Metatables Suck.

However, there are certain cases where metatables are indispensable, especially when you need custom behavior for tables. Let’s go over a basic tutorial to help you understand how they work.


Metatables: A Quick Tutorial

To get started with metatables, you’ll need two tables: a regular table and a metatable.

local myMetatable = {}
local myTable = {}

You can link the two using setmetatable:

setmetatable(myTable, myMetatable)
-- or directly with new tables
setmetatable({}, {})

To retrieve the metatable of a table, use getmetatable:

getmetatable(myMetatable)

Now that you can set and get a metatable, let’s explore metamethods. Here’s a basic example using the __index metamethod, which allows fallback behavior when accessing nonexistent keys in a table:

local metatable = {
    __index = { sound = "meow" }
}
local normaltable = {}

setmetatable(normaltable, metatable)

-- Accessing a key in the normal table that doesn't exist will fall back to the metatable
print(normaltable.sound) -- Output: "meow"

In this example, the __index metamethod specifies what happens when you try to access a key that doesn’t exist in the regular table.

There are many other metamethods available to customize behavior, such as __add, __call, __tostring, and more. To dive deeper into these metamethods, check out this comprehensive tutorial: Full Beginner Guide to Metatables.


Metatables may seem complicated at first, but they become quite intuitive with practice. Trust me, they’re easier than they seem!


For Object-Oriented Programming

While metatables can be used to implement Object-Oriented Programming (OOP), they often add unnecessary complexity. In Lua, tables act as objects, and you can use metatables to achieve inheritance and encapsulation. But, for a simpler and more efficient approach, use Class++ for OOP.

Tables Are Objects

In Lua, tables are versatile—they can function as arrays, dictionaries, lists, and objects. Since tables are so flexible, they form the basis of OOP in Lua. By attaching metatables to tables, you can mimic OOP features like inheritance and method overriding.

Implementing OOP in Lua

There are a few ways to implement OOP in Lua:

  • Metatables-based OOP: Directly use metatables to manage class behavior, which can be complex and tedious to maintain.
  • Functional OOP: Implement classes using functions, avoiding metatables altogether.

Both methods have their pros and cons, but for most practical purposes, using Class++ is a more user-friendly solution.

Metatables-based OOP Tutorial

If you’re interested in learning how to use metatables for OOP, here’s a brief tutorial:

local Class = {}
Class.__index = Class

function Class:new(value)
    local instance = setmetatable({}, Class)
    instance.value = value
    return instance
end

function Class:printValue()
    print(self.value)
end

local obj = Class:new(10)
obj:printValue() -- Output: 10

This is very common in complex scripts.


local Class = {}
Class.__index = Class

These specific lines are confusing to most people, what it does it that it sets the index to the metatable that’s because when we call a function from an object, the function doesn’t actually exist in its table, but in the metatable we created, therefore metatable here is actually the list of functions we can apply to an object and without that, we have to repeatedly make same functions for every single object(table) we create.

Functional OOP Tutorial

For functional OOP, you get rid of metatables and define classes and methods directly within a function:

local class = {}

local function class.createClass(name)
  class[name] = {}
end

local function class.dosmth(name)
--gftvybfyfy
end

class.createClass("myclass")
class.dosmth("myclass")

This method is simpler, but not used in complex scripts somehow.

You can also use class++ which is very powerful.
You should probabamy see the official Lua documentation on OOP as it explains better than most tutorials do. And also see Sleitnick’s video as he explains these methods very well.


In conclusion, metatables are a powerful feature in Lua, allowing you to customize table behavior in interesting ways.

7 Likes

̶E̶x̶p̶l̶a̶n̶a̶t̶i̶o̶n̶ ̶w̶i̶l̶l̶ ̶b̶e̶ ̶a̶d̶d̶e̶d̶ ̶t̶o̶m̶o̶r̶r̶o̶w̶ ̶a̶s̶ ̶i̶m̶ ̶s̶l̶e̶e̶p̶y̶ ̶r̶n̶ (added)

Does the metatable is setmetatable has to be the second variable?

like setmetatable(table,metatable) or can it be first?

It HAS to be second, making it first will make it a normal table

3 Likes

Just for little feedback to see if you guys want me to keep this updating.

Should I keep updating this or just… demolish?
  • UPDATE!!
  • DEMOLISHHH!!

0 voters

If you picked update, how was this post already? Rate
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

0 voters

Could you tell me what is a Class++ im curious as to what it is?
For me best OOP is just storing a pointer for a method becouse of dirrect indexing of what i need and fastest perfomance.

should be replaced with prototype based OOP

I gave the link, you can check it out here.


I think that’s all you need to understand the concept of OOP in luau.

I really hope some one understands this unlike me >_<

I just checked and that a complete bloatware.
Never do that.

Maybe… Yea… Its just simpler…?

How is it simplier?
Table extension everytime for what?
Bloatwaring the code.
Useless things aka "I do t trust my own code"™️

I dont understand what he said neither lol

Wdym “bloatware”? Can you properly criticize or are you just dissatisfied with its features? It’s pretty good to me.

“Toying” with code aka bloatware
Is when you spend memory doing absolutely nothing!
If you can’t code otherwise then too bad!Maybe learn low level/mid level languages without garbage collectors like C/C++.
Gamedev is first of all optimization of code so any potato will be able to run it!
If storing a pointer to a function is not enough for you then go learn JS and make fancy websites i dunno.
How came we having to protect own code from ourselves? :joy::skull:
Me personally only recognize 2 (technically 3) valid OOP methods in Luau:

  1. Pointer OOP/Dirrect function call OOP
  2. Metatable OOP
    Everything else is just a bloatware and completely misunderstanding of Luau