Hi,
what I'm trying to do:
I'm working on a game. Certain functionality of that game is scripted. Let's
look at one example: NPCs.
I' ve got an interface INonPlayerCharacter and a class
NonPlayerCharacterScriptProxy. The later exposes certain methods for the
script. The script should define a JS object implementing the
INonPlayerCharacter interface. Right now, all these scripts look like that:
function Suzie()
{
this.meetPlayer= meetPlayer;
}
function meetPlayer(player)
{
// do something
}
new Packages.com.somenamespace.INonPlayerCharacterScript(new Suzie());
Note the ugly call "new Package...".
I want to get rid of this call. That's fairly simple, because when reading
in the script, I just add the line on-the-fly.
But I also want to change the constructor of Suzie to look like this:
function Suzie(npcProxy)
{
this.npcProxy= npcProxy;
this.meetPlayer= meetPlayer;
}
And to construct Suzie instances from within Java passing
NonPlayerCharacterScriptProxys as parameters.
How would I go about this?
What I've tried is this:
class MyContext extends Context
{
//... all kinds of stuff
public<T> T createJavascriptObject(String javaScriptClassName, Class<?>
javaClassToBeCastedTo, Object...args) throws Throwable
{
final Scriptable jsObject = this.newObject(this.rootScope,
javaScriptClassName, args);
final NativeJavaObject adapter = (NativeJavaObject)
JavaAdapter.createAdapterWrapper(jsObject, javaClassToBeCastedTo);
final Object javaObject= adapter.unwrap();
final T result = (T) javaObject;
return result;
}
}
I'd like to call this then like:
INonPlayerCharacterScript script= context.createJavascriptObject("Suzie",
INonPlayerCharacterScript.class, npcScriptProxy);
Unfortunately, this creates an invalid adapter. So probably I went totally
wrong with guessing how to do this.
Any suggestions?
Thanks a lot,
Max Hajek
Vienna, Austria
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino