Search code examples
c#luanlua

Instance of C# class with NLua


I'm trying to create an instance of a C# class within a lua file Using NLua

Here's my code.

class Program
{
    public static void Main (string[] args)
    {

        Lua lua = new Lua();
        lua.LoadCLRPackage();

        lua.DoString(@"
                        import ('LuaTest.exe', 'LuaTest')
                        test = Test()
                    ");
    }
}

public class Test
{
    public Test()
    {
        Console.WriteLine("IT WORKED");
    }
}

But that doesn't seem to work, I've looked around and tried a number of different ways. my error with most ways I've tried is this:

An unhandled exception of type 'NLua.Exceptions.LuaScriptException' occurred in NLua.dll Additional information: [string "chunk"]:3: attempt to call global 'Test' (a nil value)

It's a bit odd because this is straight out of their example code? https://github.com/NLua/NLua

Thanks for the help guys.

Bit of a Rant:

If I'm doing something incredably wrong please let me know. On a side note I'm using the Pure C# build, not sure if that makes a difference here or not, I didn't see any warnings about it? but the whole thing seems widely undocumented...

If anyone has any suggestions for a better pure C# Lua Library I'm all ears.


Solution

  • I am sorry about that. The README File was wrong.

    Just remove the ".exe" and shuold work fine

    class Program
    {
        public static void Main (string[] args)
        {
    
            Lua lua = new Lua();
            lua.LoadCLRPackage();
    
            lua.DoString(@"
                            import ('LuaTest', 'LuaTest')
                            test = Test()
                        ");
        }
    }
    
    public class Test
    {
        public Test()
        {
            Console.WriteLine("IT WORKED");
        }
    }
    

    https://github.com/NLua/NLua

    I've already fixed the README from github.com/nlua/nlua