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.
