[SOLVED] loadSavable2DArray. Cannot cast Savables

Hello,



I am working on a mapeditor and decided to begin with saving and loading mapdata before I build stuff :slight_smile:

I am working with the savable Interface.



Loading, saving and then comparing two Tiles works flawlessly.



[java]public class Tile implements Savable{

boolean pathable;

int height;



//shortened Version without constructors or equals method



public void write(JmeExporter ex) throws IOException {

OutputCapsule capsule = ex.getCapsule(this);

capsule.write(height, "height", 0);

capsule.write(pathable, "pathable", false);

}



public void read(JmeImporter im) throws IOException {

InputCapsule capsule = im.getCapsule(this);

capsule.readInt("height", 0);

capsule.readBoolean("pathable", false);

} }

[/java]



I use Import and Exportcapsules of the JmeImporter and exporter.



Ints and Booleans load fine.



Now to the same situation with two maps.



[java]public class Map implements Savable {

private int xSize;

private int ySize;

private Tile[][] tiles;



// shortened Version again



public void read(JmeImporter im) throws IOException {

InputCapsule capsule = im.getCapsule(this);

xSize = capsule.readInt("xSize", 0);

ySize = capsule.readInt("ySize", 0);

Savable[][] temp = capsule.readSavableArray2D("tiles", null);

tiles = (Tile[][]) temp; //


"EXCEPTION HERE"
}

public void write(JmeExporter ex) throws IOException {
OutputCapsule capsule = ex.getCapsule(this);
capsule.write(xSize, "xSize", 0);
capsule.write(ySize, "ySize", 0);
capsule.write(tiles, "tiles", null);
}
[/java]

As noted, tiles = (Tile[][]) Temp; gives me my Exception: (SCHWERWIEGEND = Critical)
[java]SCHWERWIEGEND: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.ClassCastException: [[Lcom.jme3.export.Savable; cannot be cast to [[Lmapeditor.Tile;
at mapeditor.Map.read(Map.java:111)
at com.jme3.export.binary.BinaryImporter.readObject(BinaryImporter.java:341)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:243)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:130)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:271)
at com.jme3.export.binary.BinaryImporter.load(BinaryImporter.java:266)
at mapeditor.Main.simpleInitApp(Main.java:38)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:228)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:129)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:205)
at java.lang.Thread.run(Thread.java:679)[/java]

My Tile is Savable, so why can I not cast it from Savable to Tile?

You can’t cast arrays in java. Its just like that.

Blargh. Thought something like that would come up. Thanks!



So when do I use readSavable2DArray(…)?



If I use readSavable(…) instead, do I access the harddrive more often?



I dont know the jmonkey importer here, but I would assume it loads the file into ram and dumps it when the importer is dead.



P.S. This forum is fast as hell…

No way to store the data more “raw” and initialize the objects in read()?

Hmm cant think of a nice extendable way to do that, sounds like optimizing to be done someday when I’m not a prototype.



Guess I will keep that in mind but for now, just load in a nested loop.