Advertisement

Using Java as a scripting language?

Started by December 06, 2004 12:57 AM
1 comment, last by Ostsol 20 years, 2 months ago
Has anyone explored the idea of using Java as a scripting language? My experience with scripting languages started with UnrealScript, and I really enjoyed the language (it was very similar to Java/C++) and its ability to overload native C++ classes. I've done a bit of searching and it appears that it is possible to use Java in a similar manner, but I'd just like to hear opinions on the matter before I seriously consider it. Some background: My own attempt at writing a scripting language has hit a bit of a roadblock as I've found it difficult to decide how to proceed to the next step of allowing it to interacting with native C++ code. My intention was something similar to Java or UnrealScipt, run on a virtual machine and able to extend C++ classes. That ability is critical because I want to have an object oriented way of writing game logic and content without having to resort to C++ and DLLs.
-Ostsol
Java can't extend C++ classes out of the box. You have to write glue code in C++ using JNI. There are games that have used Java as a scripting language (Vampire: the Masquerade and Chrome off the top of my head, but more are out there).

Personally though, I don't think it's worth the effort unless you are distributing on CD. The user will be required to have a JRE installed, which means you either need to bundle it with your game or require them to download it if they don't have it installed already. That's an extra ~14mb or so. In that case, you might as well just code the whole game in Java using JOGL, LWJGL, or one of the several 3D engines that are being built on top of them. Then you can knock up a dynamic class loading system and expose certain parts of the game API for players to script. They'd be able to directly extend and replace the classes you expose. Or you could use a Java-based scripting language such as pnuts, Beanshell, or Jython. I like the dynamic class loading myself, as it's quite easy to implementa and very powerful.

Otherwise, Python is much smaller to distribute with your game. Like Java, you can extend C++ classes with glue code. I think it's also easier to pick up for non-programmers, which may be a boon for your players if you are going to allow them to script the game.

Lua can also extend C++ classes (again, with glue code).

If you do decide to use Java as a scripting language you need to research JNI (Java Native Interface).
Advertisement
Hmm. . . Yeah, the issue of the JRE crossed my mind. Coding the entire thing in Java instead is very tempting -- except that I've already coded a significant amount in C++.

The nature of compiled C++ code makes it a bit annoying to extend with purely content-specific code in a DLL. Even if only the base classes interact directly with the engine, one still has to link the derived content classes to them. Java certainly does take away the extra work of linking.

One other reason why I chose Java is that it's a very strict language. Lua and Python are rather loose and Lua's tables, while very powerful indeed, are extremely general and feel quite low-level. Python's classes look a bit more comfortable -- and Boost should make integration easier.

Well, if Java is better off as being used for the entire project, it looks like Python is the way I'll go. Thanks for your opinion.
-Ostsol

This topic is closed to new replies.

Advertisement