1

I would like to iterate through something like this:

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

In Lua, whilst using MoonSharp. From the docs, it appears that MoonSharp handles automatic conversion to a table for IDictionary types?

However, trying to do something like this ...

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

... is giving me:

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

Is there something I'm missing as to why MoonSharp isn't converting my dictionary into a table?

Thanks!

Bab
  • 21
  • 2
  • Seems relevant or duplicate https://stackoverflow.com/questions/45218769/moonsharp-pairs-raises-exception-bad-argument-1-to-next-table-expected – dropoutcoder Jan 07 '18 at 17:24

1 Answers1

1

I still don't know why MoonSharp isn't automatically handling this - at first I assumed it may have been a problem with my 'Entity' type, but I've switched that out and tested around it, and still nothing.

My workaround for now is to handle the conversion inside my .lua files.

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
Bab
  • 21
  • 2