Tables are the primary data structure in Lua, serving as both arrays and dictionaries. SolarSharp’s Table class provides a powerful C# interface for working with Lua tables.
using SolarSharp.Interpreter.DataTypes;Script script = new Script();// Empty tableTable table = new Table(script);// With size hints for performanceTable optimized = new Table(script, arraySizeHint: 100, // Expected array elements associativeSizeHint: 50 // Expected key-value pairs);// Initialize with array valuesTable array = new Table(script, LuaValue.NewNumber(1), LuaValue.NewNumber(2), LuaValue.NewNumber(3));
The Table class provides convenient indexer access:
Table table = new Table(script);// Set values using indexerstable["name"] = "Alice";table["age"] = 25;table[1] = "first";table[2] = "second";// Get values (auto-converts to C# types)string name = (string)table["name"]; // "Alice"int age = (int)table["age"]; // 25
// Set with LuaValue keys and valuestable.Set("key", LuaValue.NewString("value"));table.Set(LuaValue.NewNumber(42), LuaValue.NewBoolean(true));// Get returns LuaValueLuaValue value = table.Get("key");Console.WriteLine(value.String); // "value"// Integer keys (array access)LuaValue first = table.Get(1);// Returns nil for missing keysLuaValue missing = table.Get("nonexistent");Console.WriteLine(missing.IsNil()); // true
Table array = new Table(script, LuaValue.NewNumber(1), LuaValue.NewNumber(3));// Insert at index 2array.Insert(2, LuaValue.NewNumber(2));// Array is now: 1, 2, 3Console.WriteLine(array.Get(2).Number); // 2
Table array = new Table(script, LuaValue.NewNumber(1), LuaValue.NewNumber(2), LuaValue.NewNumber(3));// Remove element at index 2LuaValue removed = array.ArrayRemoveAt(2);Console.WriteLine(removed.Number); // 2// Array is now: 1, 3Console.WriteLine(array.Length); // 2
using System.Collections.Generic;Table array = new Table(script, LuaValue.NewNumber(3), LuaValue.NewNumber(1), LuaValue.NewNumber(2));// Sort with custom comparerarray.Sort(Comparer<LuaValue>.Create((a, b) => a.Number.CompareTo(b.Number)));// Array is now: 1, 2, 3
Table table = script.DoString(@" return { name = 'Alice', age = 30, city = 'New York' }").Table;// Iterate over all pairsforeach (var pair in table){ Console.WriteLine($"{pair.Key.ToPrintString()} = {pair.Value.ToPrintString()}");}
LuaValue key = LuaValue.Nil;while (true){ LuaValue result = table.GetNextFromIt(key); if (result.IsNil()) break; // Result is a tuple of (key, value) LuaValue nextKey = result.Tuple[0]; LuaValue value = result.Tuple[1]; Console.WriteLine($"{nextKey.ToPrintString()} = {value.ToPrintString()}"); key = nextKey;}
Script script = new Script();// Get global tableTable globals = script.Globals;// Set global variablesglobals["myVar"] = 42;globals["myFunc"] = (Func<int, int>)(x => x * 2);// Access from Luascript.DoString(@" print(myVar) -- 42 print(myFunc(5)) -- 10");
Script script = new Script();// Store data in registry (not accessible from Lua)Table registry = script.Registry;registry["my-private-data"] = myObject;// Retrieve latervar retrieved = registry.Get("my-private-data").ToObject();
The registry is useful for storing CLR objects that shouldn’t be directly accessible from Lua code.
// Efficient - pre-allocates spaceTable large = new Table(script, arraySizeHint: 1000, associativeSizeHint: 500);// Less efficient - multiple reallocationsTable large = new Table(script);for (int i = 0; i < 1000; i++) large[i] = i;
Table items = new Table(script);items.Append(LuaValue.NewString("sword"));items.Append(LuaValue.NewString("shield"));items.Append(LuaValue.NewString("potion"));for (int i = 1; i <= items.Length; i++){ Console.WriteLine(items.Get(i).String);}