How to read from Lua arrays into c# using NLua?

624 Views Asked by At

I have seen a few other posts, but they are the opposite of what I want to do. I simply have an array of ints in my lua file, and I want to know how I change that into a c# array. Using lua ["ints[1]"] throws an exception.

I think this would be very useful to know for many people, as it could be used for tilemaps, or many things really.

1

There are 1 best solutions below

0
On

The array of integers you have on the Lua side is just a Lua table and on the C# side it's a LuaTable type.

Do this:

lua.DoString(@"
    my_array = { 1, 2, 3, 4 }
");

LuaTable myArray = (LuaTable) lua["my_array"];

foreach(var val in myArray.Values)
{
    Console.WriteLine(val);
}

Result:

enter image description here