Setting up JMonkey so others can code their own extentions to my game

Hey,
So I wanted to have a separate folder, lets call it ‘scripts’ where people can drop in their own classes, the system will load them and the user can select which one they want to run. I don’t want this to be in the source packages folder. Additionally, I want the classes in this folder to compile separately and not go the bin.

I’ve tried adding a folder from right clicking New->Folder but this doesn’t appear in the project.

Any ideas of how I can set this up?

Do it in the files view and add it to the root folder of your project or simply do it via the file manager of the OS. You will have to create the same folder in your “dist” folder later for distribution obviously. Under normal circumstances you should then find the folder as new File("./"); in your app.

Edit: By the way note that your users can only compile java classes when they have a full JDK installed (JRE won’t suffice, it doesn’t have the javac command) and you will have to handle the compilation and supplying the classpath with the jME and your games libraries.

@normen said: Do it in the files view and add it to the root folder of your project or simply do it via the file manager of the OS. You will have to create the same folder in your "dist" folder later for distribution obviously. Under normal circumstances you should then find the folder as new File("./"); in your app.

Edit: By the way note that your users can only compile java classes when they have a full JDK installed (JRE won’t suffice, it doesn’t have the javac command) and you will have to handle the compilation and supplying the classpath with the jME and your games libraries.

If memory serves, the standard JRE bootstrap still has the compiler class. If that’s the case, he could provide his own tool to compile it.

I suggest the javascript engine build into java, you can specify the allowed method called from there.

Then at start just load all scripts in that folder, and let them do whatever is allowed.

@Empire Phoenix said: I suggest the javascript engine build into java, you can specify the allowed method called from there.

Then at start just load all scripts in that folder, and let them do whatever is allowed.

Unless you’re talking Nashorn (JDK 8+), the availability for the JS engine built in is a bit… Unpredictable. I don’t think all platforms have it by default (In fact, my mac was the only one I’d seen it on).

If you’re talking about java code (vs. javascript), it is possible to include portions of tools.jar (from the jdk) and then instantiate the compiler in code, as in:

[java]
JavaCompiler compiler = Class.forName( “com.sun.tools.javac.api.JavacTool”, false, ClassLoader.getSystemClassLoader() ).asSubclass( JavaCompiler.class ).newInstance();
[/java]

The compilation itself isn’t trivial - as it involves setting up different support code (Java file managers, classpaths, annotation processors, tasks, etc) - but it’s not overly difficult either. I use something similar as an extension mechanism in a production application at work.

If you’re interested I could probably put some example code together.

3 Likes
@fabsterpal said: Unless you're talking Nashorn (JDK 8+), the availability for the JS engine built in is a bit... Unpredictable. I don't think all platforms have it by default (In fact, my mac was the only one I'd seen it on).

Rhino JS has been part of java since JDK1.6 and available on all platforms.

1 Like
@jmaasing said: Rhino JS has been part of java since JDK1.6 and available on all platforms.

I see… Didn’t know that.

Cheers guys =).
I just need a seperate folder where classes from that folder will be put in there(or elsewhere) whilst the main part of the project is compiled as norm. Hence javaclass extensions can be dropped into the folder, compiled(doesn’t need to be done at runtime) and loaded. Normens suggestion should do it although aye users will have to setup java etc…but all part of learning for them if they havn’t already =).