Skip to main content
gamedev.net
🔒 Locked

Exposing Java methods to Lua using luaJ.

Started by Michael Wojcik Aug 27, 2011 at 12:30 AM 0 replies 11.1k views
Original Post
Michael Wojcik
Michael Wojcik
Hello,

I want to expose an API of my game engine ( a collection of some of it's methods ) to Lua, using Luaj. I've been following the documentation here on the page for the class LibFunction, in order to create the library that will serve the collection of methods to lua. Here is the current code to my library of methods I want to expose to lua...



...

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction;

public class VoidEngineAPI extends VarArgFunction
{
public VoidEngineAPI()
{

}

@Override
public LuaValue invoke(Varargs args)
{

switch ( opcode )
{
case 0:
{
LuaValue t = tableOf();
this.bind(t, VoidEngineAPI.class, new String[] { "vdTest"}, 1 );
env.set("VoidEngineAPI", t);
return t;
}
case 1: return valueOf("Lua, called a method from the Engine");
default: return error("bad opcode: "+opcode);
}
}
}


I have a Lua script file, named GameStateTransitions.lua, that wishes to use this library class's methods. Here the script's code.


vel = require('VoidEngineAPI')

print( 'vel', vel )
print( 'VoidEngineAPI', VoidEngineAPI )
for k,v in pairs(vel) do
print( 'k,v', k,v )
end


I run the this script elsewhere in my engine like so.


LuaValue _G = JsePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(pathname));


However, I get this error.


module 'VoidEngineAPI' not found: VoidEngineAPI
no field package.preload['VoidEngineAPI']
'VoidEngineAPI.lua': cannot open VoidEngineAPI.lua: No such file or directory
no class 'VoidEngineAPI'
[/quote]

I really appreciate any ideas or help!!

Thanks!
Generalist Game Developer and Cofounder at Voidseer Realms
AngLiKai
AngLiKai
Hi,


Please do not use VarArgFunction, I am not sure why, but I am facing problem with it as well, TwoArgFunction or ThreeArg works okay.
If you want more arguments, you can built your own SevenArgFunction. For example



/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/


import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.LibFunction;

abstract public class SevenArgFunction extends LibFunction {

abstract public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3, LuaValue arg4, LuaValue arg5, LuaValue arg6, LuaValue arg7);

public SevenArgFunction() {
}

public SevenArgFunction(LuaValue env) {
this .env = env;
}

@Override
public final LuaValue call() {
return call(NIL, NIL, NIL, NIL, NIL, NIL, NIL);
}

@Override
public final LuaValue call(LuaValue arg) {
return call(arg, NIL, NIL, NIL, NIL, NIL, NIL);
}

@Override
public LuaValue call(LuaValue arg1, LuaValue arg2) {
return call(arg1, arg2, NIL, NIL, NIL, NIL, NIL);
}

@Override
public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3) {
return call(arg1, arg2, arg3, NIL, NIL, NIL, NIL);
}

public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3, LuaValue arg4) {
return call(arg1, arg2, arg3, arg4, NIL, NIL, NIL);
}

public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3, LuaValue arg4, LuaValue arg5) {
return call(arg1, arg2, arg3, arg4, arg5, NIL, NIL);
}

public LuaValue call(LuaValue arg1, LuaValue arg2, LuaValue arg3, LuaValue arg4, LuaValue arg5, LuaValue arg6) {
return call(arg1, arg2, arg3, arg4, arg5, arg6, NIL);
}

@Override
public Varargs invoke(Varargs varargs) {
return call(varargs.arg1(), varargs.arg(2), varargs.arg(3), varargs.arg(4), varargs.arg(5), varargs.arg(6), varargs.arg(7));
}

}



you can add the function with this code so it become visible in the script :-


LuaValue _G = JsePlatform.standardGlobals();
_G.load(new yourJavaClass()); // or a singleton of it


------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Hello,

I want to expose an API of my game engine ( a collection of some of it's methods ) to Lua, using Luaj. I've been following the documentation here on the page for the class LibFunction, in order to create the library that will serve the collection of methods to lua. Here is the current code to my library of methods I want to expose to lua...



...

import org.luaj.vm2.LuaValue;
import org.luaj.vm2.Varargs;
import org.luaj.vm2.lib.VarArgFunction;

public class VoidEngineAPI extends VarArgFunction
{
public VoidEngineAPI()
{

}

@Override
public LuaValue invoke(Varargs args)
{

switch ( opcode )
{
case 0:
{
LuaValue t = tableOf();
this.bind(t, VoidEngineAPI.class, new String[] { "vdTest"}, 1 );
env.set("VoidEngineAPI", t);
return t;
}
case 1: return valueOf("Lua, called a method from the Engine");
default: return error("bad opcode: "+opcode);
}
}
}


I have a Lua script file, named GameStateTransitions.lua, that wishes to use this library class's methods. Here the script's code.


vel = require('VoidEngineAPI')

print( 'vel', vel )
print( 'VoidEngineAPI', VoidEngineAPI )
for k,v in pairs(vel) do
print( 'k,v', k,v )
end


I run the this script elsewhere in my engine like so.


LuaValue _G = JsePlatform.standardGlobals();
_G.get("dofile").call( LuaValue.valueOf(pathname));


However, I get this error.


module 'VoidEngineAPI' not found: VoidEngineAPI
no field package.preload['VoidEngineAPI']
'VoidEngineAPI.lua': cannot open VoidEngineAPI.lua: No such file or directory
no class 'VoidEngineAPI'


I really appreciate any ideas or help!!

Thanks!
[/quote]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.