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
Creates a new table. The owning script (can be null for prime tables)
Size hint for the array component (optional, default 0)
Size hint for the associative component (optional, default 0)
var table = new Table ( script );
var optimized = new Table ( script , arraySizeHint : 100 , associativeSizeHint : 50 );
Properties
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
Gets or sets the metatable associated with this table. var mt = new Table ( script );
mt [ "__index" ] = someFunction ;
table . MetaTable = mt ;
Gets the script owning this table.
Enumerates all values in the table. foreach ( var value in table . Values ) {
Console . WriteLine ( value );
}
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]
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
Gets the value associated with the specified key. The value, or LuaValue.Nil if not found
LuaValue val = table . Get ( LuaValue . NewString ( "key" ));
Gets the value associated with the specified string key. The value, or LuaValue.Nil if not found
LuaValue name = table . Get ( "name" );
Gets the value associated with the specified integer key. The value, or LuaValue.Nil if not found
LuaValue first = table . Get ( 1 ); // Lua arrays are 1-indexed
Gets the value associated with the specified object key. The key (automatically converted to LuaValue)
The value, or LuaValue.Nil if not found
Set Methods
Sets the value associated with the specified key. The value (use LuaValue.Nil to remove)
table . Set ( LuaValue . NewString ( "key" ), LuaValue . NewNumber ( 42 ));
table . Set ( LuaValue . NewString ( "key" ), LuaValue . Nil ); // Remove
Sets the value associated with the specified string key. table . Set ( "name" , LuaValue . NewString ( "John" ));
Sets the value associated with the specified object key.
Array Methods
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}
Inserts a value at the specified index, shifting subsequent elements. The index at which to insert (1-based)
table . Insert ( 1 , LuaValue . NewString ( "inserted at start" ));
Sorts the array segment of the table. sortComparer
IComparer<LuaValue>
required
The comparer to use for sorting
table . Sort ( Comparer < LuaValue >. Create (( a , b ) =>
a . Number . CompareTo ( b . Number )
));
Utility Methods
Removes all items from the table.
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 ;