简体   繁体   中英

How to pass a table to lua with c#

How to pass a table to lua with c#

I'm using the LuaInterface,this is my c# code

  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:

  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. like this:

Start
function
end

so how can I pass a table to 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.

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.

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;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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