public class Person{ public string Name { get; set; } public int Age { get; set; } public void Greet() { Console.WriteLine($"Hello, I'm {Name}"); }}// Register the typeUserData.RegisterType<Person>();
Now you can use this type in Lua:
-- Access from C# (assuming person object is passed to Lua)person.Name = "Alice"person.Age = 30person:Greet() -- Prints: Hello, I'm Alice
Use the [SolarSharpUserData] attribute to mark types for automatic registration:
[SolarSharpUserData]public class GameCharacter{ public string Name { get; set; } public int Health { get; set; } public void TakeDamage(int amount) { Health -= amount; }}// Register all marked types in the assemblyUserData.RegisterAssembly();
Call RegisterAssembly() without parameters to register types from the calling assembly, or pass an Assembly parameter to register from a specific assembly.
[SolarSharpUserData]public class Configuration{ [SolarSharpVisible(true)] private string secretValue; // Now accessible from Lua [SolarSharpVisible(true)] internal void InternalMethod() { // This internal method is now accessible from Lua }}
var script = new Script();var person = new Person { Name = "Bob", Age = 25 };// Set as global variablescript.Globals["player"] = person;// Use in Luascript.DoString(@" print(player.Name) -- Prints: Bob player.Age = 26 player:Greet()");
var script = new Script();script.DoString(@" function processCharacter(char) char.Health = char.Health - 10 return char.Health end");var character = new GameCharacter { Name = "Hero", Health = 100 };var result = script.Call(script.Globals["processCharacter"], character);
script.Globals["createPerson"] = (Func<string, int, Person>)((name, age) =>{ return new Person { Name = name, Age = age };});script.DoString(@" local newPerson = createPerson('Charlie', 35) print(newPerson.Name)");
Static members can be accessed through static userdata:
public class MathUtils{ public static double PI = 3.14159; public static double Square(double x) { return x * x; }}UserData.RegisterType<MathUtils>();var script = new Script();script.Globals["MathUtils"] = UserData.CreateStatic<MathUtils>();script.DoString(@" print(MathUtils.PI) -- 3.14159 print(MathUtils.Square(5)) -- 25");
public class Calculator{ private double result = 0; public double Add(double value) { result += value; return result; } public double GetResult() { return result; }}
local calc = Calculator()calc:Add(10)calc:Add(5)print(calc:GetResult()) -- 15
Use colon syntax (:) for instance methods in Lua, which automatically passes the object as the first parameter.
SolarSharp automatically resolves method overloads based on argument types:
public class Printer{ public void Print(string message) { Console.WriteLine($"String: {message}"); } public void Print(int number) { Console.WriteLine($"Number: {number}"); } public void Print(string msg, int times) { for (int i = 0; i < times; i++) Console.WriteLine(msg); }}
Use [SolarSharpProperty] to expose a property with a different name in Lua:
public class Item{ [SolarSharpProperty("id")] public string ItemIdentifier { get; set; } [SolarSharpProperty("qty")] public int Quantity { get; set; }}
local item = Item()item.id = "SWORD_001"item.qty = 5
You can implement Lua metamethods in C# using the [SolarSharpUserDataMetamethod] attribute:
[SolarSharpUserData]public class Vector2{ public double X { get; set; } public double Y { get; set; } [SolarSharpUserDataMetamethod("__add")] public static Vector2 Add(Vector2 a, Vector2 b) { return new Vector2 { X = a.X + b.X, Y = a.Y + b.Y }; } [SolarSharpUserDataMetamethod("__tostring")] public string ToString() { return $"({X}, {Y})"; }}
Register extension methods to add functionality to existing types:
public static class StringExtensions{ public static string Reverse(this string str) { return new string(str.Reverse().ToArray()); }}// Register extension typeUserData.RegisterExtensionType(typeof(StringExtensions));
local text = "hello"print(text:Reverse()) -- "olleh"