I wouldn’t call myself a Java programmer by any stretch, and I’m struggling immensely to understand this one…
Within a native plugin, creating a basic class to return a string or integer etc is easy enough:
package plugin.somePlugin; import com.naef.jnlua.LuaState; public class returnSomething implements com.naef.jnlua.NamedJavaFunction { // This reports a class name back to Lua during the initiation phase. @Override public String getName() { return "returnSomething"; } // This is what actually gets invoked by the Lua call @Override public int invoke(final LuaState luaState) { luaState.pushString("Something"); return 1; } }
I follow enough of the underlying mechanics to understand that pushString is submitting to some kind of an event queue that the Lua function making the call can interpret as a response, and that you can push multiple values back this way within the same routine as that same response. I don’t fully understand how, but I get that it happens.
Now, what I’m trying to get my head around is how to return table data. More precisely, a 3D assoc array.of data.
I’ve crawled an endless number of examples and forum posts, and as far as I understand it you can create Lua tables directly through luaState calls, passing key->val pairs in reverse order via pushString/pushBoolean/etc and setField calls. So something like this:
package plugin.somePlugin; import com.naef.jnlua.LuaState; public class returnSomething implements com.naef.jnlua.NamedJavaFunction { // This reports a class name back to Lua during the initiation phase. @Override public String getName() { return "returnSomething"; } // This is what actually gets invoked by the Lua call @Override public int invoke(final LuaState luaState) { luaState.newTable(); Integer tableIndex = luaState.getTop(); luaState.pushString("Some value"); luaState.setField(tableIndex, "some key"); luaState.pushString("Another value"); luaState.setField(tableIndex, "another key"); luaState.pushString("Some other value"); luaState.setField(tableIndex, "some other key"); tableIndex = luaState.getTop(); luaState.pushString("Some value on row 2"); luaState.setField(tableIndex, "some key"); luaState.pushString("Another value on row 2"); luaState.setField(tableIndex, "another key"); luaState.pushString("Some other value on row 2"); luaState.setField(tableIndex, "some other key"); return 1; } }
But this doesn’t seem to be working…? I’m guessing I’ve totally misunderstood what getTop() does, or I’m missing some kind of a finalise call at the end to actually push the queue back. Either way, currently when calling this function from within Lua, the app just locks up. No errors, it just hangs.
As always, all help is very much appreciated please.