10 Replies - 1643 Views - Last Post: 29 June 2012 - 04:18 PM Rate Topic: -----

#1 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 12:26 PM

Okay so I'm working on a text base adventure game and I tried to make a save/load feature by using this http://java.sun.com/.../serialization/ I failed.

Then I tried using XStream, I got confused <.<.

So now I need help trying to make a save & load feature using XStream.

Here's my player file if you need it

public class Player implements Serializable
{
	private static final long serialVersionUID = 8495992061792087696L;
	public int HP = 100;
	public int FT = 50;
	public int COINS = 0;
	public int STR = 10;
	public int STAGE;
	static String SAVE_PATH = "data/saves";
	
	public Player(int HP, int FT, int COINS, int STR, int STAGE) 
	{
		super();
		HP = this.HP;
		FT = this.FT;
		COINS = this.COINS;
		STR = this.STR;
		STAGE = this.STAGE;
	}
	
	public int getSTAGE() 
	{
		return STAGE;
	}

	public int getCOINS() 
	{
		return COINS;
	}
	
	public int getSTR() 
	{
		return STR;
	}
	
	public int getHP()
	{
		return HP;
	}
	
	public int getFT()
	{
		return FT;
	}
	
	public int getCoins()
	{
		return COINS;
	}

	public void setCOINS(int COINS) 
	{
		COINS = this.COINS;
	}

	public void setSTR(int STR) 
	{
		STR = this.STR;
	}

	public void setHP(int HP) 
	{
		HP = this.HP;
	}

	public void setFT(int FT) 
	{
		FT = this.FT;
	}
	
	public void setSTAGE(int STAGE) 
	{
		STAGE = this.STAGE;
	}

}



Is This A Good Question/Topic? 0
  • +

Replies To: Text Based Adventure Game: Save/Load

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10566
  • View blog
  • Posts: 39,107
  • Joined: 27-December 08

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 12:36 PM

See this serialization tutorial.
Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 12:54 PM

All of your set methods are wrong. You are changing the local value instead of instance value

public void setCOINS(int COINS) 
{
	COINS = this.COINS;
}



Should be
public void setCOINS(int COINS) 
{
	this.COINS = COINS;
}


Was This Post Helpful? 2
  • +
  • -

#4 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 02:02 PM

View PostCasiOo, on 29 June 2012 - 12:54 PM, said:

All of your set methods are wrong. You are changing the local value instead of instance value

public void setCOINS(int COINS) 
{
	COINS = this.COINS;
}



Should be
public void setCOINS(int COINS) 
{
	this.COINS = COINS;
}



Okay...

	public static void save()
	{
		System.out.println("Saving game...\n");
		Main.sleep();
		try {
			ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data/saves/save.ser"));
			out.writeObject(p);
			out.close();
		} catch (Exception e) {
			System.out.print("Save failed");
			e.printStackTrace();
		}
	}
	
	public static void load()
	{
		System.out.print("Loading game...\n");
		try 
		{
			ObjectInputStream in = new ObjectInputStream(new FileInputStream("data/saves/save.ser"));
			Object object = in.readObject();
			if(object instanceof PlayerHandle) {
				object = (Player) object;
			}
			in.close();
		} catch(Exception e) {
			e.printStackTrace();
		}


I got my save working but whenever I make myself lose HP then I save it, note this is all in 1 run, then it loads it says I lost 50 hp perfectly but then when I try to rerun my game it says my hp is 100 when it should be 50.

http://prntscr.com/bbndh

Now if I run it again it'll say my HP is 100 when it should say 50.

http://prntscr.com/bbnef
Was This Post Helpful? 0
  • +
  • -

#5 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 02:11 PM

package org.tbag.player;

import java.io.Serializable;

/**
 * @author Storm
 * This class handles the players attributes
 * Health, Fatigue, Strength, Coins and Stages
 * We use the stage to remember where the player has left off when
 * We load the game
 */
public class Player implements Serializable
{
	private static final long serialVersionUID = 8495992061792087696L;
	public static int HP = 100;
	public int FT = 50;
	public int COINS = 0;
	public int STR = 10;
	public int STAGE;
	
	public Player(int HP, int FT, int COINS, int STR, int STAGE) 
	{
		super();
		HP = Player.HP;
		FT = this.FT;
		COINS = this.COINS;
		STR = this.STR;
		STAGE = this.STAGE;
	}
	
	public int getSTAGE() 
	{
		return STAGE;
	}

	public int getCOINS() 
	{
		return COINS;
	}
	
	public int getSTR() 
	{
		return STR;
	}
	
	public static int getHP()
	{
		return HP;
	}
	
	public int getFT()
	{
		return FT;
	}
	
	public int getCoins()
	{
		return COINS;
	}

	public void setCOINS(int COINS) 
	{
		this.COINS = COINS;
	}

	public void setSTR(int STR) 
	{
		this.STR = STR;
	}

	public void setHP(int HP) 
	{
		this.HP = HP;
	}

	public void setFT(int FT) 
	{
		this.FT = FT;
	}
	
	public void setSTAGE(int STAGE) 
	{
		this.STAGE = STAGE;
	}

}



If my player file, I feel like it will be, is bad can you fix it or tell me what I need to do to fix it?
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 02:47 PM

First of all you have to change your Player constructor. Remember that you are assigning the value to the variable on the left of the = sign. this.FT = FT; :)

