Speed up Embedded Groovy in Java
I am trying to create scripted AI for a game engine I'm working on for fun. I decided to try to use Groovy to create .groovy behavior files which are loaded in as GroovyClasses and then cast to my behavior interface. The problem is, is that this is incredibly slow. I'm performing the updates each game cycle and the scripted AI classes bring the program to a crawl. 开发者_运维技巧My question is, is there a way to speed up embedded groovy speed or is there a better way to do what I am doing? Thanks for any help.
Here is the relevant code:
public interface Behavior {
public void execute(GameComponent component, float time);
}
The Groovy files are loaded at init time and are cast GroovyClasses:
public boolean cacheScript(String key, String path) {
try {
Class groovyClass = loader.parseClass(ClassLoader
.getSystemResourceAsStream(path));
Object object = groovyClass.newInstance();
scripts.put(key, (Behavior) object);
} catch (InstantiationException e) {
return false;
} catch (IllegalAccessException e) {
return false;
}
return true;
}
Then a GameComponent executes this code like this
engine.getBehavior(key).execute(component, time);
With out looking at your entire project it isn't possible to tell if the bottleneck is constantly loading and compiling Groovy code, or if it is running the actual Groovy code once it's loaded. If your issue is that once the Groovy code is loaded in your game it is taking to long to execute take a look at Groovy++ I'm not sure how well it works since it's still in early days but if this is a learning project it may be stable enough for you.
精彩评论