NLua console script in C# application : import statement is not working

392 views Asked by At

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?

0

There are 0 answers