【问题标题】:C# LuaInterface How to get System.String from a Lua stringC# LuaInterface 如何从 Lua 字符串中获取 System.String
【发布时间】:2013-09-14 22:08:56
【问题描述】:

我最近开始使用 LuaInterface 以便能够从 C# 程序运行 Lua 脚本。我写了一个返回字符串的测试脚本:

teststring = "PRIVMSG #```` : SUCCESS!"
return testring

然后我尝试将其添加到 C# Queue<string> 中:

sendQueue.Enqueue(lua.DoFile(script).ToString());

但是,这不会返回字符串。相反,它返回 System.Object[]。如何让它返回 System.String?

【问题讨论】:

  • 根据stackoverflow.com/questions/18197349/… 此处的评论,您可能只想要 lua 返回的对象数组中的第一项。所以,也许你可以试试sendQueue.Enqueue(lua.DoFile(script)[0].ToString());。 - 注意[0],它访问从零开始的数组的第一项。
  • @Corak 我现在收到“System.NullReferenceException:对象引用未设置为对象的实例。”在入队行。不同的错误,所以这可能是进步!
  • 好吧,看起来lua.DoFile(script)返回null...在链接线程中,他们使用lua.DoString,可能返回一个有效的字符串数组(或者一个以字符串为第一个的有效对象数组对象)。
  • @Corak 现在我得到 "LuaInterface.LuaException: [string "chunk"]:1: '=' expected near '\'" 除了脚本不包含 '\' 和我不了解 Lua,无法理解它在问什么。
  • 很抱歉没有提供更多帮助,我自己没有使用过 lua。一个简短的搜索返回了这个dreamincode.net/forums/topic/…,它不处理返回值,但也许像lua.DoFile(script); sendQueue.Enqueue((string)lua["teststring"]); 这样的东西对你有用。

标签: c# luainterface


【解决方案1】:

我的一个朋友偶然发现了答案。为了让它产生一个实际的字符串,您应该执行以下操作:

var output = lua.DoFile(script).First().ToString();
Console.WriteLine(output);
sendQueue.Enqueue(output);

我不明白为什么这种方法有效,只是它确实有效。

【讨论】:

    猜你喜欢
    • 2021-12-30
    • 2011-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    相关资源
    最近更新 更多