What I want:
I want to load *.groovy script files with assetManager:
[java] Script script = (Script) assetManager.loadAsset(“Scripts/helloworld.groovy”);
script.invokeMethod(“onHelloWorld”, null);[/java]
However… I need the engine from ScriptManager class in order to create the Script files to return from GroovyAssetLoader. But I don’t know how to access the scriptManager instance to call the getEngine() method in GroovyAssetLoader.
[java] public class GroovyLoader implements AssetLoader { @Override
public Object load(AssetInfo assetInfo) throws IOException {
GroovyScriptEngine engine = scriptManager.getEngine();
Script script = null;
You can make either the ScriptManager a static variable / access method or devise another way you load your script (e.g. make a transient instance of the script generating class or load “raw” data that you still pass to the ScriptManager after loading).
Thanks for the reply. I decided to devise a new way because the GroovyScriptEngine only accepts the path to the script as String object. I can’t give it the script as a String object.
Well… My new plan is to make the ScriptManager a singleton:
?? This way you have to specify the path to your assets twice and don’t load the script content via the asset system, completely bypassing the whole point of the asset loader system?
What do you mean by that I have to specify the path to my assets twice? As for the bypassing the whole point… yes, my hands are tied because I can’t tell GroovyScriptEngine that here is variable X with contents of a script file as string and make a groovy.lang.Script object from it for me please.
I further inspected my code and what you’ve linked and I will use the built in script support that you’ve mentioned. It’s waaaaaay more flexible than the solution I’m using now. This way, I won’t bypass assetManager, because I will load the script file as string with it and pass this string to the engine creating the script object itself. Thanks for everyone!