[Tutorial] Metatables & Metamethods |
Bolodefchoco « Sénateur » 1517429100000
| 3 | |||||||||||||||||||||||
Metatables & Metamethods In Lua, there are diferent types of metamethods. They peform some features in a table, and this same table that contains a metamethod is called a metatable. "This metatable is a Lua table that defines the behavior of the original value under certain special operations. You can change a number of aspects of an operation behaviour on a value by specifying fields in the metatable.", (Lua manual) Each metamethod is preceded by two underscores ( _ ) and the name of the event, as in __event. Functions Summary: » getmetatable(table)
__index(list,index)
|
Mescouleur « Citoyen » 1517430060000
| 2 | ||
looks amazing good job |
King_seniru « Censeur » 1517450940000
| 1 | ||
TY Bolo! |
1 | ||
But why we use them. It would be better if i ask that if i didnt use metatables do it throw any error. If yes give an example pls. TY And Nice thread! |
Bolodefchoco « Sénateur » 1517497380000
| 1 | ||
Mouseclick1 a dit : Well. Try this code (sum a table to 100) Code Lua 1 2 3 4 a = {10, 20, 30, 40, 70} Now, let's use a metatable in " a " that returns the first value of the table when we try to sum it to something Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 a = {10, 20, 30, 40, 70} |
King_seniru « Censeur » 1517539020000
| 1 | ||
So why we use values form tables without adding them by integers |
Bolodefchoco « Sénateur » 1517540160000
| 1 | ||
Indika123 a dit : Sorry, what do you mean? |
Censere « Consul » 1517617140000
| 2 | ||
Nice, sort of looks like the "magic methods" of a Python class. Dernière modification le 1517617200000 |
Bolodefchoco « Sénateur » 1517619600000
| 1 | ||
Drescen a dit : Kinda xD It's metaprogramming |