Subj : Javascript for scripting a computer game To : netscape.public.mozilla.jseng From : smileyhamster Date : Thu Nov 18 2004 09:42 am Hi, I'm trying to use Rhino to script a computer game with Javascript. The game consists of a grid of cells (think of a chess-board) where only one game entity can be placed on each cell. Each entity is a Java class which has several actions it can do like move up/down/left/right, place new entities on the board, display messages etc.. Each entity will have a script associated with it to make it do things. The game execution is split into discrete turns where each entity can do one action per turn. For example, at the start of a turn, an object could move up, then it has to wait until the next turn before it can do anything. Each entity gets a chance to perform one action per turn. Here is an example of the type of scripts I want. When the first turn starts, the "start" function is called: // Person function start() { while (true) { moveUp(); moveRight(); moveDown(); moveLeft(); } } function touched() { say("Hello!"); start(); } So when the first turn begins, the start function is executed, the entity will move one tile up then return control to the game to let the other entities update. Then in the next turn, moveRight() would be executed. If the player entity touches this entity, the current thread of execution is stopped, the "touched" function is called and the entity will say hello. If "start()" wasn't at the end of this function, the script would finish. What is the best way to implement this? I have a prototype working which uses the new and experimental continuations feature of Rhino. When moveUp() is called, the Java code to move the entity is called, the script state is saved, then the Javascript throws an exception which is caught by the game so that the script ends. The previous state is then reloaded in the next turn. Using continuations means I have to use interpreted Javascript though. Is there a better way to do this? For example, when the move function of the Java object is called, can I call a method on the Context object which will make the current executing statement return, while also being resumable? Also, each script can be assigned to multiple entities. For example, the player entity could create a entity each turn and attach a bullet script to it (to shoot at things). How can I do this using the least resources in a fast way? My prototype runs very slowly when I create new entities on the board but is fast when nothing is being created. Each time a new bullet entity is made, it runs code like this: ScriptableObject so = new ScriptableObject(); Context cx = Context.enter(); cx.setOptimizationLevel(-1); Scriptable scope = cx.initStandardObjects(so); // Assign "me" to point to the game entity // the script controls scope.put("me", scope, Context.toObject(new GameObject(), scope)); so.processSource(cx, "bullet.js"); cx.evaluateString(so, "start();", "", 1, null); // Will return here after the first action // is executed What can I reuse when I do this? I'm a little confused by how each object stores its state. For example, can I load the "bullet.js" code once only, attach this somehow to 100 different game objects but set the "me" variable to point to the entities they control for each one? Each entity needs to be able to store several variables, like "me" and whatever else the script needs to work. For example, the player entity might want to use a "score" variable to keep track of the player's score. Any help would be greatly appreciated. .