Better way of doing this?

This is quite a strange project but I will try to explain it. It takes code from an application called game maker (www.yoyogames.com) and converts that code to java.
In the game maker language (gml) you don’t need to declare variables, you just use them, and you can use a variable that is not defined yet from another ‘object’. Also variables can be either double or string and it can change from one to the other.
The current system works but since I am just a hobby programmer I don’t have very much experience so there could be a faster way to do it. Speed is important because its the backend of all the games. Its already faster than gm but i still would like to know if it can be any faster.

here is an example how the current system will convert gml:
gml:

java:

It uses a hashtable to store all the variable names with their values:

Code for Game.getValueOf():

[quote]public static Variable getValueOf(int i){
NUMBER_VARIABLE=integers.get(i);
if (NUMBER_VARIABLE !=null){
return NUMBER_VARIABLE;
}
else {
NUMBER_VARIABLE=new Integer(i);
integers.put(i, NUMBER_VARIABLE);
return NUMBER_VARIABLE;
}

}

[/quote]
It also uses a hashtable to store all numbers that will be used, to save memory rather than doing new Integer() every time.

Heres some more source files if you need to see them:
http://gjava.svn.sourceforge.net/viewvc/gjava/Dolphin/src/org/dolphin/game/api/types/

Thats basically the system, it uses hashtables and Variable objects to get and set variables. is their a more efficient way of doing this.

I appreciate any help :slight_smile:

You won’t get much more efficient without actually compiling GML to bytecode, but I must say that I find the type system absolutely horrible. “Hello, World” + 1 = 1?!

Actually “Hello, World” + 1 = “Hello, World1”
As it uses the string add function.

Yeah I suppose it is horrible but I can’t think of a better way of doing things :-.

maybe an idea but i will requiere some works and make source code coufusing, only to do if performance is very important.

1 - read the script and collect how many variable and their names. (each name will have an index)
=> create an indexed array at the start of your translated java


Variable variables[] = new Variable[NB_VAR];
for(int n=0;n<NB_VAR;n++)
 variables[n] =new Variable();

then:

a - use index everywhere
b - remove null check/alloc
c - remove casting

public Variable getVariable(int idx) {
       return variables[idx]
    }
public void setVariable(idx,value) {
        variables[idx].setValue(value);
    }

or even faster replace method call directly by object reference in the variables arrray

if “s” is the first vaiables

static int VAR_S=0;

{variables[VAR_S].setValue(variables[VAR_S].add(Game.getValueOf(1));}