I am trying to figure out how I can re-load a bunch of Box2D joints/bodies but so far all attempts seem to lead to stack overflow due to excessive recursion.
I have quite a few classes but here is the Player class:
/**
*
* @author Stephen Gibson
*/
public class Player {
/** The players profile */
private Profile profile;
/** Json serialization */
private transient Json json;
public Player() {
json = new Json();
json.setOutputType(OutputType.json);
if (!Gdx.files.external("saves/profile.sav").exists()) {
Gdx.app.log("Warning", "No Profile found, creating new...");
profile = new Profile();
Spaceship ship = new Spaceship(new Hull1(), new WeldJoint(BaseScreen.world, 0), new WeldJointDef());
profile.setSpaceship(ship);
profile.setCredits(50);
} else {
load();
}
}
/** Save the players current profile */
public void save() {
Gdx.app.log("Info", "Saving Profile...");
// Check if player has reached a new all high altitude
if (profile.getSpaceship().getAltitude() > profile.getHighestAltitude()) {
profile.setHighestAltitude(profile.getSpaceship().getAltitude());
}
Gdx.files.external("saves/profile.sav").writeString(
Base64Coder.encodeString(json.toJson(profile)), false);
System.out.println(json.prettyPrint(profile));
}
/** Load a players profile */
public void load() {
Gdx.app.log("Info", "Loading profile...");
String temp = Base64Coder.decodeString(Gdx.files.external(
"saves/profile.sav").readString());
profile = json.fromJson(Profile.class, temp);
}
public Profile getProfile() {
return profile;
}
public void setProfile(Profile profile) {
this.profile = profile;
}
}
Basically what happens is, when the game is shut down in anyway the save() method is called the the entire Profile class is serialized to a file. This works just fine and I can successfully save and load profiles upon closing and starting. The problem is I want to save the Spaceship that is currently assigned in the Profile class:
/**
*
* @author Stephen Gibson
*/
public class Profile {
/** The players spaceship */
private Spaceship spaceship;
/** The players current credits */
private int credits = 0;
/** The highest altitude reached */
private int highestAltitude = 0;
public Profile() {
}
The spaceship class is not complicated but it is made up of a couple parts, the Hull and Engine. Which are held together by joints.
/**
*
* @author Stephen Gibson
*/
public class Spaceship extends Entity {
/** The altitude reached in meters? */
private int altitude = 0;
private HullBase hullBase;
private transient WeldJointDef jointDef;
private transient WeldJoint hullJoint;
/** Constructor used for when the a existing profile is loaded */
public Spaceship() {
System.out.println("Test");
}
/** Constructor used when creating a new profile */
public Spaceship(HullBase hull, WeldJoint hullJoint, WeldJointDef jointDef){
this.hullBase = hull;
this.hullJoint = hullJoint;
this.jointDef = jointDef;
}
@Override
public void process() {
super.process();
if (getBody() != null)
altitude = (int) body.getPosition().y;
}
What is the best way to save these joints?
Also is having multiple constructors the way to go when loading from json? Since json needs a no arg constructor (from what the error tells me) to initialize the object?