In your load method, this line doesnt actually do anything
object = (Player) object;


Was This Post Helpful? 0
  • +
  • -

#7 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 03:51 PM

View PostCasiOo, on 29 June 2012 - 02:47 PM, said:

First of all you have to change your Player constructor. Remember that you are assigning the value to the variable on the left of the = sign. this.FT = FT; :)

In your load method, this line doesnt actually do anything
object = (Player) object;



So I can just take it out or do I need to fix it?
Was This Post Helpful? 0
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 03:55 PM

View PostStorm2059, on 29 June 2012 - 10:51 PM, said:

View PostCasiOo, on 29 June 2012 - 02:47 PM, said:

First of all you have to change your Player constructor. Remember that you are assigning the value to the variable on the left of the = sign. this.FT = FT; :)

In your load method, this line doesnt actually do anything
object = (Player) object;



So I can just take it out or do I need to fix it?


You should replace it with something... Let your load method return the player that has been loaded?
Maybe another example of what you are currently doing can help you understand what you are doing wrong.
This is exactly like what you are doing:

int a = 5;
a = a; //Has no effect!


Was This Post Helpful? 0
  • +
  • -

#9 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 04:00 PM

View PostCasiOo, on 29 June 2012 - 03:55 PM, said:

View PostStorm2059, on 29 June 2012 - 10:51 PM, said:

View PostCasiOo, on 29 June 2012 - 02:47 PM, said:

First of all you have to change your Player constructor. Remember that you are assigning the value to the variable on the left of the = sign. this.FT = FT; :)

In your load method, this line doesnt actually do anything
object = (Player) object;



So I can just take it out or do I need to fix it?


You should replace it with something... Let your load method return the player that has been loaded?
Maybe another example of what you are currently doing can help you understand what you are doing wrong.
This is exactly like what you are doing:

int a = 5;
a = a; //Has no effect!



I'm still confused..
Was This Post Helpful? 0
  • +
  • -

#10 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1398
  • View blog
  • Posts: 3,097
  • Joined: 05-April 11

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 04:14 PM

sorry I have no idea how to explain it to you so you will understand it
Was This Post Helpful? 0
  • +
  • -

#11 Storm2059  Icon User is offline

  • New D.I.C Head

Reputation: -5
  • View blog
  • Posts: 44
  • Joined: 29-June 12

Re: Text Based Adventure Game: Save/Load

Posted 29 June 2012 - 04:18 PM

View PostCasiOo, on 29 June 2012 - 04:14 PM, said:

sorry I have no idea how to explain it to you so you will understand it

Can't you figure it out and post the fix and add comments?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1