Hello, i’m new on the forum :p,
I want to do a world save system but i have no idea of how to do this.
I use a sprite system for my game.
Can you help me ?
Thx,
Brian22950
PS : I’m french
Hello, i’m new on the forum :p,
I want to do a world save system but i have no idea of how to do this.
I use a sprite system for my game.
Can you help me ?
Thx,
Brian22950
PS : I’m french
Sort answer: Learn how to use the Serializable interface in Java
Long answer:
I can only assume that you want to implement save data, correct? I recommend that you learn how to implement the Serializable interface. It’s basically a way to write objects to a file. You could make a class that takes in properties and retrieves properties like…
SaveData sd = SaveData.getSaveData("saveFile.txt") // Uses an ObjectInputStream to read an object from a file
if(sd == null) // If the getSaveData() method failed, create your save data object
{
sd = new SaveData();
}
sd.addProperty("hasBanana", "true"); // Adds a property to your save data object. The first argument must be a string to identify
// what you want, and the object you put in can be anything. In this case, it's the String value
// "true"
sd.save("saveFile.txt"); // Uses an ObjectOutputStream to write sd to a file
if(sd.getProperty("hasBanana").equals("true"))
{
eatBanana();
}
Of course, you’ll have to write the SaveData class yourself, but if you wanted to save data for more complicated objects, it is possible. For instance…
sd.addProperty("inventory", playerInventory); // You can write in any object so long as it implements the Serializable interface.
Keep in mind that it takes some work to property implement the Serializable interface. Basically, you’ll be adding many Serializable objects that you want to save to an instance of the Serializable class, SaveData. Then, you can easily write a SaveData to a file.
It is not THAT simple and you should definitely look up how to property use the Serializable interface.
Just remember that you need an ObjectOutputStream to write an Object to a file, and ObjectInputStream to read an Object from a file, and all object being written must implement the Serializable interface.
Oh, and just so you know, there are other ways to write Objects to a file besides using java’s built in Serializable interface. Just felt like mentioning this so no one on this site yells at me.
ok, that is i wanted to know, thx, but how can i do to save some informations about a block(like a text for a sign block) ?
Can’t really tell you EXACTLY how you’ll store and retrieve save data, but you should know that the String class implements the Serializable interface. You can just store the string text in the Serializable object and retrieve it later.
sd.addProperty("AwesomeText", "Read signs. It's good for you!"); // Stores property
Sign sign = new Sign(); // Makes sign
sign.setText(sd.getProperty("AwesomeText")); // Assigns text
Just think of the save data as a machine that you can put stuff in, and take stuff out. If your entire level was Serializable, you COULD write the entire level to the SaveData. I wouldn’t recommend this because it can get a little inflexible.
Ok, thank you for your quick answers !!