Skip to main content
The LuaValue class represents any value in Lua, including nil, booleans, numbers, strings, tables, functions, and userdata.

Properties

Type
DataType
Gets the type of the value (Nil, Boolean, Number, String, Function, Table, UserData, Thread, etc.)
if (value.Type == DataType.Number) {
    Console.WriteLine(value.Number);
}
Number
double
Gets the numeric value. Valid only if Type is DataType.Number.
LuaValue num = LuaValue.NewNumber(42.5);
double d = num.Number; // 42.5
String
string
Gets the string value. Valid only if Type is DataType.String.
LuaValue str = LuaValue.NewString("Hello");
string s = str.String; // "Hello"
Boolean
bool
Gets the boolean value. Valid only if Type is DataType.Boolean.
LuaValue val = LuaValue.True;
bool b = val.Boolean; // true
Table
Table
Gets the table. Valid only if Type is DataType.Table.
LuaValue tbl = script.Globals;
Table t = tbl.Table;
Function
Closure
Gets the function (closure). Valid only if Type is DataType.Function.
Callback
CallbackFunction
Gets the CLR callback. Valid only if Type is DataType.ClrFunction.
Coroutine
Coroutine
Gets the coroutine handle. Valid only if Type is DataType.Thread.
UserData
UserData
Gets the userdata. Valid only if Type is DataType.UserData.
Tuple
LuaValue[]
Gets the values in the tuple. Valid only if Type is DataType.Tuple.

Static Instances

Nil
LuaValue
A preinitialized, readonly instance representing nil.
return LuaValue.Nil;
Void
LuaValue
A preinitialized, readonly instance representing void (no value).
True
LuaValue
A preinitialized, readonly instance representing true.
False
LuaValue
A preinitialized, readonly instance representing false.

Factory Methods

NewNumber
method
Creates a new LuaValue initialized to the specified number.
LuaValue val = LuaValue.NewNumber(3.14);
NewString
method
Creates a new LuaValue initialized to the specified string.
LuaValue val = LuaValue.NewString("Hello, World!");
NewBoolean
method
Creates a new LuaValue initialized to the specified boolean.
LuaValue val = LuaValue.NewBoolean(true);
NewTable
method
Creates a new LuaValue initialized to a new table.
LuaValue tbl = LuaValue.NewTable(script);
NewCallback
method
Creates a new LuaValue initialized to a CLR callback function.
LuaValue callback = LuaValue.NewCallback(
    (ctx, args) => LuaValue.NewString("Hello from C#!")
);
script.Globals["myFunc"] = callback;
NewTuple
method
Creates a new tuple from the specified values.
LuaValue tuple = LuaValue.NewTuple(
    LuaValue.NewNumber(1),
    LuaValue.NewString("two"),
    LuaValue.True
);
NewNil
method
Creates a new writable nil value.
LuaValue val = LuaValue.NewNil();

Conversion Methods

FromObject
method
Creates a LuaValue from a CLR object.
returns
LuaValue
The converted LuaValue
LuaValue val = LuaValue.FromObject(script, 42);
LuaValue str = LuaValue.FromObject(script, "hello");
ToObject
method
Converts this LuaValue to a CLR object.
returns
object
The converted CLR object
LuaValue val = LuaValue.NewNumber(42);
object obj = val.ToObject(); // Returns 42.0 (double)
ToObject<T>
method
Converts this LuaValue to a CLR object of the specified type.
returns
T
The converted CLR object
LuaValue val = script.DoString("return 42");
int num = val.ToObject<int>(); // 42

Type Checking and Coercion

CastToNumber
method
Attempts to cast this value to a number, with string coercion.
returns
double?
The numeric value, or null if not convertible
LuaValue str = LuaValue.NewString("42");
double? num = str.CastToNumber(); // 42.0
CastToString
method
Attempts to cast this value to a string, with number coercion.
returns
string
The string value, or null if not convertible
LuaValue num = LuaValue.NewNumber(42);
string str = num.CastToString(); // "42"
CastToBool
method
Casts this value to a boolean following Lua rules (false and nil are false, everything else is true).
returns
bool
The boolean value
LuaValue.Nil.CastToBool(); // false
LuaValue.False.CastToBool(); // false
LuaValue.NewNumber(0).CastToBool(); // true (in Lua, 0 is truthy)
IsNil
method
Determines whether this value is nil or void.
returns
bool
True if nil or void
if (value.IsNil()) {
    // Handle nil case
}
IsNotNil
method
Determines whether this value is not nil or void.
returns
bool
True if not nil or void
CheckType
method
Checks that this value is of the specified type, throwing an exception if not.
returns
LuaValue
This value (for chaining)
value.CheckType("myFunc", DataType.Number, 1);

Utility Methods

GetLength
method
Gets the length of a string or table value.
returns
LuaValue
The length as a LuaValue
LuaValue str = LuaValue.NewString("hello");
int len = (int)str.GetLength().Number; // 5
ToScalar
method
Converts a tuple to a scalar value (returns the first element). If already scalar, returns this.
returns
LuaValue
The scalar value

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);
});

Build docs developers (and LLMs) love