3

in our project I try to call a java method from lua which has a variable number of arguments. So the code of the java-method looks like:

public static void addEvent( String luaFile, String function, 
                             int milliseconds, Object...args )
{
    events.add( new TimerEvent( luaFile, function, milliseconds, args ) );
}

I want to call this method from a lua file with the line:

mgr:addEvent( "luaFile.lua", "doSomething", 3000, var )

But using Luajava I allways get an error:

PANIC: unprotected error in call to Lua API (Invalid method call. No such method.)

Even removing the "var"-argument or adding some more arguments does not work.

So maybe anyone of you has ever used a java-method with variable arguments in a Lua-file and can give me a hint how I can solve this problem. I just don't want to use too many lines of code in the Lua-file as I would need for creating an ArrayList and adding arguments and passing this ArrayList to the Java-method. So maybe there is also a simple way to create an Array which I can pass as Array to Java. So a solution must not necessarily use vargs, but I thought it would be an easy way.

Thanks for any help in advance

FredFS
  • 33
  • 3
  • I don't know lua but a Java varargs method can also be called with an array for example `String.format` can be called with `String.format(formatString, new Object[] { arg1, arg2, arg3 }`. Does that help? – Miserable Variable Mar 06 '12 at 22:42

3 Answers3

1

A varargs parameter (eg Object... args) is really of type Object[].

LUA (probably) isn't capable of recognising varargs and dynamically creating the array, so try this:

mgr:addEvent( "luaFile.lua", "doSomething", 3000, {var})
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Well, yes, I tried this. But it seems that LuaJava is using tables not as arrays. Hence your solution is not working, because {var} is sort of an Object. So what I am looking for is a solution to create an array with Lua which is also used as an array in Java. – FredFS Mar 06 '12 at 23:26
1

Unfortunately, Java arrays are not currently supported by LuaJava. It does not allow the construction of new Java arrays and does not support operations with arrays (getting and setting values). Therefore it is unable suport the Object... args syntax.

You could work around this by using specialized methods which take 0, 1, 2, 3 arguments (I do not think you would need more than 3). Then you would add a Lua vararg function which calls the appropriate function. An example for 3-argument call:

public static void addEvent3( String luaFile, String function, 
                             int milliseconds, Object arg1, Object arg2, Object arg3 )
{
    events.add(new TimerEvent(luaFile, function, milliseconds, new Object[] {arg1, arg2, arg3}));
}
Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
  • Thank you for your answer. No I know, that I have to find a work around. What you suggested ist probably the best way and I will do it this way. – FredFS Mar 23 '12 at 16:05
0
    public static Object[] ConvertTableToArray(LuaTable table, Class toClass) {
    var array = Array.newInstance(toClass, table.length());
    for (int i = 0; i < table.length(); i++) {
        var v = table.get(i + 1).touserdata(toClass);
        Array.set(array, i, v);
    }
    return (Object[]) array;
}

Call this in lua. Make sure the luatable only contains object that is toClass type. Example:

JavaClass:ConvertTableToArray({obj1,obj2,obj3},obj1:getClass())
etwxr9
  • 11
  • 2
  • Perhaps a brief explanation would make the answer more informative. – David Lee Jul 11 '21 at 05:18
  • It convert lua table to java array, which can be used as varargus in java method. But I made a mistake. I thought it was luaj, not luajava. I'm not sure if it works for luajava. – etwxr9 Jul 12 '21 at 01:15