3

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

Charlie Hopperson
  • 415
  • 1
  • 9
  • 23
  • 1
    Please provide a sample of the source for how you are currently constructing the table and its values on the C# side. What functions are you using and how are you using the Lua virtual stack? – Richard Chambers Jan 13 '13 at 05:05
  • You may also find this stack overflow article helpful. http://stackoverflow.com/questions/10941563/passing-c-sharp-collection-to-back-to-lua – Richard Chambers Jan 13 '13 at 14:18
  • I've already found this way, but I'm looking for c# solution. – Charlie Hopperson Jan 13 '13 at 21:33
  • 1
    Not sure what you mean by a "c# solution" since the stackoverflow article is C#. The problem is that the Lua engine is written in the C programming language, so you have to have a C# to C interface to do marshaling and conversion between the two. With C or C++ it is a matter of directly using the functions provided in the Lua engine which allow you to build tables and such on the Lua virtual stack and then call the function that gives the built up tables to the Lua engine. – Richard Chambers Jan 14 '13 at 00:27

3 Answers3

3

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"]
user1829325
  • 108
  • 6
0

user1829325 has provided excellent approaches, though they don't compile without modifications.
lua.DoString returns an array, lua.NewTable does not return anything.

But it lead me to the following solution, which runs perfectly, so +1 anyway!

public LuaTable CreateTable()
{
    return (LuaTable)lua.DoString("return {}")[0];
}

A C# function returning a table which should be called from Lua could look like this:

LuaTable newtable = CreateTable();
table["lala"] = 5;
return table;

I also wrote a marshall function, which converts a Dictionary to a LuaTable using my function above:

private LuaTable MarshalDictionaryToTable<A,B>(Dictionary<A, B> dict)
{
    LuaTable table = runner.CreateTable();
    foreach (KeyValuePair<A, B> kv in dict)
        table[kv.Key] = kv.Value;
    return table;
}
JCH2k
  • 3,361
  • 32
  • 25
0

JCH2k is right. NewTable does not have a return type!

Using JCH2k logic I was able to make this function to convert a c# Point to LuaTable.

public LuaTable ConvertPointToTable(Point point)
{
return (LuaTable)lua.DoString("return {" + point.X + ", " + point.Y + "}")[0];
}

To use once return in Lua.

local x = val[1]
local y = val[2]
LastEnd
  • 71
  • 3