Programming => General Java Game Development => Topic started by: Fool Running on April 23, 2006, 02:14:16

Title: Using reflection for scripts
Post by: Fool Running on April 23, 2006, 02:14:16
I'm making a scripting language for my game engine and I'm using reflection to set variables from the script to the code in Java. The script variable assignments look something like this:
Code: [Select]
Parent.velocity.x = 5
Where the Parent variable is the current owner of the script.

My problem is this: I can't find a good way of processing the variable (Parent.velocity.x). Currently I use the following:

Code: [Select]
   public static void setVariableOnObject(Object obj, String variable, Object value)
                throws NoSuchFieldException, IllegalAccessException{
        Class objClass = obj.getClass();
        Field field;
            field = objClass.getField(variable.substring(0, variable.indexOf(".")));
            variable = variable.substring(variable.indexOf(".") + 1);
            obj = field.get(obj);
            objClass = obj.getClass();
        field = objClass.getField(variable);
        field.set(obj, value);

The scripts control various objects in the game world and calls to this method (and its counterpart, the getter) can happen hundreds/thousands of times a second; This doesn't seem very efficient to me :lol: .
Does anyone else have any ideas on how to parse variables in a script like this?
The rest of the script commands I've been able to "compile" (create classes that represent the commands to improve execution speed), but I can't figure out how assignments like this could be "compiled" since the same script can control multiple objects (i.e. Parent can change) :?

Any ideas would be appreciated. :D
Title: Using reflection for scripts
Post by: Matzon on April 23, 2006, 07:39:03
I would consider very much to use existing mechanisms for scripting, including rhino, groovy, beanshell etc.
Title: hmmmmm...
Post by: Fool Running on April 23, 2006, 17:44:49
Ok, I'll take a look at them.
Thanks :)
Title: Using reflection for scripts
Post by: oNyx on April 24, 2006, 00:51:13
Haha... there are like 20 scripting languages for java :)

I also suggest to take a look at Janino. Its not a really a scripting language (its a very fast on-the-fly compiler), but its very nice for scripting regardless. You can also script complete classes with it (which in turn can reference other scripted classes etc). That has the advantage that you can compile it at the end, which means you can save ~360kb (janino.jar).

Compiling the complete logic of a small game takes only a split second (maybe 100-200msec on my lame 500mhz machine) and you get *full* execution speed. While its like 1/400 of the usual speed with beanshell. Other scripting languages are faster than that, but you usually get about 1/40 at most.
Title: hmmmmmmmm...
Post by: Fool Running on April 25, 2006, 17:35:19
It seems like Janino is more what I was looking for. Mostly because of execution speed.
Isn't the next version of Java supposed to have runtime compiling of code (Similar to what Janino does)? Do you know of any possible benefits of Janino over that?
Title: Using reflection for scripts
Post by: tortoise on August 27, 2006, 02:55:02
Yeah reflection is not the way to go if you're looking for speed :)

I've also used jython to good success in Java games. At a convenient down time -- typically right before a level starts -- I take the jython script and have it compiled into bytecode and from then on the script's no worse than the actual Java code.