Skip to content
Kevin Zhao edited this page Jan 25, 2018 · 3 revisions

Tables

Tables can be created using the CreateTable function:

var table = lua.CreateTable();

The table members can then be accessed/modified using the indexer. Since LuaTable implements IDictionary<object, object>, you can also use it exactly like a dictionary:

lua.DoString("table = { x = 6, y = 9, z = 10 }");

var table = (LuaTable)lua["table"];
Assert.Equal(3, table.Count);
Assert.True(table.Remove("x"));
Assert.False(table.ContainsKey("x"));

Metatables

The Metatable property allows you to get or set the metatable. This allows you to define custom operations on tables, essentially transforming them into full-fledged objects.

Dynamic Access

LuaTables support dynamic access. You may access string keys directly:

dynamic table = lua.CreateTable();
table.x = 10;
table.x += 10;

If the table has the relevant metamethods (such as __add for addition, __sub for subtraction, etc), you may also perform operations:

dynamic table = ...
dynamic table2 = ...
dynamic sum = table + table2;
Clone this wiki locally