I am trying to build a controller using Luaj + java. I have the following java classes
public class Duck {
public void talk() { System.out.println("Duck quacks!"); }
public void walk() { System.out.println("Duck walks!"); }
}
public class Person {
public void talk() { System.out.println("Person talks!"); }
public void walk() { System.out.println("Person walks!"); }
}
and the following lua script for the controller:
onTalk(obj)
obj:talk();
end
onWalk(obj)
obj:walk();
end
I would ideally like to define one controller (written in lua) where I will keep all of the program's logic, and I would like to expose API from that controller to my java code. I was trying to use the following approach:
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine engine = sem.getEngineByExtension(".lua");
ScriptEngineFactory factory = engine.getFactory();
// Script defined above
CompiledScript cs = ((Compilable)engine).compile(MY_LUA_SCRIPT);
SimpleBindings b = new SimpleBindings();
b = newSimpletBindings();
LuaValue onWalkHandler = (LuaValue)b.get("onWalk");
//func.call(LuaValue.valueOf(duck)); // Passing duck object does not work ???
I am not able to pass the object to the LuaValue. How can I pass a java object to the lua script?
PS: In general, when using Java and embedded scripting, do people bundle functions in one script, or is there a separate script for every callback?