读取嵌套的Lua表,谁的键是System.Double [英] Read nested Lua table who key is a System.Double

查看:75
本文介绍了读取嵌套的Lua表,谁的键是System.Double的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#和LuaInterface尝试读取嵌套表,但是当我尝试打开包含表的键时却得到了一个空的LuaTable.

Using C# and LuaInterface, I am trying to read a nested table, but am getting a null LuaTable when I try to open the key containing the table.

.lua文件:

DB = {
    ["inventory"] = {
        [10001] = {
            ["row"] = 140,
            ["count"] = 20,
        },
        [10021] = {
            ["row"] = 83,
            ["count"] = 3,
        },
        [10075] = {
            ["row"] = 927,
            ["count"] = 15,
        },
    }
}

通过打开以下表格,我可以成功地列出库存中的条目:

I can successfully foreach the entries under inventory, by opening that table with:

LuaTable tbl = lua.GetTable("DB.inventory");
foreach (DictionaryEntry de in tbl)
...

我不能做的是打开一个清单项目,并以相同的方式枚举其条目.这是因为键是System.Double类型吗?失败:

What I cannot do is open an inventory item and enumerate its entries the same way. Is this because the key is a System.Double type? This fails:

LuaTable tbl = lua.GetTable("DB.inventory.10001");
foreach (DictionaryEntry de in tbl)

有一个例外,因为tbl为空.

with an exception, because tbl is null.

有效地,一旦我枚举了键(库存物品),我便想深入到嵌套表中并使用这些内容.如您所见,我无法像执行此操作那样获得对嵌套表的引用.

Effectively, once I enumerate the keys (inventory items), I then want to drill down into the nested table and work with those contents. As you can see, I am not able to get a reference to the nested table the way I am doing it.

推荐答案

LuaInterface似乎仅支持字符串键.从其 Lua.cs ,该函数最终会由您的代码调用:

It appears that LuaInterface only supports string keys. From its Lua.cs, this function is eventually called by your code:

internal object getObject(string[] remainingPath) 
{
        object returnValue=null;
        for(int i=0;i<remainingPath.Length;i++) 
        {
                LuaDLL.lua_pushstring(luaState,remainingPath[i]);
                LuaDLL.lua_gettable(luaState,-2);
                returnValue=translator.getObject(luaState,-1);
                if(returnValue==null) break;    
        }
        return returnValue;    
}

请注意,没有规定不是字符串的键,因为此代码使用您索引的一部分字符串调用lua_pushstring().

Note that there is no provision for keys which aren't strings, because this code calls lua_pushstring() with part of the string that you indexed with.

LuaInterface将点分隔的字符串参数用作其operator[]()的方式是不足的.您发现了一个缺点;如果您尝试查找实际上有一个点的键,则会出现另一个(这是合法的Lua,虽然不是惯用语,但有时您会发现表达键的最自然的方法是不使用某些东西看起来像C标识符).

The way LuaInterface takes a dot-delimited string argument to its operator[]() is deficient. You found one shortcoming; another would appear if you tried to look up a key that actually had a dot in it (which is legal Lua--though not idiomatic, there are times like you've discovered when the most natural way to express a key is not with something that looks like a C identifier).

LuaInterface应该提供的索引方法采用字符串以外的类型.既然没有,您可以像这样重写表:

What LuaInterface should provide is an indexing method taking types other than strings. Since it does not, you can rewrite your table like this:

DB = {
    ["inventory"] = {
        ["10001"] = {
            ["row"] = 140,
            ["count"] = 20,
        },
        ["10021"] = {
            ["row"] = 83,
            ["count"] = 3,
        },
        ["10075"] = {
            ["row"] = 927,
            ["count"] = 15,
        },
    }
}

我认为这会起作用.请注意,尽管Norman Ramsey的建议完全适合于适当的Lua,但它会在LuaInterface中中断,因此您应该像以前一样单独使用点进行索引(尽管对于任何普通的Lua程序员来说,这似乎都是一个错误).

I think this will work. Note that Norman Ramsey's suggestion, while entirely appropriate for proper Lua, will break in LuaInterface, so you should index with dots alone, as you were before (despite that this will look like a bug to any normal Lua programmer).

这篇关于读取嵌套的Lua表,谁的键是System.Double的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