2

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.

Mike Laren
  • 8,028
  • 17
  • 51
  • 70
Dusty
  • 413
  • 6
  • 17
  • Did you import the current executable into the Lua system? This was in the sample code: state.DoString (@" import ('MyAssembly.exe', 'MyNamespace') import ('System.Web') "); – Ron Beyer Apr 26 '15 at 03:09
  • @RonBeyer yes, I think so LuaTest.exe is my assembly and LuaTest is the namespace. – Dusty Apr 26 '15 at 03:14
  • Sorry, I missed that line in the code you posted, thats the only idea I had. – Ron Beyer Apr 26 '15 at 03:15
  • @RonBeyer No worries, I've been throwing my face at this all day, anything is helpful :) – Dusty Apr 26 '15 at 03:16
  • For the time being I'll have to do some work around stuff... registering some C# functions that return new instances of objects i need. It's gross but I can't figure this one out. – Dusty Apr 26 '15 at 04:04
  • @Dusty Is the @ symbol required for your lua.DoString? Based on the [code](https://github.com/NLua/NLua/blob/master/NLuaTest/AAACodeGenTests.cs) I've [seen](https://github.com/NLua/NLua/blob/master/tests/LuaTests.cs), it's not required. – lloyd Apr 26 '15 at 04:37
  • @lloydm The @ symbol before the string as far as I know just lets you have multi line strings without needing to do "stuff \n" & " stuff" – Dusty Apr 26 '15 at 05:00
  • shouldn't it be `LuaTest.Test()`? – hjpotter92 Apr 26 '15 at 11:22
  • @hjpotter92 I tried that as well – Dusty Apr 26 '15 at 13:46

2 Answers2

2

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

Vinicius Jarina
  • 797
  • 5
  • 16
  • I'm at work right now so I'll give it a try as soon as I get home, I also couldn't get it to work with libraries ex: "system.net" Ideas on that ? – Dusty Apr 28 '15 at 19:01
  • IIRC assembly load is case sensitive, use "System.Net" instead – Vinicius Jarina Apr 28 '15 at 19:12
  • I'm glad the solution was a simple remove the .exe. :) thank you. for other libraries this works: import ('System', 'System.Net.Sockets') – Dusty Apr 28 '15 at 21:15
  • All is good with system libraries, but no luck with custom ones I import. but It's still a great step forward :) – Dusty Apr 28 '15 at 21:30
  • Would you please also fix the homepage of nlua.org? The same 'bug' is in the example presented there, and it induced me to do the same mistake. import('MyAssembly.exe', 'MyNamespace'). Thanks, and keep the good work! – sɐunıɔןɐqɐp Feb 06 '17 at 19:54
0

You're missing test annotations for your class and method.

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

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

        lua.DoString(@"
                        import ('LuaTest.exe', 'LuaTest')
                        test = Test()
                    ");
    }
}
[TestFixture]
public class Test
{
    [Test]
    public Test()
    {
        Console.WriteLine("IT WORKED");
    }
}
lloyd
  • 1,683
  • 2
  • 19
  • 23
  • 1
    Hmmm not sure that's it, I just named it "Test" as an example, it doesn't work for anything I name the classes, assembly or namespace... but I'll try it out tomorrow :) – Dusty Apr 26 '15 at 05:03