A method in java returns an Array, and I want to manipulate the information from that array in Lua but it seems that Lua doesn't convert arrays to tables as I'd hoped.
Is there a way to do this?
For example I have this method in Java:
public Node[] getChildren(){
return children.toArray(new Node[children.size()]);
}
When I call this function from Lua I can't do anything with it or have to Instance it, iterate trough it and copy everything to a Lua-Table and then use it. Is there a way to convert the Array to a Lua-Table in Java and then return that?
EDIT: I use LuaJ and the LuaJava library.
From the LuaJava manual, it appears that you have to manipulate the proxy objects returned by Java using their Java methods (using Lua's colon syntax for object-oriented calls, ie
my_proxy_array_list:get(5)
).It doesn't describe any built-in translation of Arrays to tables, so if you need to construct a table from a Java Array (say, because you want to run a function from Lua's
table
library on it), you'll have to iterate over the Array (using your knowledge of Java to do that) and put the value of each Array index into the corresponding table index.If, however, you just need something that works like a Lua table, you can make something in Lua with a metatable with functions that translate
__index
and__newindex
into the appropriate Java methods (probablyget
andset
), like this:With the above you can call
wrap_lj_proxy
with your ArrayList and get back an object you can index with the index operator: