Skip to main content
The Script class is the central class for executing Lua code in SolarSharp. Multiple Script objects can coexist in the same program but cannot share data among themselves unless some mechanism is put in place.

Constructors

Script()
constructor
Creates a new Script instance with default core modules (Preset_Default).
var script = new Script();
Script(CoreModules)
constructor
Creates a new Script instance with specified core modules.
var script = new Script(CoreModules.Preset_HardSandbox);

Properties

Globals
Table
The default global table for this script. Execution uses this table unless a different table is intentionally passed.
script.Globals["myVar"] = 42;
Options
ScriptOptions
Gets access to the script options for this instance.
Registry
Table
A predefined table that can be used by any CLR code to store Lua values. Use unique keys (GUIDs, library names, or userdata with CLR object addresses) to avoid collisions.
GlobalOptions
ScriptGlobalOptions
Gets the global options that cannot be customized per-script.
DefaultOptions
ScriptOptions
Gets or sets the default script options for all newly created scripts.
SourceCodeCount
int
Gets the number of source code chunks loaded in this script.

Loading Methods

LoadString
method
Loads a string containing Lua/SolarSharp code.
returns
LuaValue
A function which will execute the loaded code when called
LuaValue chunk = script.LoadString("return 1 + 1");
LuaValue result = script.Call(chunk); // returns 2
LoadFile
method
Loads a file containing Lua/SolarSharp code.
returns
LuaValue
A function which will execute the loaded code when called
LuaValue chunk = script.LoadFile("script.lua");
script.Call(chunk);
LoadStream
method
Loads a stream containing Lua/SolarSharp code. Does not close the stream.
returns
LuaValue
A function which will execute the loaded code when called

Execution Methods

DoString
method
Loads and executes a string containing Lua/SolarSharp code.
returns
LuaValue
The result of executing the code
LuaValue result = script.DoString("return 2 + 2");
Console.WriteLine(result.Number); // 4
DoFile
method
Loads and executes a file containing Lua/SolarSharp code.
returns
LuaValue
The result of executing the file
script.DoFile("init.lua");
Call
method
Calls a Lua function.
returns
LuaValue
The return value(s) of the function call
LuaValue func = script.Globals.Get("myFunction");
LuaValue result = script.Call(func, LuaValue.NewNumber(5));

Static Helper Methods

RunString
method
Quickly runs Lua code with all defaults.
returns
LuaValue
The result of execution
LuaValue result = Script.RunString("return 'Hello, World!'");
RunFile
method
Quickly runs a Lua file with all defaults.
returns
LuaValue
The result of execution
Script.RunFile("script.lua");

Coroutine Methods

CreateCoroutine
method
Creates a coroutine pointing at the specified function.
returns
LuaValue
The coroutine handle
LuaValue func = script.LoadString("return coroutine.yield(1)");
LuaValue co = script.CreateCoroutine(func);

Type Metatable Methods

GetTypeMetatable
method
Gets the metatable for a specific Lua type.
returns
Table
The metatable for the type, or null
SetTypeMetatable
method
Sets the metatable for a specific Lua type.
var mt = new Table(script);
mt["__add"] = (Func<string, string, string>)((a, b) => a + b);
script.SetTypeMetatable(DataType.String, mt);

Constants

VERSION
string
The version of the SolarSharp engine (“2.0.0.0”)
LUA_VERSION
string
The Lua version being supported (“5.2”)

Example Usage

using SolarSharp.Interpreter;

// Create a script instance
var script = new Script();

// Execute Lua code
script.DoString(@"
    function greet(name)
        return 'Hello, ' .. name .. '!'
    end
");

// Call a Lua function from C#
LuaValue greetFunc = script.Globals.Get("greet");
LuaValue result = script.Call(greetFunc, "World");
Console.WriteLine(result.String); // "Hello, World!"

// Access global variables
script.Globals["myNumber"] = 42;
LuaValue value = script.DoString("return myNumber * 2");
Console.WriteLine(value.Number); // 84

Build docs developers (and LLMs) love