Skip to main content
The Table class represents Lua tables, which are the primary data structure in Lua, supporting both array-like and associative (dictionary-like) access patterns.

Constructor

Table(Script, int, int)
constructor
Creates a new table.
var table = new Table(script);
var optimized = new Table(script, arraySizeHint: 100, associativeSizeHint: 50);

Properties

Length
int
Gets the length of the “array part” (the number of consecutive integer keys starting from 1).
var table = new Table(script);
table[1] = "first";
table[2] = "second";
Console.WriteLine(table.Length); // 2
MetaTable
Table
Gets or sets the metatable associated with this table.
var mt = new Table(script);
mt["__index"] = someFunction;
table.MetaTable = mt;
OwnerScript
Script
Gets the script owning this table.
Values
IEnumerable<LuaValue>
Enumerates all values in the table.
foreach (var value in table.Values) {
    Console.WriteLine(value);
}

Indexer

this[object key]
indexer
Gets or sets values using object keys. Automatically marshals CLR and Lua objects.
// Setting values
table[1] = "first item";
table["name"] = "John";
table["nested"] = new Table(script);

// Getting values
object value = table["name"];
string name = (string)table["name"];
this[params object[] keys]
indexer
Gets or sets values using multiple keys to access nested tables.
table["player", "stats", "health"] = 100;
int health = (int)table["player", "stats", "health"];

Get Methods

Get(LuaValue)
method
Gets the value associated with the specified key.
returns
LuaValue
The value, or LuaValue.Nil if not found
LuaValue val = table.Get(LuaValue.NewString("key"));
Get(string)
method
Gets the value associated with the specified string key.
returns
LuaValue
The value, or LuaValue.Nil if not found
LuaValue name = table.Get("name");
Get(int)
method
Gets the value associated with the specified integer key.
returns
LuaValue
The value, or LuaValue.Nil if not found
LuaValue first = table.Get(1); // Lua arrays are 1-indexed
Get(object)
method
Gets the value associated with the specified object key.
returns
LuaValue
The value, or LuaValue.Nil if not found

Set Methods

Set(LuaValue, LuaValue)
method
Sets the value associated with the specified key.
table.Set(LuaValue.NewString("key"), LuaValue.NewNumber(42));
table.Set(LuaValue.NewString("key"), LuaValue.Nil); // Remove
Set(string, LuaValue)
method
Sets the value associated with the specified string key.
table.Set("name", LuaValue.NewString("John"));
Set(object, LuaValue)
method
Sets the value associated with the specified object key.

Array Methods

Append
method
Appends a value to the end of the array part (at index Length + 1).
var array = new Table(script);
array.Append(LuaValue.NewNumber(1));
array.Append(LuaValue.NewNumber(2));
array.Append(LuaValue.NewNumber(3));
// array = {1, 2, 3}
Insert
method
Inserts a value at the specified index, shifting subsequent elements.
table.Insert(1, LuaValue.NewString("inserted at start"));
Sort
method
Sorts the array segment of the table.
table.Sort(Comparer<LuaValue>.Create((a, b) => 
    a.Number.CompareTo(b.Number)
));

Utility Methods

Clear
method
Removes all items from the table.
table.Clear();
GetEnumerator
method
Gets an enumerator for iterating over all key-value pairs.
foreach (var kvp in table) {
    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}

Example Usage

using SolarSharp.Interpreter;
using SolarSharp.Interpreter.DataTypes;

var script = new Script();

// Create and populate a table
var table = new Table(script);

// Array-like access (1-indexed)
table[1] = "first";
table[2] = "second";
table[3] = "third";
Console.WriteLine(table.Length); // 3

// Associative access
table["name"] = "John";
table["age"] = 30;
table["active"] = true;

// Nested tables
var nested = new Table(script);
nested["x"] = 10;
nested["y"] = 20;
table["position"] = nested;

// Access nested values
Console.WriteLine(table["position", "x"]); // 10

// Iterate over table
foreach (var kvp in table) {
    Console.WriteLine($"{kvp.Key}: {kvp.Value}");
}

// Use from Lua
script.Globals["myTable"] = table;
script.DoString(@"
    print(myTable[1])        -- first
    print(myTable.name)      -- John
    print(myTable.position.x) -- 10
");

// Metatables
var mt = new Table(script);
mt["__tostring"] = (Func<Table, string>)((t) => "Custom Table");
table.MetaTable = mt;

Build docs developers (and LLMs) love