[SOLVED]Saving and Loading

I am currently making a game where players can make buildings. However, when i was playing with saving and loading i stubbled accross a weird problem. Objects save fine but when they are loaded they sort of go invisible. However when i check the data by calling all the buildings in the system to check their positions and their names everything looks fine so yeah im kinda confused :confused: . One thing you might want to know before looking at the code, “buildings” is an Arraylist and takes three parameters, i.e. new Building(string, int, int);. Here is the code i use for saving and loading buildings:

public static void Save_Buildings() {
		try {

			// saving
			FileOutputStream saveFile = new FileOutputStream("saves/World_Data.sav");
			ObjectOutputStream save = new ObjectOutputStream(saveFile);

			save.writeObject(Object_Control.Buildings.size());
			
			for (int i = 0; i < Object_Control.Buildings.size(); i++) {
				save.writeObject(Object_Control.Buildings.get(i).name);
				save.writeObject(Object_Control.Buildings.get(i).x);
				save.writeObject(Object_Control.Buildings.get(i).y);
				}
			for (int i = 0; i < Object_Control.Buildings.size(); i++) {
				System.out.println(Object_Control.Buildings.get(i).name);
				System.out.println(Object_Control.Buildings.get(i).x);
				System.out.println(Object_Control.Buildings.get(i).y);
				}
			save.close();
			saveFile.close();
			
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}	
	public static void Load_Buildings() {
		try {
			FileInputStream saveFile = new FileInputStream("saves/World_Data.sav");
			ObjectInputStream save = new ObjectInputStream(saveFile);
			
			Object_Control.Buildings.clear();
			
			int AmountOfBuildings = (Integer)save.readObject();
			
			for (int i = 0; i < AmountOfBuildings; i++) {
				Object_Control.addBuilding(new Building("",0,0));
			}
			
			for (int i = 0; i < Object_Control.Buildings.size(); i++) {
				Object_Control.Buildings.get(i).name = (String) save.readObject();
				Object_Control.Buildings.get(i).x = (Integer) save.readObject();
				Object_Control.Buildings.get(i).y = (Integer) save.readObject();
				}
			
			for (int i = 0; i < Object_Control.Buildings.size(); i++) {
				System.out.println(Object_Control.Buildings.get(i).name);
				System.out.println(Object_Control.Buildings.get(i).x);
				System.out.println(Object_Control.Buildings.get(i).y);
				}
			save.close();
			saveFile.close();
			Menu.loading=false;
			
		} catch (Exception exc) {
			exc.printStackTrace();
		}
	}

Genius Coder doesn’t realize that nobody can really help you with such issues. This is not a library usage bug, like LWJGL. This is the code you written buggy. This is only loading part. You might be doing something wrong in a different place entirely.

The problem might be that you’re actually assigning values that you use for rendering ONLY after some kind of an event. But seriously, nobody can really help you.

PS

People should stop doing something, just because they think they can be the best at it. Like “Genius Coder”. You obviously are not a genius and you will never be. I remember when I started playing Counter Strike 1.6 like when I was 10 years old, first time I connected to server, my username was “killer”, even though I couldn’t kill anyone. This seems like a thing that humans use to hide their weakness. In reality I was a noob at that game who couldn’t do anything, so I used words to try to make an impression. You’re doing the same here. I’m not blaming or anything, just something I noticed about human beings :smiley:
Ow just to add it, if you don’t know what League Of Legends is, it is basically a match game (like CoD) where you play matches about 30 minutes or something. You can play a lot of different characters. There are people who only “Main” 1 character and it is like they “think” they are good at it, because they “main” it. In reality, they are usually the worst player in the team. :frowning:

I think I should stop this shiet. Way too off topic :smiley:

^ Pretty much. The name is kinda obnoxious; I could care less but it’s impossible to tell what the problem is from the limited code you’ve provided.

[offtopic]
@trollwarrior Maining a champion actually works, if you put the time into it. But yeah, autolock teemo is kinda annoying.
[/offtopic]

Just something I would like to point out, I didnt choose my name my frined did so it cant be much of a weekness can it. I also realised that I had limited the code here, thats because I’m making a game to sell so I cant release a load of source code, anyway what would help you solve the problem. In other words what do you need to see?

I don’t think you got what we said.

The problem might be anywhere in your source code. ANYWHERE. From my little experience, I would suggest you look into constructors, type changes. You should first of all do a rough search for the bug. Meaning, don’t stare at the same code for a long time. Just look if it has bugs or not, if not, check other parts of the code. Don’t limit yourself to one area of the code. Just look at as much code as possible. BUGS ARE AT PLACES YOU DON’T EXPECT THEM TO BE. So just LOOK ANYWHERE. It is like “don’t think, just do it”.

Also, about the commercial thingy. Did you ever do a commercial project before?

Ok you shouldn’t be trying to sell a game if you’re new to game development. And even then releasing source code is the least of your issues. You can give us that snippet of code, or you can solve the problem by yourself. You’re talking of invisible objects, but you’re not even giving us the code that renders.

[quote]Ok you shouldn’t be trying to sell a game if you’re new to game development. And even then releasing source code is the least of your issues. You can give us that snippet of code, or you can solve the problem by yourself. You’re talking of invisible objects, but you’re not even giving us the code that renders.
[/quote]
Sorry I expected you to sort of guess

		for (Building c : Buildings) {
			c.draw(g2d);
		}

Sorry I expected you to sort of guess

		for (Building c : Buildings) {
			c.draw(g2d);
		}

[/quote]
Yup cause we all have crystal balls here and we know what your draw method does ::slight_smile:

Also, your coding style is terrible. Please read & learn existing Java programs before trying to write your own.

[quote]Also, about the commercial thingy. Did you ever do a commercial project before?
[/quote]
I beleive not no

[quote]Yup cause we all have crystal balls here and we know what your draw method does
[/quote]
Well I mean its obvious enough so dont be a sarcy little shit because im seriously not in the mood

Are you for real mate?

Seriously… If this is your FIRST PROJECT, you should just share it with everyone for free. g2d probably means “graphics 2d” which means your game is probably shiet. If this is your FIRST COMMERCIAL PROJECT, make it free also, you will soon realize that nobody will buy your game. You can trust me on that one. I thought I was making the best game. 2 people bought it for 2EUROS each. I could call it a success. You need tremendous amount of work for people to enjoy it. You always need to put in 10 times more work than you’re putting in.

The problem is likely one of the following

An order of events issue.
or
A scope issue (scope as in what variables can be referenced and from where)

Your drawing works… you showed this by drawing the buildings initially.
But your loading doesnt… so what is different in these flows?

All of this i am summarizing based on your description of the problem not your code.

j.

Honestly if I didnt know better i’d just assume this guy’s an elaborate troll.

Im really not in the mood mate so if your not here to help with the problem like I have very kindly requested at the start of the thread then please leave and if i want to sell a game, whether it’ll be a succes or not I honestly dont care

Did you use BufferedImages to display them? If so you should know that these are transient and doesn’t get saved.
Save the Path to the image and get it back when you load. Something like


	
	public void setBattlerSprite(String filePath) {
		this.spritePath = filePath;
		this.battlerSprite = new Sprite(filePath);
	}
	
	public Sprite getSprite() {
		if(this.battlerSprite == null) {
			this.battlerSprite = new Sprite(spritePath);
		}
		
		return this.battlerSprite;
	}

@Genius Coder
Everyone here has been trying to help you. But you need to post more code. If you want help I recommend you actually listen to the people who are trying to help you, who are better at coding than you.

Here i’ll try and help you now, but you honestly need to post more code.


class Building implements Serializable {
    int x, int y;
    String path;
    transient BufferedImage wot;

    public void draw(Graphics2D g) {
        if (wot == null) wot = loadImage(path);
        g.drawImage(wot, x, y, null);
    }
}

then to save:

ObjectOutputStream oos = ..
oos.writeInt(buildings.size());
for (Building b : buildings) oos.writeObject(b);

then to load:

ObjectInputStream ois = ...
int zzzz = ois.readInt();
buildings = new ArrayList(zzzz);
for (int i = 0; i < zzzz; i++) buildings.add((Building)ois.readObject());

In your buildings constructor do you do any kind of image initialization? If so, then when you set the variables directly ie:


Object_Control.Buildings.get(i).name = (String) save.readObject();
Object_Control.Buildings.get(i).x = (Integer) save.readObject();
Object_Control.Buildings.get(i).y = (Integer) save.readObject();

it will not have any effect. Why are you not just creating the objects when you load:


for (int i = 0; i < AmountOfBuildings; i++) {
     Object_Control.addBuilding(new Building((String)save.readObject(), (Integer)save.readObject(), (Integer)save.readObject()));
}

Try this for all your loading code and see if it fixes it.

Could you post your building constructor code and initialization to help us debug it easier?

I don’t know if I missed something, but this guy is just like the next clueless newbie, yet for some reason, he is getting flamed off the mark, just because of his username.

He’s gotten the general response these people normally get (learn to debug, don’t bother selling your first game, if you want help, give relevant source, etc.), but the attitude has been a lot… darker. :-\

On topic:

Learn to debug.
Don’t bother selling your first game.
If you want help, we need the relevant source code.

Shut the fuck up. Nobody tells someone that. Ever. How about I tell you that? You are a retard, a complete idiot, and you will never ever be the least bit smart. That didn’t sound so great did it? Now I do not believe you are stupid, quite the opposite actually, but you need to think about how you would feel if when you first started programming someone told you that when you asked a question. Please do.

Excuse my language. I don’t think it’s acceptable for people to tell someone they are stupid or something of the like when they don’t know them and just judge on what they first see. Please, next time think, “Will any good come out of me saying this or doing that?”. Because saying that stuff just puts people down and makes you look like a dick.