0

I'm trying to embed a NLua console script running in a thread in my C# software. My code read the lines from the console and append those until ":quit" is read. It then executes the script. I then catch any exception and print it to the console. Here is an example:

x=4
:quit
print(4)
:quit
4
print(math.sin(x))
:quit
-0.75680249530793
import ('Microsoft.Xna.Framework')
:quit
[string "chunk"]:1: attempt to call global 'import' (a nil value)

It seems I have a problem with the import statement. Here is the C# code:

public void Run()
{
    NLua.Lua lua = new Lua();
    lua["Map"] = map;
    lua.LoadCLRPackage();
    lua.DoString(@"
import ('Game1', 'Game1') 
import ('System.Web')
import ('Microsoft.Xna.Framework;')");


    Quit = false;
    StringBuilder stream = new StringBuilder();
    string line;

    while(!Quit)
    {
        line = Console.ReadLine();
        while (line != ":quit")
        {
            Thread.Sleep(1000);
            stream.AppendLine(line);
            line = Console.ReadLine();
        }


        lock (mutex)
        {
            try
            {
                state.DoString(stream.ToString());
                stream.Clear();
            }

            catch (NLua.Exceptions.LuaException e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

I use Visual Studio 2017, installed NLua for the NuGet Package Manager. Anyone have an idea of why I can't use the import statement?

lucas92
  • 464
  • 2
  • 11
  • You say you caught and logged exceptions but you haven't shown us what they are... Tell us what your problem is. Does it thrown some kind of runtime exception? Does it not compile? Does it run but without errors but not do what you want? Give us full details of what your actual problem is - don't leave us guessing... – Chris Dec 17 '17 at 16:44
  • The exception is in the first code block i.e. [string "chunk"]:1: attempt to call global 'import' (a nil value). The first code block is the Console. – lucas92 Dec 17 '17 at 16:52

0 Answers0