Search code examples
c#lualuainterface

LuaInterface - a function which will return a LuaTable value


Has anyone an idea, how to write a C# function which will return a LuaTable value (for example {1 = "example1", 2 = 234, "foo" = "Foo Example"}? All types I've tested are returning LuaUserData values which are non pair/ipairable. Thanks in advance.

--update-- The closest type to luaTable is in my opinion ListDictionary:

        [LuaFunc(Name = "table", Desc = "returns test LuaTable", Params = new string[] { })]
    public System.Collections.Specialized.ListDictionary table()
    {
        var k = new System.Collections.Specialized.ListDictionary(){
            {"1",1},
            {2,"2"}
        };

        return k;
    }

But it's still recognized in Lua as LuaUserData and can not be pair/ipaired


Solution

  • There a two possible solutions for this problem.

    The first is, to let Lua return the table:

    LuaTable lt = (LuaTable) lua.DoString("return {1 = "example1", 2 = 234, "foo" = "Foo Example"}")[0];
    

    The second possibility is to create a new table

    LuaTable lt = lua.NewTable("ThisTable")
    lt["1"] = "example1"
    lt["2"] = 234
    lt["foo"] = "Foo Example"
    

    You could access the second table from Lua through

    ThisTable[1] = ThisTable["foo"]