My game uses scripts that are saved in assets/Scripts but I can’t simply load them with loadAsset(“Scripts/Example.lua”) cause it causes a ‘No loader registered for type lua’. Now I really don’t need anything except simply getting the object out of the asset (I can already run them so don’t go into Lua or scripting itself), how would I do this? register it without a loaderclass or something?
You could implement your own AssetLoader, and then register it with the AssetManager using registerLoader()
Thanks, but its still unclear to me how would such a AssetLoader look like or what it even does. I run the scripts with a method build into a library so except returning the script file itself it really doesn’t need to do anything.
How do I extract my file from an ‘InputStream’? It only returns integers this makes no sense to me
Read up on java streams.
Yeah I just did, it’s not that hard… I guess it’s getting late
Well anyway thanks
It’s not really working out as simple as I thought, when I register this class as a loader I get a NullPointerException
[java]package Loaders;
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetLoader;
import java.io.IOException;
import Objects.LuaBaseScript;
public class LuaScriptLoader implements AssetLoader{
public Object load(AssetInfo assetInfo) throws IOException {
AssetKey key = assetInfo.getKey();
System.out.println( key.getExtension() );
System.out.println( key.getFolder() );
System.out.println( key.getName() );
return new LuaBaseScript(key.getFolder());
}
}
[/java]
I try to register it with
[java]assetManager.registerLoader(“Loaders.LuaScriptLoader”, “lua”);[/java]
Whats the stack trace?
Here you go
SEVERE: Uncaught exception thrown in Thread[AWT-EventQueue-0,6,main]
java.lang.NullPointerException
at mygame.WindowLuaTest$1.run(WindowLuaTest.java:130)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:642)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:603)
at java.awt.EventQueue$1.run(EventQueue.java:601)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:612)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
The NullPointer is in your own code and not even executed on the OpenGL thread :roll:
Ugh sorry I’m really unfamiliar with debugging in Java, this means that the error is in the loader class?
This is all relevant code in the main class:
[java]
//This is line 130
WindowLuaTest.assetManager.registerLoader(“Loaders.LuaScriptLoader”, “lua”);
script = (LuaBaseScript) WindowLuaTest.assetManager.loadAsset(“Scripts/LuaTest.lua”);
lib = (LuaBaseScript) WindowLuaTest.assetManager.loadAsset(“Scripts/LuaTestLib.lua”);[/java]
No, its in or before line 130 in “WindowLuaTest.java”. NullPointers are the easiest bugs to track. One of the variables in that line is null, most probably the one you call a method on there.
Edit: Also you seem to call your code from a button press in AWT or something, make sure you do calls to the scene graph via the execution queue:
[snippet id=“10”]
Ah so that is the problem! I do have some GUI stuff but the error’ing code is in the main class’s run method. I’ll try what you did but would it also solve the problem if I move the problematic code to the simpleInitApp method?
Everything seems to go perfect now thank you so very much for that, but one little thing.
Why does this code return (slashes are reversed, the one’s I get dont work on this forum) D:/myProject/Scripts/LuaTest.lua instead of D:/myProject/assets/Scripts/LuaTest.lua? I guess I can fix it with some string manipulation but it seems very odd to me
[java]
public class LuaScriptLoader implements AssetLoader{
public Object load(AssetInfo assetInfo) throws IOException {
String path = new File(assetInfo.getKey().getName()).getCanonicalPath();
return new LuaBaseScript(path);
}
}
[/java]
Ugh, I suppose I need to register a locator too to do what I want? Isn’t there a tutorial on this topic, I can’t find any.
I swear after this I’ll stay away from the forums for at least a week
The NeoTextureLoader is a very simple example of a Loader and Locator (which is used to parse infos from the name string in this case):
http://code.google.com/p/jmonkeyplatform-contributions/source/browse/#svn%2Ftrunk%2FNeoTexturePlugin%2Fsrc%2Fcom%2Fjme3
Hmm it’s getting more and more clear but what are the actual definitions/purposes of AssetKey, AssetLocator and AssetLoader? and when you use the AssetManager to get something in what order are those being used?