Using reflection for scripts

Started by Fool Running, April 23, 2006, 02:14:16

Previous topic - Next topic

Fool Running

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:
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:

   public static void setVariableOnObject(Object obj, String variable, Object value)
                throws NoSuchFieldException, IllegalAccessException{
        Class objClass = obj.getClass();
        Field field;
        while(variable.contains(".")){
            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
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Matzon

I would consider very much to use existing mechanisms for scripting, including rhino, groovy, beanshell etc.

Fool Running

Ok, I'll take a look at them.
Thanks :)
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

oNyx

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.

Fool Running

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?
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

tortoise

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.