Question

I need to be able to return an (X, Y) position from Java to Lua and I can't figure out how to do it. With the C API you would just push the values onto the stack and then return the number of return values, but that doesn't seem to be how LuaJava does it. Any suggestions?

Was it helpful?

Solution

The problem may be that the first parameter is always the JavaFunction object itself. Let's say you want a function addmult which takes two numbers, and returns their sum and product. The numeric parameters have to be retrieved as indexes 2 and 3, as follows:

JavaFunction addmult = new JavaFunction(L) {                
    @Override
    public int execute() throws LuaException {
        double x = L.toNumber(2);
        double y = L.toNumber(3);
        L.pushNumber(x+y);
        L.pushNumber(x*y);
        return 2;
    }
};
addmult.register("addmult");

Now, you should get a similar result in your code:

print(addmult(3, 5)) --> 8    13
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top