Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

State Saves in JAVA


  • Please log in to reply
13 replies to this topic

#1 sausage

sausage

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 03 December 2008 - 09:47 PM

Ok so what I've got is this:

System.out.println("1)New");
		System.out.println("2)Help");
		System.out.println("3)Load");
		select = keyboard.nextInt();
		if(select==3)
		loadgame.load();

public static void load() throws FileNotFoundException,IOException,ClassNotFoundException
{

save temp = null;
 FileInputStream is = new FileInputStream("Save");
 ObjectInputStream oip = new ObjectInputStream(is);
 Object object = oip.readObject();
 oip.close();
 is.close();
 temp = (save)object;
}

So I save it later in the program, I have the file saved, trust me
what happens when I hit load is it lags for a few seconds then starts from the beginning of the game (After the New Help Load stuff)
If you need it I have the savegame code and the code I used to activate it

public static void save() throws FileNotFoundException,IOException
	{
		

save tempSave = new save();
tempSave.playerName = "asdfadsfasdfasdf";
tempSave.playerPass = "asdfadsfasdfasdf";
ObjectOutputStream oop = null; 
oop = new ObjectOutputStream(new
					BufferedOutputStream(new FileOutputStream("Save")));


 oop.writeObject((save)tempSave);
 oop.close();

}

and

System.out.println("1) Weapon Shop");
		System.out.println("2) Tavern");
		System.out.println("3) Save");
		System.out.println("4) Leave");
if(select==3)
		{
		savegame.save();
		Town(Sauce);
	}

The save works perfectly, It creates the file and then resets the menu for the town, if your wondering I havent created anything after leaving the town or going into the inn yet but I still need help with this :thumbsup:

Oh yeah, you need one more class

this is the "other" save class, if you look in the first one I say save tempSave = new save(); this is the actual save class the other one was called savegame

public class save implements java.io.Serializable{
 
	public String playerName;
	public String playerPass;
}

Help?

Thanks in advance

Sauce :-)

BC AdBot (Login to Remove)

 


#2 Billy O'Neal

Billy O'Neal

    Visual C++ STL Maintainer


  • Malware Response Team
  • 12,304 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Redmond, Washington
  • Local time:05:19 PM

Posted 03 December 2008 - 09:55 PM

temp = (save)object;

is the problem I think.
Not entirely positive how this works in Java but if if I'm not mistaken, you need to implement a copy constructor for the save class, because the memory allocated by the ObjectInputStream class is freed when that object is destroyed.

Can you post a copy of the generated file? (Attached)

Billy3

Edited by Billy O'Neal, 03 December 2008 - 09:55 PM.

Twitter - My statements do not establish the official position of Microsoft Corporation, and are my own personal opinion. (But you already knew that, right?)
Posted Image

#3 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 03 December 2008 - 09:59 PM

I'm not sure what you mean by any of that....... including the post the generated file.... how do I do that?

EDIT: Download Save

Edited by sausage, 03 December 2008 - 10:08 PM.


#4 jpshortstuff

jpshortstuff

    WhatTheTech Teacher


  • Members
  • 660 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:UK
  • Local time:01:19 AM

Posted 04 December 2008 - 02:29 AM

I notice you aren't catching any exceptions (from the code you posted) - might be worth doing so since your load method "throws FileNotFoundException,IOException,ClassNotFoundException"

Have you tried debugging to see where it is going wrong? Most Java IDEs have pretty easy to use debuggers, get a breakpoint in around the start of the load functions and check the contents of each variable after each step.
Trained at the What The Tech Classroom where you too could learn to help others.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Posted Image

#5 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:19 PM

Posted 04 December 2008 - 07:41 AM

you need to implement a copy constructor for the save class,

It shouldn't, because the data is saved as an object before the stream is closed, and it is the stream being closed, not the object. The memory for the stream might be getting frees; then again, it might not.

@sausage: is that code all part of the same class? And exactly what is it that you think you are doing, and exactly what is wrong. You say

