Question

So, I'm trying to implement Lua as a scripting language in C#. It's simple enough, really, if you use it as part of a console application. But I want to be able to create a game console within XNA. The problem is that Lua.DoString() seems to return a console line, rather than a string - which, as I said, is fine for console applications, but I want to add it to an output buffer as a string so I can display it in-game, rather than in a console window. I apologize if this is confusing - hopefully the code will clear it up.

using System;
using System.Collections.Generic;
using LuaInterface;

namespace LuaConsoleLibrary
{
    public class LuaConsole
    {
        private Lua lua;

        private bool isRunning = true;
        private string input;
        private List<string> outputBuffer;

        public bool IsRunning
        {
            get { return isRunning; }
        }

        public LuaConsole()
        {
            this.lua = lua;
        }

        public void Run()
        {
            int i = 0;
            while(isRunning)
            {
                outputBuffer.Add("> ");

                input = Console.ReadLine();
                outputBuffer.Add("");

                try
                {
                    lua.DoString(input);
                }
                catch(Exception e)
                {
                    outputBuffer.Add(e.Message);
                }
                finally
                {
                    outputBuffer.Add("");
                }
            }
        }
    }
}

Basically, I'm confused as to why lua.DoString(input); should work as opposed to having to use Console.WriteLine(lua.DoString(input));. I've even tried outputBuffer.Add(lua.DoString(input).ToString());, which, being that ToString is valid there, should work in theory. Instead it performs lua.DoString and then throws an exception - "Object reference not set to an instance of an object". Unless I'm mistaken, the object in question is Lua, which makes lua a valid object reference. Anyway, seeing as these two solutions don't work, I was wondering if anyone knows enough about LuaInterface to suggest an alternative way to get the same result. I essentially want something to the effect of outputBuffer.Add(lua.DoString(input));. By the way, the Lua command I've been using to test this is print("test") - maybe print is somehow returning a console line instead of a string, but I can't think of another way to return a string from Lua.

I apologize if my problem is rambling or poorly explained - I tried to be as concise as possible, but it's kind of a niche issue and I don't know how else to explain it, really. Oh, and I did try Google, and I browsed all 65 or so LuaInterface questions on Stack Overflow, so I'm 95% sure I've done my due diligence before asking this question. LuaInterface is just not that well-known of a tool, I guess.

Was it helpful?

Solution

Object reference not set to instance of an object is C#'s way of saying "something is null and it shouldn't be."

In this case, most likely DoString is returning null, so null.ToString() is raising the exception. You could try outputBuffer.Add(Convert.ToString(lua.DoString(input)[0])) if you want to convert to string without throwing on nulls.

I haven't used either LuaInterface or NLua, but I'd guess that to return a string you'd actually have to execute return "test" as your Lua command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top