Skip to content

Instantly share code, notes, and snippets.

@viniciusjarina
Created December 27, 2020 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viniciusjarina/9bee6a6650d4a2101576b6b19aa982a2 to your computer and use it in GitHub Desktop.
Save viniciusjarina/9bee6a6650d4a2101576b6b19aa982a2 to your computer and use it in GitHub Desktop.
NLuaPost.md

What is NLua?

NLua is a .NET library that allows you to easily integrate Lua in your C# application, calling Lua functions, reading Lua tables or calling C# methods from within Lua scripts. NLua is actually a fork of the project called LuaInterface from Craig Presti/Fabio Mascarenhas so most of features and documentation you find from LuaInterface should work with NLua.

Why you would want to use Lua on .NET?

You could use Lua to script parts of your application, allowing to quickly change behavior without having to rebuild your application. Scenarios like games, customization, extensions.

How to use NLua?

Install-Package NLua -Version 1.5.6
  • Add NLua namespace:
using NLua;
  • Create a state object:
Lua state = new Lua()
  • You can pass objects from C# to Lua context using the [] operator, you can pass strings, numbers, delegates, objects
state["var1"] = "someValue";

Here is a full example of an application passing values back and forth:

using System;
using NLua;

namespace TestNLua
{
    class MyClass
    {
        public int IntegerProperty { get; set; }

        public void DoSomething(string p)
        {
            Console.WriteLine($"Doing something: {p}");
        }
    }

    class MainClass
    {
        static Lua state;

        public static double Compute(double x)
        {
            return x * (x - 1);
        }

        public static void Main(string[] args)
        {
            var myobj = new MyClass();
            myobj.IntegerProperty = 17;

            state = new Lua();

            state["var1"] = "someValue";
            state["obj"] = myobj;
            state["compute"] = new Func<double, double>(Compute);

            // Will print "output: someValue"
            state.DoString(" print('output: ' .. var1) ");
            // Will print 18
            state.DoString(" print(obj.IntegerProperty + 1) ");
            // Will print "Doing something: hello"
            state.DoString(" obj:DoSomething('hello') ");
            // Will print "-0.1875"
            state.DoString(" print(compute(0.25)) ");

            // Reading values back
            state.DoString(" x = compute(0.75) ");
            state.DoString(" y = 2.0 * (obj.IntegerProperty * x) ");

            double x = state.GetNumber("x");
            double y = state.GetNumber("y");
            // Will print: x = -0.1875, y = -6.375
            Console.WriteLine($"x = {x}, y = {y}");
        }
    }
}

Which platforms are supported?

NLua supports Windows/ UWP, Linux, macOS, iOS/tvOS/watchOS, Android.

How it works?

NLua uses KeraLua which is a low-level binding for the Lua C API, KeraLua binding 1:1 every function from Lua C API.

Which version of Lua KeraLua is using?

KeraLua package uses the latest version of Lua (5.4.2)

What about NeoLua, MoonSharp?

Those are good libraries, I guess people that can't use p/Invoke can use those as alternatives. The performance can be very poor, and most of those libraries try to be compatible with Lua. I believe instead of reinventing the wheel is better just to use the battle tested Lua library.Lua has been developed for almost 28 years, so it would be a waste to try to rewrite it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment