Saving an array

Hi JGO,

I am getting errors everytime I am opening a new chunk, but when I am fixing other problems it gets kinda annoying… So I wanted to fix this problem, but I don’t know if there is a way to save a LevelTile[][]…

Currently my saving and loading scripts are:
Saving:

save.writeObject(this.tiles);

Loading:

this.tiles = (LevelTile[][]) load.readObject();

And the whole code (if you want to see this for some reason):

    public void saveChunk(){
		try {
			File file = new File(levelDirectory + "/chunks/chunk[" + x + "," + y + "].dat");
			if(file.exists()){
				file.delete();
			}
			FileOutputStream saveFile = new FileOutputStream(levelDirectory + "/chunks/chunk[" + x + "," + y + "].dat");
			ObjectOutputStream save = new ObjectOutputStream(saveFile);
			save.writeObject(this.tiles);
			save.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    public void loadChunk(){
		try {
			File file = new File(levelDirectory + "/chunks/chunk[" + x + "," + y + "].dat");
			if(file.exists()){
				FileInputStream loadFile = new FileInputStream(levelDirectory + "/chunks/chunk[" + x + "," + y + "].dat");
				ObjectInputStream load = new ObjectInputStream(loadFile);
				this.tiles = (LevelTile[][]) load.readObject();
				System.out.println("CHUNK LOADED");
				load.close();
			} else {
				generateChunk();
				System.out.println("CHUNK GENERATED");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

If you’ve any questions, please ask!
Thanks already!
-RoseSlayer.

First off, they aren’t scripts. Sorry, but it bugs me when people call code scripts.

Second, as you’re making a voxel game and I’ve dabbled in it(I have a YouTube series I never finished), I can say you shouldn’t store the actual tiles in an array, save the id of the tiles (generally a byte).

Third, your code isn’t a bunch of Java functions, so saying it doesn’t work is the least helpful thing you can do. Make sure you allow your chunks to be serializable as well as the tile class (I believe). Post your generateChunk method, show us what you do with the array you load from the file. A helpful thing to do would be to loop through the array and print out every object to see if the file is saving/loading correctly.

Sorry for using the word “scripts” in the wrong way… Saving the id in a bite is actually a very smart idea! easy and works great. If i am using bytes I also don’t need all the other variables so it will also be very good for the CPU. Thank you very much for this! When I post a topic next time I’ll keep it in mind to do what you said! So thank you again!

-RoseSlayer.

It is probably impossible to tell what the problem is. If you have large chunks of code like this, you need to work it out yourself. Even though chunk is getting you an exception, the problem might be lying within load method or something. + I think you should be using 1d arrays… I don’t even know how 2d arrays work :smiley: It is faster I think and easier to use for me at least.

Could you just use a nested for loop to save each element at a time? Then when you load it load it through another nested for loop and refill the 2d array?

He should do this, but he’s serializing his objects. Both ways work, but I prefer manually savings objects unless they are very simple.