Difference between revisions of "Lua: Tables"

From Mario Fan Games Galaxy Wiki
 
m
Line 15: Line 15:
 
Values in tables can be accessed in a few ways. One way is via dot syntax: '''table1.key''' will return ''test''. Another is by specifying an integer index: '''table1[3]''' will return ''5'', not ''true'', as ''5'' was explicitly stored in '[3]'. Strings can also used within square brackets: '''table1["what"]''' is exactly the same as '''table1.what''', but does allow you to evaluate a string within the brackets.  
 
Values in tables can be accessed in a few ways. One way is via dot syntax: '''table1.key''' will return ''test''. Another is by specifying an integer index: '''table1[3]''' will return ''5'', not ''true'', as ''5'' was explicitly stored in '[3]'. Strings can also used within square brackets: '''table1["what"]''' is exactly the same as '''table1.what''', but does allow you to evaluate a string within the brackets.  
  
Functions inside tables can called in various ways; remember, they are variables, after all. '''table1.testfunc(arg)''' and '''table1["testfunc"](arg)''' are two valid ways to call a function. Functions are also able to use colon syntax, which effectively gives Lua [[Lua: Basic OO|OO]] capabilities. '''table1:testfunc(arg)''' is the same as '''table1.testfunc(self, arg)'''.
+
Functions inside tables can called in various ways; remember, they are variables, after all. '''table1.testfunc(arg)''' and '''table1["testfunc"](arg)''' are two valid ways to call a function. Functions are also able to use colon syntax, which effectively gives Lua [[Lua: Basic OO|OO]] capabilities. '''table1:testfunc(arg)''' is the same as '''table1.testfunc(table1, arg)'''.
 +
 
 +
''[[Lua: Metatables|Metatables]]'' are normal Lua tables that can be "attached" to other tables. They allow you to define otherwise undefined behavior: normally, adding two tables would result in an error. However, metatables allow you to define exactly how tables should be added, which would make adding tables a valid operation.

Revision as of 02:38, 18 May 2009

Lua
Lua.gif
Basics
Intermediate
Advanced
XLua
Add to this template
 Standardwikimessagebox.png This article assumes the use of Lua 5.1.

Information may not be accurate or may need revision if you are using a different version.

 Stub.png This article or section is in need of expansion.

Please add further information.

Tables are the only real complex data structure provided in Lua. Tables are basically arrays, specifically associative arrays, associating a key with a corresponding value. Unlike most languages, Lua's tables start at 1 instead of 0. Lua tables can store every other data type, and even mixed types. The following is a valid table:

table1 = {
    key = "test", -- defines 'key' and assigns 'test' to it 
    [3] = 5, -- assigns '5' to index 3
    ["what"] = true,
    table2 = { }, -- creates a subtable in this table 
    testfunc = function(arg) return 10 end, -- this way of defining functions must be used in tables 
    not_here = nil, -- this value does not exist
    [0] = "skipped", -- allowed but more or less ignored by default functions
}

Values in tables can be accessed in a few ways. One way is via dot syntax: table1.key will return test. Another is by specifying an integer index: table1[3] will return 5, not true, as 5 was explicitly stored in '[3]'. Strings can also used within square brackets: table1["what"] is exactly the same as table1.what, but does allow you to evaluate a string within the brackets.

Functions inside tables can called in various ways; remember, they are variables, after all. table1.testfunc(arg) and table1["testfunc"](arg) are two valid ways to call a function. Functions are also able to use colon syntax, which effectively gives Lua OO capabilities. table1:testfunc(arg) is the same as table1.testfunc(table1, arg).

Metatables are normal Lua tables that can be "attached" to other tables. They allow you to define otherwise undefined behavior: normally, adding two tables would result in an error. However, metatables allow you to define exactly how tables should be added, which would make adding tables a valid operation.