Java as a scripting language

Started by
8 comments, last by Jimfing 19 years ago
Hi, i'm just starting into the world of scripting for my game engine, and have been looking at LUA. It looks damn good, but my only concern is the CPU usage, since it's interpreted. This may be a silly question, but is it possible to use Java as a scipting language? It's not as efficient as C++, but it gets compiled into byte code for the VM right? (i'm a noob with Java so correct me if I'm wrong). My engine is written in C++. Thanks for any replies. Jim.
Advertisement
Lua is designed for speed, and has been used in several commercial game projects. It may well be possible to embed Java but the overhead of the VM probably cancels out any performance benefits you might get.

Quotes from http://www.lua.org/uses.html:

"Lua's speed and footprint are awesome."
"We chose Lua as scripting language to ClanLib because it is so fast."
"The reasons for choosing Lua as a scripting language were mainly its speed and small memory footprint"
"MPC initially chose Lua for its reputation for speed and efficiency as well as for the great simplicity of its API. It has subsequently proved itself through heavy use by a team of 16 or so CG artists and across a renderfarm of over 500 machines."

Seriously, if Lua proves to be a bottleneck in your program, then (a) I would be very very surprised, and (b) you can just push some of the functionality out to your C++ code.
Lua can be compiled into an intermediary language as well. This will make it execute a bit faster. I'm not sure about java.
I would think that using Java would be a royal pain since as far as I know there is no existing library for embeding Java completely in a program. So you'd have to write your own mini JVM. Also, the garbage collection would be killer on performance since you never know when it will kick in.

If Lua's not your thing, there's plenty of other scripting languages available for games. Squirrel, I believe was used in FarCry. I think I've heard of people using Python. I'm sure other people will be able to think of more if none of those fit the bill.
Ah thanks guys - you've been very helpfull.

OK - I'll go with Lua - I'm convinced!

:)
You could impliment a managed .NET interface for incorporating JScript.NET via VSA ( Visual studio for applications ). This allows for compiled scripts which are pretty fast and also have access to a lot of the .NET framework as well as code level security features etc.

Using the VSA also gives you the option to use VB.NET as a scripting language

I believe their is a demo of how to do this in the DirectX9 Summer 2004 SDK, their are also a few articles at msdn.microsoft.com

Cheers


Mark
AngelScript works for me and is apparently faster than lua. Its also alot easier (IMHO) to get working. Lua is definatly a good choice though, just thought I'd give you another option if your really worried about speed.
You *can* embed Java in your application, by embedding a Java virtual machine.

You *can* use JNI calls to call into Java code to do specific things.

You *can* make a system which invokes the Java compiler to build code on the fly, even from your own program, and reload the classes using a new classloader - so enabling you to change stuff without reloading the program.

But it would be a lot of effort; you'll need to bundle a JVM if there isn't one already (or it's the wrong version).

You'll need a considerably amount of messing around to mangle the interfaces to your objects / methods for JNI. You'll need JNI wrappers for the methods you want to call.

True, it's possible that you might gain a small bit of performance if your "scripts" are really intensive, but if they aren't, you could lose more performance in the overhead of moving everything between C and Java.

It'll also use up quite a lot of memory at runtime and be relatively slow to start up.

If your scripts are so intensive you worry about their runtime performance, consider rewriting those parts of scripts in C anyway - you can probably do just as well. And you'll detach yourself from the JVM.

I believe some games have actually used embedded JVMs for scripting / addons. It does work, it's just not especially straightforward.

BOTTOM LINE:

- Decide whether you really want Java, ignoring performance issues
- Decide whether it's more convenient to use something less performant, but easier to embed and with a smaller footprint (like lua)
- Profile your application to decide whether performance matters
- It probably doesn't.

Mark
I was just going to second the remarks of Markr:

Java as a scripting language in games is very viable. For instance:
Q2Java
Chrome
Vampire: Masquerade
Vampire: post Mortem*
Gamasutra Java Embedding
Sun's JNI book (download - free)
"Scott's site (FuBI reference"

*I still think that Storyteller mode is one of the most underrated innovations I have seen in a long time. It would be nice to see that feature get explored further in other projects...


Lua is very cool too though - no doubt about it, and 5.x I think is a great advancement in that it is not stack but register based. This is a similar technique to what Stackless Python (Eve Online) is using. For bytecode (imho), this makes a lot of sense as you reduce nonsense opcodes that constantly push and pop arguments on a stackframe. There's a good article on the LUA 5.0 spec and the optimizations that provides.

I have not used AngelScript but I hear good things about it and I think it uses the FuBI technique for binding. It would be cool if Java (actually JNI) would provide a 'FuBI' type interface (although I haven't extensively google'd - so there may be one) for Java. The only problem here is that would violate spec and could cause other issues in the end.

I think one thing I really like about Java that the others do not offer is JIT.. so not only can you have code 'execute' in bytecode format, but you can have JIT services turn it into machine code for an additional performance boost if need be. However, JNI (as I am beginning to see) is not real fun and there are some issues with Garbage Collection and parameter caching that can make the integration more difficult. AngleScript uses the FuBI technique (if I recall) which is about as fast as you will get - but that can open some stability issues. LUA is quick but not really an OO language.

Ultimately I think it really depends on your project and what you need from it. LUA worked pretty darn well in Baldurs Gate and FarCry (if I remember correctly) so there is no argument that it is viable and works. Given the success, and experience of all those projects (as I have played all those games to completion) I think they are all viable and it comes down to preference.


hmmm - maybe I should write an article...


hope that helps..,

#dth-0
"C and C++ programmers seem to think that the shortest distance between two points is the great circle route on a spherical distortion of Euclidean space."Stephen Dewhurst
Thanks for all the replys - you've all been very informative.

This topic is closed to new replies.

Advertisement