The LuaValue class represents any value in Lua, including nil, booleans, numbers, strings, tables, functions, and userdata.
Properties
Gets the type of the value (Nil, Boolean, Number, String, Function, Table, UserData, Thread, etc.)if (value.Type == DataType.Number) {
Console.WriteLine(value.Number);
}
Gets the numeric value. Valid only if Type is DataType.Number.LuaValue num = LuaValue.NewNumber(42.5);
double d = num.Number; // 42.5
Gets the string value. Valid only if Type is DataType.String.LuaValue str = LuaValue.NewString("Hello");
string s = str.String; // "Hello"
Gets the boolean value. Valid only if Type is DataType.Boolean.LuaValue val = LuaValue.True;
bool b = val.Boolean; // true
Gets the table. Valid only if Type is DataType.Table.LuaValue tbl = script.Globals;
Table t = tbl.Table;
Gets the function (closure). Valid only if Type is DataType.Function.
Gets the CLR callback. Valid only if Type is DataType.ClrFunction.
Gets the coroutine handle. Valid only if Type is DataType.Thread.
Gets the userdata. Valid only if Type is DataType.UserData.
Gets the values in the tuple. Valid only if Type is DataType.Tuple.
Static Instances
A preinitialized, readonly instance representing nil.
A preinitialized, readonly instance representing void (no value).
A preinitialized, readonly instance representing true.
A preinitialized, readonly instance representing false.
Factory Methods
Creates a new LuaValue initialized to the specified number.
LuaValue val = LuaValue.NewNumber(3.14);
Creates a new LuaValue initialized to the specified string.
LuaValue val = LuaValue.NewString("Hello, World!");
Creates a new LuaValue initialized to the specified boolean.
LuaValue val = LuaValue.NewBoolean(true);
Creates a new LuaValue initialized to a new table.
Size hint for array part (optional)
Size hint for associative part (optional)
LuaValue tbl = LuaValue.NewTable(script);
Creates a new LuaValue initialized to a CLR callback function.
callBack
Func<ScriptExecutionContext, CallbackArguments, LuaValue>
required
The callback delegate
Function name for debugging (optional)
LuaValue callback = LuaValue.NewCallback(
(ctx, args) => LuaValue.NewString("Hello from C#!")
);
script.Globals["myFunc"] = callback;
Creates a new tuple from the specified values.
values
params LuaValue[]
required
The values to include in the tuple
LuaValue tuple = LuaValue.NewTuple(
LuaValue.NewNumber(1),
LuaValue.NewString("two"),
LuaValue.True
);
Creates a new writable nil value.LuaValue val = LuaValue.NewNil();
Conversion Methods
Creates a LuaValue from a CLR object.
The CLR object to convert
LuaValue val = LuaValue.FromObject(script, 42);
LuaValue str = LuaValue.FromObject(script, "hello");
Converts this LuaValue to a CLR object.LuaValue val = LuaValue.NewNumber(42);
object obj = val.ToObject(); // Returns 42.0 (double)
Converts this LuaValue to a CLR object of the specified type.LuaValue val = script.DoString("return 42");
int num = val.ToObject<int>(); // 42
Type Checking and Coercion
Attempts to cast this value to a number, with string coercion.The numeric value, or null if not convertible
LuaValue str = LuaValue.NewString("42");
double? num = str.CastToNumber(); // 42.0
Attempts to cast this value to a string, with number coercion.The string value, or null if not convertible
LuaValue num = LuaValue.NewNumber(42);
string str = num.CastToString(); // "42"
Casts this value to a boolean following Lua rules (false and nil are false, everything else is true).LuaValue.Nil.CastToBool(); // false
LuaValue.False.CastToBool(); // false
LuaValue.NewNumber(0).CastToBool(); // true (in Lua, 0 is truthy)
Determines whether this value is nil or void.if (value.IsNil()) {
// Handle nil case
}
Determines whether this value is not nil or void.
Checks that this value is of the specified type, throwing an exception if not.
Function name for error messages
Argument number for error messages (optional, default -1)
Validation flags (optional)
This value (for chaining)
value.CheckType("myFunc", DataType.Number, 1);
Utility Methods
Gets the length of a string or table value.LuaValue str = LuaValue.NewString("hello");
int len = (int)str.GetLength().Number; // 5
Converts a tuple to a scalar value (returns the first element). If already scalar, returns this.
Example Usage
using SolarSharp.Interpreter;
using SolarSharp.Interpreter.DataTypes;
// Creating values
LuaValue num = LuaValue.NewNumber(42);
LuaValue str = LuaValue.NewString("Hello");
LuaValue tbl = LuaValue.NewTable(script);
// Type checking
if (value.Type == DataType.Number) {
Console.WriteLine($"Number: {value.Number}");
}
// Conversion
LuaValue val = script.DoString("return 'test'");
string result = val.String;
// CLR interop
LuaValue fromClr = LuaValue.FromObject(script, new List<int> { 1, 2, 3 });
object toClr = fromClr.ToObject();
// Callbacks
script.Globals["add"] = LuaValue.NewCallback((ctx, args) => {
double a = args[0].Number;
double b = args[1].Number;
return LuaValue.NewNumber(a + b);
});