Demonstrations of integrating Lua scripting into C# applications using NLua for dynamic behavior without recompilation.
Install NLua via NuGet:
Install-Package NLua -Version 1.5.1
NLua enables C# applications to execute and interact with Lua scripts dynamically, allowing behavior changes without recompiling. This example demonstrates:
- Registering C# methods for Lua to call
- Executing Lua scripts from C#
- Retrieving variables from Lua back to C#
- Method Registration — Register C# functions as Lua callables
- Script Execution — Run Lua scripts within C# context
- Variable Retrieval — Access Lua variables from C#
- Dynamic Behavior — Modify logic via scripts without recompiling
Create a new Lua state:
var state = new Lua();state["print"] = (Action<string>)LuaPrint;
state["magnitude"] = (Func<float, float, float>)CalculateMagnitude;Define and run Lua code:
function calculate()
local result = magnitude(3.0, 4.0)
print("Magnitude: " .. result)
end
calculate()Retrieve values from Lua:
var value = state["myVariable"];├── README.md
├── NLuaExamples.sln
└── src/
├── LuaInterop.cs — Main Lua integration class
├── Examples.cs — Example code demonstrating features
└── LuaScripts/
└── script.lua — Sample Lua scripts
- Single Lua instance per application context
- Method registration enables C# ↔ Lua communication
- Variable sharing through indexing (state["name"])
NLua automatically converts between:
- C# methods → Lua functions
- Lua tables → C# dictionaries
- Lua numbers → C# float/double
- Lua strings → C# strings
try
{
state.DoString("-- Lua code here");
}
catch (Exception ex)
{
Console.WriteLine("Lua error: " + ex.Message);
}state["onEvent"] = (Action<string>)HandleEvent;
// Call from Lua: onEvent("data")// Set table from C#
state.DoString("data = {}");
state["data"] = new LuaTable(state);
state.DoString("table.insert(data, 'value')");
// Retrieve from Lua
var result = state["data"];- Wrap Lua execution in try-catch for error handling
- Ensure C# method signatures match Lua function expectations
- NLua handles type conversions between C# and Lua
- Keep Lua scripts simple for better performance
- Use LuaTable for structured data exchange