Originally Posted by Layback_
After watching some tutorials about metatable, I got the generic concept of it in Lua. However, I am still struggling to understand how and why metatable is used in your example.
|
The code I posted isn't really a good reference about metatables and how powerful they can be as it's highly optimized for its specific application.
Metatables offer a way to control how tables respond to different operations. To implement a fallback table to another, I assigned it to the index operation by setting it to
__index in its metatable. This operation is referred to when Lua looks for an entry in a table and doesn't find anything. If another table is assigned to
__index in the metatable, it looks through that in addition to the original table.
If you're interested in the official documentation, here's the link.
Lua 5.1 Reference Manual Section 2.8