Bridge between Lua and the .NET.
| Hello there! |
|---|
Thank you for checking out this project.
We've made the project Open Source and MIT license so everyone can enjoy it.
To deliver a project with quality we have to spent a lot of time working on it.
⭐️ If you liked the project please star it.
We also appreaciate any Sponsor [ Patreon | PayPal ] NLua =======
| NuGet |
|---|
| Status | |
|---|---|
Bridge between Lua world and the .NET (compatible with .NET/UWP/Mac/Linux/Android/iOS/tvOS)
msbuild NLua.slnNLua allows the usage of Lua from C#, on UWP, Windows, Linux, Mac, iOS , Android.
NLua is a fork project of LuaInterface (from Fábio Mascarenhas/Craig Presti).
Example: You can use/instantiate any .NET class without any previous registration or annotation.
public class SomeClass
{
public string MyProperty {get; private set;}
public SomeClass (string param1 = "defaulValue")
{
MyProperty = param1;
}
public int Func1 ()
{
return 32;
}
public string AnotherFunc (int val1, string val2)
{
return "Some String";
}
public static string StaticMethod (int param)
{
return "Return of Static Method";
}
}
NLua runs on top of KeraLua binding, it encodes the string using the ASCII encoding by default.
If you want to use UTF-8 encoding, just set the Lua.State.Encoding property to Encoding.UTF8:
using (Lua lua = new Lua())
{
lua.State.Encoding = Encoding.UTF8;
lua.DoString("res = 'Файл'");
string res = (string)lua["res"];
Assert.AreEqual("Файл", res);
}
Creating Lua state:
using NLua;
Lua state = new Lua ()
Evaluating simple expressions:
var res = state.DoString ("return 10 + 3*(5 + 2)")[0] as double;
// Lua can return multiple values, for this reason DoString return a array of objectsPassing raw values to the state:
double val = 12.0;
state ["x"] = val; // Create a global value 'x'
var res = (double)state.DoString ("return 10 + x*(5 + 2)")[0];Retrieving global values:
state.DoString ("y = 10 + x*(5 + 2)");
double y = (double) state ["y"]; // Retrieve the value of yRetrieving Lua functions:
state.DoString (@"
function ScriptFunc (val1, val2)
if val1 > val2 then
return val1 + 1
else
return val2 - 1
end
end
");
var scriptFunc = state ["ScriptFunc"] as LuaFunction;
var res = (int)scriptFunc.Call (3, 5).First ();
// LuaFunction.Call will also return a array of objects, since a Lua function
// can return multiple values##Using the .NET objects.##
Passing .NET objects to the state:
SomeClass obj = new SomeClass ("Param");
state ["obj"] = obj; // Create a global value 'obj' of .NET type SomeClass
// This could be any .NET object, from BCL or from your assembliesUsing .NET assemblies inside Lua:
To access any .NET assembly to create objects, events etc inside Lua you need to ask NLua to use CLR as a Lua package.
To do this just use the method LoadCLRPackage and use the import function inside your Lua script to load the Assembly.
state.LoadCLRPackage ();
state.DoString (@" import ('MyAssembly', 'MyNamespace')
import ('System.Web') ");
// import will load any .NET assembly and they will be available inside the Lua context.Creating .NET objects:
To create object you only need to use the class name with the ().
state.DoString (@"
obj2 = SomeClass() -- you can suppress default values.
client = WebClient()
");Calling instance methods:
To call instance methods you need to use the : notation, you can call methods from objects passed to Lua or to objects created inside the Lua context.
state.DoString (@"
local res1 = obj:Func1()
local res2 = obj2:AnotherFunc (10, 'hello')
local res3 = client:DownloadString('http://nlua.org')
");Calling static methods:
You can call static methods using only the class name and the . notation from Lua.
state.DoString (@"
local res4 = SomeClass.StaticMethod(4)
");Calling properties:
You can get (or set) any property using . notation from Lua.
state.DoString (@"
local res5 = obj.MyProperty
");All methods, events or property need to be public available, NLua will fail to call non-public members.
If you are using Xamarin.iOS you need to Preserve the class you want to use inside NLua, otherwise the Linker will remove the class from final binary if the class is not in use.
##Sandboxing##
There is many ways to sandbox scripts inside your application. I strongly recommend you to use plain Lua to do your sandbox.
You can re-write the import function before load the user script and if the user try to import a .NET assembly nothing will happen.
state.DoString (@"
import = function () end
");Download package link).nupkg to .zipPlugins/KeraLua and Plugins/NLuanetstandard2.0 contents of those zip files into the newly created unity folders. Your tree should look roughly like this (with KeraLua runtimes customized to your build targets)…Platform settings for both editor and standalone based on the folder. Don't forget to applypackage.path you can do it with something like this lua.DoString("package.path = package.path .. \";" + Application.persistentDataPath + "/?/?.lua;" + Application.persistentDataPath + "/?.lua\"");print function to see lua logs)double typeCopyright (c) 2026 Vinicius Jarina ([email protected])
NLua huge cleanup and refactor after a few years.
###Help NLua###
- Contributing
- NLua uses the Mono Code-Style http://www.mono-project.com/Coding_Guidelines .
- Please, do not change the line-end or re-indent the code.
- Run the tests before you push.
- Avoid pushing style changes (unless they are really needed), renaming and move code.
Copyright (c) 2003-2006 Fabio Mascarenhas de Queiroz
Maintainer: Craig Presti, [email protected]
lua51.dll and lua51.exe are Copyright (c) 2005 Tecgraf, PUC-Rio
Look at src/TestNLua/TestLua to see an example of usage from C#
(optionally you can run this from inside the NLua solution using the debugger).
Also provides a good example of how to override .NET methods of Lua and usage of NLua
from within your .NET application.
Look at samples/testluaform.lua to see examples of how to use .NET inside Lua
More installation and usage instructions in the doc/guide.pdf file.
Fix a serious bug w.r.t. garbage collection - made especially apparent with the new lua5.1 switch: If you were very unlucky with timing sometimes Lua would loose track of pointers to CLR functions.
When I added support for static methods, I allowed the user to use either a colon or a dot to separate the method from the class name. This was not correct - it broke disambigu
No open issues yet, or sync has not completed.