ScriptFunctionDelegate types allow Lua functions to be wrapped as C# delegates, enabling seamless integration between Lua and C# code.
Delegates
A delegate type that wraps a Lua function with object return type.
The return value of the Lua function
A generic delegate type that wraps a Lua function with strongly-typed return value.
The return value automatically converted to type T
Usage
These delegates are obtained fromClosure objects using the GetDelegate methods:
Examples
Basic Usage
Passing Complex Arguments
Using with LINQ
Callback Pattern
Event Handlers
Multiple Return Values
Error Handling
Type Conversion
Arguments passed to the delegate are automatically converted from C# to Lua types:- Numbers (int, double, etc.) → Lua numbers
- Strings → Lua strings
- Booleans → Lua booleans
- null → Lua nil
- Collections → Lua tables
- Other objects → Lua userdata
- Lua numbers → double (or requested numeric type)
- Lua strings → string
- Lua booleans → bool
- Lua tables → Table or Dictionary/List (depending on structure)
- Lua nil → null
- Lua userdata → Original CLR object
Performance Considerations
While convenient, delegate wrappers add a small overhead compared to calling functions directly viaScript.Call(). For performance-critical code with many calls, consider:
- Caching the delegate instead of getting it each time
- Using
Script.Call()directly for maximum performance - Batching operations when possible