【问题标题】:Looping a C# dictionary in Lua with MoonSharp使用 MoonSharp 在 Lua 中循环 C# 字典
【发布时间】:2018-06-16 17:58:25
【问题描述】:

我想遍历这样的东西:

public readonly IDictionary<int, Entity> Entities = new Dictionary<int, Entity>();

在 Lua 中,同时使用 MoonSharp。从文档来看,MoonSharp 似乎可以自动转换为 IDictionary 类型的表格?

但是,尝试做这样的事情......

for k,v in pairs(World.Entities) do
  -- something
end

...正在给我:

ScriptRuntimeException: bad argument #1 to 'next' (table expected, got userdata)

关于为什么 MoonSharp 没有将我的字典转换为表格,我是否遗漏了什么?

谢谢!

【问题讨论】:

标签: c# lua moonsharp


【解决方案1】:

我仍然不知道为什么 MoonSharp 没有自动处理这个问题 - 起初我认为这可能是我的 'Entity' 类型的问题,但我已经把它换掉并围绕它进行了测试,但仍然没有.

我现在的解决方法是在我的 .lua 文件中处理转换。

function convert.dictTable(clr)
  local table = {}
  local enumerator = clr:GetEnumerator()

  while enumerator:MoveNext() do
    table[enumerator.Current.Key] = enumerator.Current.Value
  end

  return table
end

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 2017-07-21
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2018-12-30
    • 2011-01-25
    • 2021-05-23
    相关资源
    最近更新 更多