Setting up Java to run Python scripts
Since playing around with Python scripts in Whalechat during Comp 121 (Jason presented it one class), I've been fascinated with the idea of setting up scripting for a game. However, the topic always eluded me. There was always the question in my mind of you would go about getting two totally different programming languages to interact with each other. Feeling that it was beyond me, I put the topic to the side for awhile.
I'd participated in forums where some game developers had set up Lua scripting in their C++ games, so I knew it wasn't unreachable. In addition, I had the source code for Whalechat available to learn from (thanks Jason) which gave me confidence that I'd be able to figure it out. Just recently, with the final for Comp 126 quickly approaching, I decided to give it a go. It turns out that it's not as difficult as I initially thought - though it did take some patience and willingness to troubleshoot a lot of initial errors.
The ability to read Python scripts basically comes from a Java class called PythonInterpreter which is part of a package called Jython. Jython can be obtained through NetBeans/Eclipse or downloaded and installed individually (adding it as a plugin in Netbeans is the easiest route in my opinion). Jython essentially provides a framework for interpreting the Python language in Java bytecode, rather than a different type of intermediate bytecode. This allows the Java Virtual Machine to read the instructions from the Python scripts as it would Java instructions. It also allows Python to make use of Java libraries.
I'd like to write a tutorial on how to set up a simple PythonInterpreter and write a little script in Python. The language itself has some notable differences from Java. First of all, it uses no semicolons, or brackets, and instead just uses indentations. Also, Python doesn't require you to designate the type of a variable/field (it's not strongly-typed).
eg. a typical Python function might look like this:
x = 1+1