what happens when I hit load is it lags for a few seconds then starts from the beginning of the game (After the New Help Load stuff)

Is that what is supposed to happen?

I have the file saved, trust me

How do you know? Because the file exists? Does it actually contain any information? Creating the file, and then writing to a file are two different things, and as has been pointed out, your not catching any exceptions anywhere, so how do you know for sure what is happening?

BTW, class names are always capitalized. save temp = null; makes no sense. Save temp = null; does.

So how about this, you explain how things are supposed to work, as in "I do this, then the application does this...., I do this, then the application is supposed to do x, but it does y instead." I'm not as smart as everyone else. I don't have time to guess what the problem is. :thumbsup:

#6 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 04 December 2008 - 07:40 PM

@sausage: is that code all part of the same class? And exactly what is it that you think you are doing, and exactly what is wrong. You say


what happens when I hit load is it lags for a few seconds then starts from the beginning of the game (After the New Help Load stuff)

Is that what is supposed to happen?


Hmm..... Good point, I know what I'm trying to do but not exactly what's supposed to happen, maybe that is what's supposed to happen with my code but I have no idea. What I'm trying to do is to make it so the save function saves exactly where (or somewhat close) the program currently is. I also want the load function to load where the save saved.... as of now it's not working. If you want my full code, my sig has it in a link

But hey, if you know how to help me out I would greatly appreciate it

I notice you aren't catching any exceptions (from the code you posted) - might be worth doing so since your load method "throws FileNotFoundException,IOException,ClassNotFoundException"

Have you tried debugging to see where it is going wrong? Most Java IDEs have pretty easy to use debuggers, get a breakpoint in around the start of the load functions and check the contents of each variable after each step.


I'm not exactly sure how i use my debugger, my you could help me out I use a compiler called BlueJ and have no idea how to access it. I can try to look for it but I could still use your help if possible. and what exactly would catching those do, I know I'm supposed to throw them but I'm not sure what catching will actually do/prevent.

#7 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:19 PM

Posted 04 December 2008 - 09:05 PM

I am happy to help. I spend a lot of time teaching proper coding techniques.
It sounds as though you are confused as to what your application is supposed to do, as opposed to what it really does. This solution to that problem is to save a file, and then verify that waht you think you saved is really what you saved. Once you are sure that data is being saved properly, then worry about loading data from the file.

Using a try/catch structure will allow you to help catch any errors that may be happening, without disrupting the flow of your application.

The easiest way to debug is to simply dumpt the contents of whatever variables are in play, to the console. Make sure that what you think your variables are doing, really are doing. I assume that you know by now what a System.out does.

#8 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 04 December 2008 - 10:50 PM

May I ask how to be sure about if my save file is working properly? I'm not exactly sure what you mean, it makes a file and it as 101 bytes, which I'm not exactly sure what that means in this particular case but it definetely has something in there. If you mean open it up in notepad I did and this is what it says:

sr save yВ@ L
playerNamet Ljava/lang/String;L
playerPassq ~ xpt asdfadsfasdfasdfq ~ 

Wow.... looking at that I'm pretty sure that it means nothing.... hmm......

Would that mean I have to put in another variable to add to the save file for reading when it loads?

If so, how exactly would I do that?

would I put something in like when your in the town say

State = 0;

and then out in like a dungeon be State = 1;

something like that?

That makes snese to me.... but then I would need multiple statements/arrays calling each area, there has to be a simpler way to do that right?

I mean, that seems kind of inefficient...

Anyways, that's the only idea I got on me, can you help?

EDIT: That worked however I currently have no other areas to test the variable in so I'm still not sure if that will work permanently.... :thumbsup:
I also haven't tested any variable changes inside the main character at all so I should probably do that.

EDIT2: After testing load with player variables..... the test failed....
Shouldn't be too hard to fix though, just have to add a few extra variables in my save.... so far working like it should :-)

EDIT3: Scratch the previous edit, the variables added made no difference except that instead of printing 10 and 0 it printed 0 and 0...

