views:

54

answers:

2

I currently write a small game engine for 2D games in java.
As part of the engine I want to support scripts as the main way to implement in-game events.

  • the scripts may run a long time
  • several scripts can run parallel
  • calls to java code are limited

As I want to implement a save-game function I will have to get the current state of the scripts in a format I can store in a file and load again at a later point.

  • Are there any script engines for java which support this? (javascript or other)
  • How do they deal with multithreading and calls to java code?

  • Are there any good resources for this problem online?

Thanks
Edit: For clarity of what I want to do

  1. Game starts One or more scripts start to run in parallel
  2. Player calls save function
  3. The scripts pause
  4. The state of the scripts is stored continue or end
  5. Player calls load function
  6. the state of the scripts is loaded
  7. the scripts resume

I would like to know if there are any scripting engines for java which support pausing and storing their internal state in such a way.
(Support for the official java scripting api is not required)

A: 

I am not sure if this is a solution for your problem. Here are some of the scripting engine i know that work in jvm

  1. Rhino javascript engine, mozilla.org/rhino
  2. Clojure lisp engine, clojure.org
  3. groovy, groovy.codehaus.org

May be this article will help

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/

chinmaya
my problem is not scripting itself, it is about pausing a running script and storing its state so that I can continue it later from where I stopped it.
josefx
+1  A: 

What you are talking about is essentially the running of coroutines, along with the ability to serialise a coroutine's state. Sadly I know little about Java or the scripting facilities available to it, but some game developers use Lua, which features coroutines, with Pluto, a serialisation library that - I'm told - allows you to save out all your individual coroutine states. Obviously this means that all the relevant state is in the coroutine and that such references that get saved out still make sense when you read them back in - this typically means access through predictable handles and ID values.

Kylotan
While I don't like the manual control flow handling with yield and resume, it will cause some problems and may even prevent others. Ignoring this Lua for java and Pluto seem to be just what I need :-)
josefx
One thing I found, the lua library for java still requires the native library, I want to avoid native libs as much as possible.
josefx
The manual flow control saves you a lot of trouble - synchronisation of data becomes a lot easier when you know exactly where the context switches can happen. As for native libs, I did see that someone implemented Lua in Java somewhere, but I don't know if Pluto will work with that.
Kylotan
ok, it does what I asked for and nothing is perfect :-).
josefx