Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  World save system  (Read 803 times)
0 Members and 1 Guest are viewing this topic.
Offline brian22950

Junior Newbie





« Posted 2012-08-27 22:42:37 »

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
Offline DrewLols

Junior Member


Projects: 1


Noob going through metamorphosis...


« Reply #1 - Posted 2012-08-28 02:12:19 »

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...


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
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...

1  
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.

Did you know that 90% of statistics are wrong?
Offline brian22950

Junior Newbie





« Reply #2 - Posted 2012-08-28 10:17:37 »

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) ?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jonjava

JGO Knight


Medals: 32



« Reply #3 - Posted 2012-08-28 22:08:48 »

http://www.tutorialspoint.com/java/java_serialization.htm

Offline DrewLols

Junior Member


Projects: 1


Noob going through metamorphosis...


« Reply #4 - Posted 2012-08-29 02:13:30 »

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.

1  
2  
3  
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.

Did you know that 90% of statistics are wrong?
Offline brian22950

Junior Newbie





« Reply #5 - Posted 2012-08-29 10:41:14 »

Ok, thank you for your quick answers !!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
hobbles (7 views)
2013-05-22 00:54:55

cubemaster21 (74 views)
2013-05-17 21:29:12

alaslipknot (86 views)
2013-05-16 21:24:48

gouessej (115 views)
2013-05-16 00:53:38

gouessej (110 views)
2013-05-16 00:17:58

theagentd (121 views)
2013-05-15 15:01:13

theagentd (110 views)
2013-05-15 15:00:54

StreetDoggy (155 views)
2013-05-14 15:56:26

kutucuk (177 views)
2013-05-12 17:10:36

kutucuk (175 views)
2013-05-12 15:36:09
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.084 seconds with 20 queries.