EDIT4: Through various debugging techniques in my brain I have noticed that 3 variables were static, removed static identifier and am recieeing compilation errors stating that non-static variables con not be used in a static context, however, through the savegame class, it is recieving the same variables in the same way with no compilation errors....

EDIT5: My bad, just realized that the only compilation error I am getting is for the if statement: if(tempSave.playerState==0){town.Town(Sauce)}
Probably means something...

EDIT6: Fixed that previous issue only to run into the exact same issue as last time, this is what it now says in notepad

sr savei}&qB I playerACI
playerDMODI playerStateL
playerNamet Ljava/lang/String;L
playerPassq ~ xp   t asdfadsfasdfasdfq ~ 

I am starting to believe Billy's post:

you need to implement a copy constructor for the save class


because nothing is working the way I want it to.... besides the load the town instead of beggining of game, which I'm not even sure works right


EDIT7: Giving up for the night if you want to view my code, before I get home from school to review replies, use link in my sig, it should work after 10 PM Mountain Standard (12PM Eastern, 9 Pacific)

Gnite! :-)

Edited by sausage, 04 December 2008 - 11:32 PM.


#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:19 PM

Posted 05 December 2008 - 06:55 AM

Your code is being saved just fine. That is what a serialized object looks like.

That makes snese to me.... but then I would need multiple statements/arrays calling each area, there has to be a simpler way to do that right?

I am not following you. Arrays do not call anything. They hold data. The area that you are in only needs to be held in a single variable,a nd there is no need to write to the disk every single time you make a move. THAT is horribly inefficient.

So now the next problem is?

#10 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 05 December 2008 - 12:47 PM

The next problem is now that when I tell it to load, I tell it to state the player's AC and the players DEX which I set as 15 and 6, then I saved and set it as 10 and 0, now when it states the variables it says 0 and 0 which sort of means that my player variables aren't saved, you can look at my code for a better view, but I definetely have no idea what I'm doing wrong (or not doing at all) my whole program is in my sig. If there is any more information you need I'm happy to help. :-) :thumbsup:

EDIT: I was talking about making an array for different state variables but... not exactly sure how that would work.

Edited by sausage, 05 December 2008 - 12:48 PM.


#11 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:19 PM

Posted 05 December 2008 - 01:12 PM

The compiled .jar file does not help me any. I need to see the .java files.

#12 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 05 December 2008 - 05:43 PM

If you get winRAR you can extract the .jar, I do believe that the .java files are in there however I can give you the java files, they will be in my edit, one moment

EDIT: Download Battle.rar That file has every .java file in my program, you will need winRAR for this.

Edited by sausage, 05 December 2008 - 05:52 PM.


#13 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:19 PM

Posted 05 December 2008 - 06:32 PM

you will need winRAR for this

No, I sure don't.

I'll need some time to take a look at the code. I am swamped with my own projects right now, so it will be a bit. I can't understand why you think you need to cast your save object to type save... it already is of type save.

EDIT: Where is your main class?

#14 sausage

sausage
  • Topic Starter

  • Members
  • 390 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Colorado
  • Local time:06:19 PM

Posted 05 December 2008 - 09:14 PM

The main class is the BattleCall class, and what do you mean by it is it's own save already.

What other program do you use to extract .RAR's?

EDIT: I think I got it! While I reviewed the code for loading the object, I realized that the variables that were being read don't go anywhere. Unfortunately I'm not sure what the command I should give it so they do go somewhere so for all of you out there that are trying to find the problem I think that's it.

EDIT2: Got it!!!!!!! it works just as it's supposed to, when AC is set to 15 then I save and switch it back to ten and load the save it still pops 15 WOOT! not going to update sig yet, I have some more stuff I need to add before I do that..... WOOTZORZ!


Topic Solved!

Thanks groovicus and billy for the help I greatly appreciate it!

Edited by sausage, 06 December 2008 - 02:21 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users