Search code examples
c#luainterfacenlua

Register functions without using them "Object reference not set to an instance of an object."


I have an IRC Bot in C# and I want to use Lua Scripting for the moment. On bot startup I want to register functions, and detect if a new file has been added then load it. I did the new file / reload scripts function already, but when I hit run I get "Object reference not set to an instance of an object." on a custom function I want users to be able to use.

Here's the current code:

public Lua lua;
public void RegisterFunctions() {
    lua.RegisterFunction("print", this, typeof(DashLua).GetMethod("ConsoleOut"));
}
#region Custom Functions for Lua
public void ConsoleOut(String line) {
    if (line == null) {
        Console.WriteLine("Script error: print() can't be null.");
    } else {
        Console.WriteLine(line);
    }
}

In the Main() of the bot I have just 2 lines currently:

    DashLua dash = new DashLua();
    dash.RegisterFunctions();

Solution

  • NLua (NuGet) only works on x64 builds which is why none of this actually worked. So if you're building a project and really need a Lua as a scripting language, you need to change your build to x64 in the Project's properties. That being said, you can get the Win32 version at their website which is a shame it's not on nuget.

    EDIT: When you go and download the Win32 build, it's empty. I guess they have not built it and encourage you to build it yourself.