Skip to content

Pre compiling scripts

Thomas Cashman edited this page Jul 9, 2016 · 1 revision

To improve performance GameScriptingEngine provides a mechanism for compiling your scripts into bytecode for the JVM to interpret. This is a blocking operation so if called on the game thread it will block execution - to avoid slowing down the frame rate try to only call this once per frame or on a separate thread.

Compiling a script returns an identifier for that script for later invocation.

int scriptId = scriptingEngine.compileScript("...script content");

Scripts can also be read and compiled from input streams.

InputStream inputStream = ....;
int scriptId = scriptingEngine.compileScript(inputStream);

Now you can invoke the script.

ScriptBindings scriptBindings = new ScriptBindings();
scriptingEngine.invokeCompiledScript(scriptId, scriptBindings);

For more complex scripts you'll probably want to load them from files on your classpath or filesystem.

ScriptBindings scriptBindings = new ScriptBindings();
scriptingEngine.invokeScript("puts \"Hello world!\"", scriptBindings);
Clone this wiki locally