Jump to content
Divine-X

Text Based Adventure Game: Save/Load

Recommended Posts

Okay so I'm working on a text base adventure game and I tried to make a save/load feature by using this [url]http://java.sun.com/developer/technicalArticles/Programming/serialization/[/url] 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

[code]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;
}

}
[/code]

Share this post


Link to post
Share on other sites
[CODE]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;
}[/CODE]

[CODE]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;
}[/CODE]

Because your setters are useless right now :p

Share this post


Link to post
Share on other sites
[quote name='Nouish'][CODE]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;
}[/CODE]

[CODE]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;
}[/CODE]

Because your setters are useless right now :p[/QUOTE]


Okay thanks for that but what about my save/load problem >.<.

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Okay thanks for that but what about my save/load problem >.<.[/QUOTE]

Provide some code related to your saving/loading, please :)

The fields you made getters/setters for have public access by the way

Share this post


Link to post
Share on other sites
Omg dude please learn java conventions, then work on your problem... You can find a good tutorial on XStream on google

Share this post


Link to post
Share on other sites
[quote name='Nouish']Provide some code related to your saving/loading, please :)

The fields you made getters/setters for have public access by the way[/QUOTE]

Yeah I know

[quote name='Steve']Omg dude please learn java conventions, then work on your problem... You can find a good tutorial on XStream on google[/QUOTE]

Don't bitch, if you can help me with my problems then help if you can't then be quiet. And I know Java is camelCasing but I get bored of it.

[code]package org.tbag.player.util;

import com.thoughtworks.xstream.XStream;

public class XStreamUtil
{
private static XStream xstream = null;

public static XStream getXStream()
{
if(xstream == null)
{
xstream = new XStream();
xstream.alias("player", org.tbag.player.Player.class);
}
return xstream;
}
}
[/code]

[code] public static void save()
{
try {
XStream xs = XStreamUtil.getXStream();
xs.toXML(p, new FileOutputStream(Player.SAVE_PATH+"Save.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}[/code]

No one make fun of the packaging <.<! Edited by Divine-X

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Yeah I know



Don't bitch, if you can help me with my problems then help if you can't then be quiet. And I know Java is camelCasing but I get bored of it.

[code]package org.tbag.player.util;

import com.thoughtworks.xstream.XStream;

public class XStreamUtil
{
private static XStream xstream = null;

public static XStream getXStream()
{
if(xstream == null)
{
xstream = new XStream();
xstream.alias("player", org.tbag.player.Player.class);
}
return xstream;
}
}
[/code]

[code] public static void save()
{
try {
XStream xs = XStreamUtil.getXStream();
xs.toXML(p, new FileOutputStream(Player.SAVE_PATH+"Save.xml"));
} catch(Exception e) {
e.printStackTrace();
}
}[/code]

No one make fun of the packaging <.<![/QUOTE]

[CODE]import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializationExample implements Serializable {

private String someSerializableString = "null";

public static void main(String[] args) {
// --- SAVING ---
SerializationExample exampleSave = new SerializationExample();
exampleSave.someSerializableString = "If this message is printed after running this then the save/load was a success!";
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("example.ser"));
out.writeObject(exampleSave);
out.close();
} catch (Exception e) {
e.printStackTrace();
}

// --- LOADING ---
SerializationExample exampleLoad = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("example.ser"));
Object object = in.readObject();
if (object instanceof SerializationExample) {
exampleLoad = (SerializationExample) object;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
if (exampleLoad != null)
System.out.println(exampleLoad.someSerializableString);
}

private static final long serialVersionUID = 586735038613610946L;

}[/CODE]

That's an example using serialization

Share this post


Link to post
Share on other sites
[quote name='Nouish'][CODE]import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializationExample implements Serializable {

private String someSerializableString = "null";

public static void main(String[] args) {
// --- SAVING ---
SerializationExample exampleSave = new SerializationExample();
exampleSave.someSerializableString = "If this message is printed after running this then the save/load was a success!";
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("example.ser"));
out.writeObject(exampleSave);
out.close();
} catch (Exception e) {
e.printStackTrace();
}

// --- LOADING ---
SerializationExample exampleLoad = null;
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("example.ser"));
Object object = in.readObject();
if (object instanceof SerializationExample) {
exampleLoad = (SerializationExample) object;
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}
if (exampleLoad != null)
System.out.println(exampleLoad.someSerializableString);
}

private static final long serialVersionUID = 586735038613610946L;

}[/CODE]

That's an example using serialization[/QUOTE]

Okay, thanks, but how would I get the information from my Player class
[code] public int HP = 100;
public int FT = 50;
public int COINS = 0;
public int STR = 10;
public int STAGE;[/code] into the .ser file?

Share this post


Link to post
Share on other sites
[quote name='Divine-X']Okay, thanks, but how would I get the information from my Player class
[code] public int HP = 100;
public int FT = 50;
public int COINS = 0;
public int STR = 10;
public int STAGE;[/code] into the .ser file?[/QUOTE]

The object is flattened so they will automatically be stored, unless they are transient:

[CODE]private transient int temporaryInt = new Random().nextInt(Integer.MAX_VALUE);[/CODE]

Share this post


Link to post
Share on other sites
[quote name='Nouish']The object is flattened so they will automatically be stored, unless they are transient:

[CODE]private transient int temporaryInt = new Random().nextInt(Integer.MAX_VALUE);[/CODE][/QUOTE]

So my save should be this then? [code] static Player p;

public static void save()
{
try {
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data/saves/save.ser"));
out.writeObject(p);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}[/code]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×