返回一个带有LuaInterface的字符串 [英] Return a String with LuaInterface
问题描述
所以,我试图在C#中将Lua实现为脚本语言。它很简单,真的,如果你使用它作为控制台应用程序的一部分。但我想要能够在XNA中创建一个游戏控制台。问题是Lua.DoString()似乎返回一个控制台行,而不是一个字符串 - 如我所说,这是很好的控制台应用程序,但我想将它作为字符串添加到输出缓冲区,所以我可以显示它在游戏中,而不是在控制台窗口。如果这很困惑,我道歉,希望代码可以清除它。
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("");
}
}
}
}
}
$ b b
基本上,我很困惑为什么 lua.DoString(input);
应该工作,而不是使用 Console.WriteLine (lua.DoString(input));
。我甚至尝试了 outputBuffer.Add(lua.DoString(input).ToString());
,这是ToString是有效的,应该在理论上工作。相反,它执行 lua.DoString
,然后抛出异常 - 对象引用未设置为对象的实例。除非我错了,对象是 Lua
,这使得 lua
是有效的对象引用。无论如何,看到这两个解决方案不工作,我想知道是否有人知道足够LuaInterface建议一个替代方法,以获得相同的结果。我本质上想要的效果 outputBuffer.Add(lua.DoString(input));
。顺便说一下,我用来测试这个Lua命令是 print(test)
- 也许 print
以某种方式返回控制台行而不是字符串,但我不能想到从Lua返回字符串的另一种方法。
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.
如果我的问题是rambling,我道歉或者解释不好 - 我试图尽可能简洁,但它是一个利基问题,我不知道如何解释它,真的。哦,我试过谷歌,我浏览了所有65左右的关于Stack Overflow的LuaInterface问题,所以我95%肯定我在提出这个问题之前做了我的尽职调查。 LuaInterface只是不知道一个工具,我猜。
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.
推荐答案
到对象的实例
是C#的说法某事是null,它不应该是。
Object reference not set to instance of an object
is C#'s way of saying "something is null and it shouldn't be."
在这种情况下,很可能DoString返回null,因此null.ToString()引发异常。你可以尝试 outputBuffer.Add(Convert.ToString(lua.DoString(input)[0]))
如果你想转换为字符串不抛出null。
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.
我没有使用LuaInterface或者NLua,但我想要返回一个字符串,你实际上必须执行 return test
作为您的Lua命令。
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.
这篇关于返回一个带有LuaInterface的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!