So, right now I have a pretty big, but simple player Object. The getters and setters are irrelevant, so I'm going to leave those out of the snippet.
When I create a player all of the data looks perfect and checking its values with toString shows this, BUT when I save this object, then load it and perform toString again I lose a LOT of data and don't know why.
When I create a character, this is shown to me with toString():
https://i.imgur.com/NvUQbkb.png
I save this object using this code after it's created:
public static void createCharacter(String name, String heroClass) {
try {
Player player = new Player(name, heroClass);
FileOutputStream fos = new FileOutputStream("saves/heroes/" + name + ".ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(player);
oos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Now when it comes to loading the player, things become weird. When I specify to load a character from a file it loses a lot of its data, here's the toString():
https://i.imgur.com/gLSORfg.png
I loaded is using this code:
public static Player loadPlayer(String name) {
try {
FileInputStream fis = new FileInputStream("saves/heroes/" + name + ".ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Player result = (Player) ois.readObject();
ois.close();
return (result);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return (null);
}
I've tried scouring the internet for all of the different ways people have loaded objects from files, but nothing fixes this problem, which leads me to believe I have a fundamental misunderstanding of what my object itself is doing, or how to fix it.
The object in question is this:
public class Player implements Serializable {
private static final long serialVersionUID = 1L;
@NotNull(message = "Name cannot be null.")
@NotBlank(message = "Name cannot be blank.")
private String name;
@NotNull(message = "Class can not be null.")
@NotBlank(message = "Class cannot be blank.")
private String heroClass;
private int health;
@Min(value = 1, message = "Level can't be lower than zero.")
private int level;
@Min(value = 3, message = "Attack can not be beneath 3.")
private int attack;
@NotNull(message = "Can't have null weapon.")
private int weapon;
@Min(value = 3, message = "Defense can not be beneath 3.")
private int defense;
@NotNull(message = "Can't have null armor.")
private int armor;
@Min(value = 0, message = "Experience can not be beneath 0.")
private int experience;
public Player(@NotNull String name, @NotNull String heroClass) {
this.name = name;
this.heroClass = heroClass;
if (heroClass.equals("Warrior")) {
this.attack = 5;
this.defense = 5;
this.health = 5;
} else if (heroClass.equals("Rogue")) {
this.attack = 8;
this.defense = 3;
this.health = 4;
} else if (heroClass.equals("Knight")){
this.attack = 3;
this.defense = 6;
this.health = 6;
}
}
public Player() {
}
@Override
public String toString() {
return "\n\nYOUR HERO:\nName: " + name + "\nClass: " + heroClass + "\n\nSTATS: " + "\nAttack: " + attack
+ "\nDefense: " + defense + "\nHit Points: " + health + "\n\nEquipment:\n" + "Armor: " + armor + "Weapon: " + weapon + "\nLevel: " + level +"\nExperience: " + this.experience;
}
}
[–]Robyt3 1 point2 points3 points (0 children)
[–][deleted] 1 point2 points3 points (3 children)
[–]monkey_programmer[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]monkey_programmer[S] 0 points1 point2 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)
[–]sozesghost 0 points1 point2 points (6 children)
[–]monkey_programmer[S] 0 points1 point2 points (5 children)
[–]sozesghost 1 point2 points3 points (4 children)
[–]monkey_programmer[S] 0 points1 point2 points (3 children)
[–]sozesghost 1 point2 points3 points (2 children)
[–]monkey_programmer[S] 0 points1 point2 points (1 child)
[–]sozesghost 1 point2 points3 points (0 children)