简体   繁体   English

如何用c#将表传递给lua

[英]How to pass a table to lua with c#

How to pass a table to lua with c# 如何用c#将表传递给lua

I'm using the LuaInterface,this is my c# code 我正在使用LuaInterface,这是我的c#代码

  using System;
  using System.IO;
  using System.Text;
  using LuaInterface;

  namespace GetLuaTable
  {
      class Program
      {
          static void Main(string[] args)
          {

              Lua netLua = new Lua();

              CShaprFunction cShapr = new CShaprFunction();
              netLua.RegisterFunction("CShaprConsoleLine", cShapr, cShapr.GetType().GetMethod("CShaprConsoleLine"));
              netLua.RegisterFunction("CSharpGetTableFromStr", cShapr, cShapr.GetType().GetMethod("CSharpGetTableFromStr"));
              netLua.DoFile("MyLua.lua");
              netLua.GetFunction("main").Call();
              Console.ReadKey();
          }
      }
      class CShaprFunction
      {
          public void CShaprConsoleLine(object obj)
          {
              Console.WriteLine(obj);
          }
          public LuaTable CSharpGetTableFromStr(string name)
          {
              Lua lua = new Lua();
              lua.DoString("a={\"test\"}");
              LuaTable tab = lua.GetTable(name);
              return tab;
          }
      } 
  }

this is lua code: 这是lua代码:

  function main()
    CShaprConsoleLine("Start")
    local tmptable = CSharpGetTableFromStr("a")
    CShaprConsoleLine(type(tmptable))  
    CShaprConsoleLine("end")
  end

But I get the result,the tmptable is function type not table type. 但是我得到了结果,tmptable是函数类型而不是表类型。 like this: 像这样:

Start
function
end

so how can I pass a table to lua? 那么如何将表传递给lua?

You need to use the same Lua object in both Program and CShaprFunction for this to work right because it is creating the Lua table in the lua enviornment and you can't directly move a Lua table to a different environment. 您需要在Program和CShaprFunction中使用相同的Lua对象才能正常工作,因为它在lua环境中创建了Lua表,并且您无法直接将Lua表移动到其他环境。

Here is an example that produces: 这是一个产生的例子:

Start
table
end

I used NLua, LuaInterface's successor that is still being updated because I was having trouble with LuaInterface but it should work the same in LuaInterface. 我使用了NLua,LuaInterface的后续版本仍然在更新,因为我在使用LuaInterface时遇到了麻烦,但它在LuaInterface中应该是一样的。

using System;
using System.IO;
using System.Text;
using NLua;

namespace GetLuaTable
{
    class Program
    {
        public static Lua netLua;

        static void Main(string[] args)
        {

            netLua = new Lua();

            CShaprFunction cShapr = new CShaprFunction();
            netLua.RegisterFunction("CShaprConsoleLine", cShapr, cShapr.GetType().GetMethod("CShaprConsoleLine"));
            netLua.RegisterFunction("CSharpGetTableFromStr", cShapr, cShapr.GetType().GetMethod("CSharpGetTableFromStr"));
            netLua.DoString(@"
            function main()
                CShaprConsoleLine(""Start"")
                local tmptable = CSharpGetTableFromStr(""a"")
                CShaprConsoleLine(type(tmptable))  
                CShaprConsoleLine(""end"")
              end
            ");
            netLua.GetFunction("main").Call();
            Console.ReadKey();
        }
    }
    class CShaprFunction
    {
        public void CShaprConsoleLine(object obj)
        {
            Console.WriteLine(obj);
        }
        public LuaTable CSharpGetTableFromStr(string name)
        {
            var lua = Program.netLua;
            lua.DoString("a={\"test\"}");
            LuaTable tab = lua.GetTable(name);
            return tab;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM