Goal: allow end users to write jython scripts that will create AI objects [for controlling fleets of spaceships in a game] in Java.
I want the user to be able to write all the logic for the AI in a .py file then convert that code into a java object. I would like to do this without using Java reflection and without running the user's code through an interpreter every time it is needed. The whole point of this is to make the code run as fast as possible, because the AI logic will have to run every game loop.
I've seen Jython's __toJava__ method, but I don't really understand how to use it or if it is what I need.
I would greatly appreciate any insight into a good solution to my problem, or knowledge about __toJava__. I am committed to both Java and Jython, so suggesting alternatives is not productive :)
-
1Any reason you can't use jythonc? If "go fast" is a goal I'm not sure I'd allow scripting at all, but that's a separate issue, and I don't know how "fast" you really need it to be.Dave Newton– Dave Newton2013年01月07日 22:52:59 +00:00Commented Jan 7, 2013 at 22:52
-
1I can't use jythonc because I don't know how (links are good!), and I read somewhere that it was deprecated(?) With a good solution to my problem, scripting shouldn't be a speed issue since once I create my java object from the script, I can reuse that instead of going through the script again.TLive– TLive2013年01月07日 23:01:40 +00:00Commented Jan 7, 2013 at 23:01
2 Answers 2
'jythonc' is no longer supported. You have to compile using jython:
jython
>>> import compileall
>>> compileall.compile_dir('directory/', force=True)
for a single file:
>>> import py_compile
>>> py_compile.compile('Building.py')
Comments
Jythonc is deprecated, but here is the old version with jythonc anyway. Compile with something like this:
jythonc --core --jar foo.jar foo.py
Another (untested) solution could be to write save your class as foo.py, open a jython shell in the same folder and type
import foo
This will automatically create a foo$py.class in that folder.
Although Jython is a nice language, the lack of good packaging tools made me go for Clojure which can be easily packaged for java interoperation with Leiningen. And since you are going to write AI, using a lisp might not be a bad idea.