What is __index how do I use it, and what happens when you set it to a table?
First, we need to understand what Metables are before we get into the functions:
In Lua, metatables are tables that allow tables to manipulate another table. This means they make a table more powerful or have a greater advantage over something. Metatables allow us to accumulate data and change the behaviour of such property to be added. We can alter keys with a value.
__Index is a function in metatables, which is a hierarchy of Metatables that fires when a table should be indexed. __Index can also return a table.
Metatables are useful if you want to hide a table or return something that is not a descendant of the workspace. (Meaning it doesn't exist) you can easily use a metatable.
Here's a simple guide on metatables
Metatables usually introduce OOP. That's another topic yet to be introduced!
February 19, 2021