jaetennessee.blogg.se

Lua table insert
Lua table insert













lua table insert

Newindex(self, index, value) The newindex metamethod can be used to tell the Lua virtual machine what to do when a program attempts to add a new field in a table. If it finds it, it will return the value corresponding to that index in the table that is specified in the index metamethod. If it is a table, Lua will, when a program attempts to index a field of the table that doesn't exist, look in that table for the same index. Most metamethods can only be functions, but the index metamethod can also be a table. Finally, it can be noted that the function tells the Lua virtual machine what should be given to the code that indexed the table by returning a value. The second argument is the index that was attempted to be indexed. In the case here, we could just refer directly to the associative_array variable, but this is useful when a single metatable is used for many tables. The first argument, self, is the table of which the index metamethod was invoked. Another thing that may be noticed is that the index metamethod is in fact a function (most metamethods are functions, but not all are), which takes two arguments. This is always the case: when Lua looks in a table's metatable for metamethods, it looks for indices that correspond to the name of a metamethod and that start with two underscores. One of the things that may be noticed is that the name of the field containing the index metamethod is prefixed with two underscores. There are many things that may be noticed in the above example.















Lua table insert