Sign in to follow this  
Code Sage

Best Way to Save Player Information?

Recommended Posts

Code Sage    100

What is the best way to save player information in Java? Some things that I would like to save are:

 

- Player equipment (wearing)

- Player inventory

- Position

- Levels

- EXP

 

 And maybe some Misc things like monsters killed, player deaths, total items found. Any ideas on what would be the best way to do this?

Share this post


Link to post
Share on other sites
Ashaman73    13715

The best way would be to save it to a persistent storage....

 

Well, if you need more specific information, you need to give us more information first. What are your requirements:

- Multiple players, massive ?

- Distributed access ?

- Performance ?

- Flat data or object net ?

 

The most simple way would be to write it to a property file. More advanced, but although complexer, would be the usage of a xml file. Nevertheless, keep it human readable (don't just serialize your object to a stream). Eventually, if performance and distributed access is necessary, I would look into using a DB (eg. if you target a browser game, MMORGP etc.).

Share this post


Link to post
Share on other sites
Code Sage    100

The best way would be to save it to a persistent storage....

 

Well, if you need more specific information, you need to give us more information first. What are your requirements:

- Multiple players, massive ?

- Distributed access ?

- Performance ?

- Flat data or object net ?

 

The most simple way would be to write it to a property file. More advanced, but although complexer, would be the usage of a xml file. Nevertheless, keep it human readable (don't just serialize your object to a stream). Eventually, if performance and distributed access is necessary, I would look into using a DB (eg. if you target a browser game, MMORGP etc.).

It is just going to be a 2D single player game. All loading will be done before they are thrown in game and saving will be done when they quit and or manually save the game. I am not sure what you mean by distributed access other than maybe more than 1 person saving and loading to the same file at once and in that case only 1 at a time. I also don't know what the difference between flat data and object net is. Could it be that object net is saved over a server and flat data is saved locally? If so I will be saving all the settings locally, however it would be great if users couldn't easily access the file and give themselves whatever they want.

Share this post


Link to post
Share on other sites
Ashaman73    13715

Well, in this case I would sugguest to start small with a simple property file (which is just a simple map in a textfile), Java have some decent support to manage properties files. Take a look at the link I've already posted. A property file will eventually look liks this

name=Conan
position_x=10.223
position_y=23.07
position_z=-232.98
exp=1002
level=2
equipment_helm=leahter_cap
equipment_belt=
...
inv_slot_1=apple
inv_slot_2=steel_sword
..

The advantages are, that they are easy to read and edit, that you don't need to handle different versions, don't need a definition file etc. A really easy start.

Share this post


Link to post
Share on other sites
Code Sage    100

Alrighty thanks for the information, really appreciated. Just out of curiosity when would you recommend I use XML? I realize that is may be tougher to get setup, but what are the advantages of using it if property files work just fine?

Edited by Code Sage

Share this post


Link to post
Share on other sites
arka80    1959

XML adds semantic data, which can be easily used by a program to do advanced stuff (to really know what it is reading).

For the sake of a property list I wouldn't use it.

Share this post


Link to post
Share on other sites
Ashaman73    13715

With xml you can get objects in relation, eg instead of this

name=Conan
position_x=10.223
position_y=23.07
position_z=-232.98
exp=1002
level=2
equipment_helm=leahter_cap
equipment_belt=
...
inv_slot_1=apple
inv_slot_2=steel_sword
..

you could write this

<character name=Conan exp="1002" level="2" >
  <position x="10.223" position_y="23.07" position_z="-232.98" />
  <eqquipment>
     <slot id="helm"><item id="leather_cap" /></slot>
     <slot id="belt"/>
  </eqquipment>
  <inventory>
     <slot id="1"><item id="apple" /></slot>
     <slot id="2"><item id="steel_sword" /></slot>
  </inventory>
</character>

This way you can add more structure to the data you save. You can define a data schema, you have auto-check at hands, it is still human readable etc. But the handling gets more complex on the other hand.

Share this post


Link to post
Share on other sites
Code Sage    100

Oh ok I see, in that case properties does seem like the most viable option for what I am doing right now. Thanks for all the replies, you guys are great! :)

Share this post


Link to post
Share on other sites
wicked357    2424

Or you could always go the route of JSON I have always preferred this over XML any time of the day but of course that is web development and not games.

 

So your file example would look like this: (With some tweaks of course to show a little more flexibility more organization in mind.

{
    "character":
    {
        "name":"Conan",
        "level":2,
        "experience":1002,
        "position":
        {
            "x":10.223,
            "y":23.07,
            "z":232.98
        },
        "equipment":
        {
            "head":"leather_cap",
            "waist":""
        },
        "inventory":
        {
            "slot_1":
            {
                "name":"apple",
                "quantity":1,
                "item_id":1
            },
            "slot_2":
            {
                "name":"steel_sword",
                "quantity":1,
                "item_id":2
            }
        }
    }
}
Edited by wicked357

Share this post


Link to post
Share on other sites
Code Sage    100

I have already implemented a simple save/load system with properties, but I am still interested in how easy JSON would be to implement? I realize that in JSON you could actually store String AND Integer values? With properties it seems as though only Strings could be stored... Also as you said a lot more organization with JSON, another thing about properties is that when your values get written to a .properties file they are not written in any particular order that I can tell all your information gets jumbled up. Now I new all this about properties before I fully implemented it, just for simplicity sake, being how difficult it is to implement XML.  As far as complexity would you say JSON is easier or harder than XML to implement?

Share this post


Link to post
Share on other sites
Sacaldur    932
There are Libraries for a huge variation of languages for both, XML and JSON. For the latter one, just take a look at json.org for a list of librearies for JSON parsing and serialisation. It's not hard to use these libraries, but it could require lot's of code. There are also more advanced libraries to not only parse and store XML or JSON, but they also analyse your data structures, e. g. by checking for annotations, and automatically generate and read XML or JSON, just from your data structures. At least in my opinion the problem about this automatic serialisation is the generated structure. Some libraries generate a child node for each list/array, containing the child nodes themselfes, even though there is only 1 list and all entries could be put into the element itself. A simple example:
<playlist name="My Playlist">
  <tracks>
    <track [...]/>
    <track [...]/>
  </tracks>
</playlist>
instead of
<playlist name="My Playlist">
  <track [...]/>
  <track [...]/>
</playlist>
But still: these libraries reduce the amount of code you have to write.

You can store any kind of Text in a properties file. This way, you're able to store numbers as well, but you need to store them as text - just as you have to do with every other text based file format - and you would have to parse them yourself - by calling Integer.parseInt().
Also: the order of the entries in a properties file shouldn't matter at all. You're reading the values using the keys, but you don't have to know the exact order.

Besides Properties, XML, and JSON, there are INI files as well. They are comparable with Properties files, but have some more features (e. g. sections) by default, but the set of features varies depending on the implementation.
And you could use your own file format, but since you would need to implement the file parsing yourself, you should just stick to one of the mentioned file formats.


If you don't know, which format to use for your files: just stick to what you already have (properties I guess) until you encounter some seriour issues regarding the format. Then you'll be able to make a better decision, since you know what problem you'll have to solve with another.

Share this post


Link to post
Share on other sites
Code Sage    100

There are Libraries for a huge variation of languages for both, XML and JSON. For the latter one, just take a look at json.org for a list of librearies for JSON parsing and serialisation. It's not hard to use these libraries, but it could require lot's of code. There are also more advanced libraries to not only parse and store XML or JSON, but they also analyse your data structures, e. g. by checking for annotations, and automatically generate and read XML or JSON, just from your data structures. At least in my opinion the problem about this automatic serialisation is the generated structure. Some libraries generate a child node for each list/array, containing the child nodes themselfes, even though there is only 1 list and all entries could be put into the element itself. A simple example:

<playlist name="My Playlist">
  <tracks>
    <track [...]/>
    <track [...]/>
  </tracks>
</playlist>
instead of
<playlist name="My Playlist">
  <track [...]/>
  <track [...]/>
</playlist>
But still: these libraries reduce the amount of code you have to write.

You can store any kind of Text in a properties file. This way, you're able to store numbers as well, but you need to store them as text - just as you have to do with every other text based file format - and you would have to parse them yourself - by calling Integer.parseInt().
Also: the order of the entries in a properties file shouldn't matter at all. You're reading the values using the keys, but you don't have to know the exact order.

Besides Properties, XML, and JSON, there are INI files as well. They are comparable with Properties files, but have some more features (e. g. sections) by default, but the set of features varies depending on the implementation.
And you could use your own file format, but since you would need to implement the file parsing yourself, you should just stick to one of the mentioned file formats.


If you don't know, which format to use for your files: just stick to what you already have (properties I guess) until you encounter some seriour issues regarding the format. Then you'll be able to make a better decision, since you know what problem you'll have to solve with another.

 

Wow, thanks for your insight! I believe I will stick to properties as you said, until issues start popping up and I am more or less forced to go with something else be it XML or JSON. However I still plan to research these two, so when the time comes I will be prepared. It is also very true that the order of the property files doesn't make a difference seeing as how you actually access the data, but I guess it just makes my OCD itch seeing things out of order haha.

Share this post


Link to post
Share on other sites
Nicholas Kong    1535

From my experience, I would suggest implementing the load/save system should be the highest priority on the to-do list. This approach makes the code implementation more readable. 

 

I remember implementing the load save feature for my 2D rpg. But that was the last feature I implemented of all the many things I coded for the game. It took me 3-4 days to implement the feature and on top of having duplicated code. 

Share this post


Link to post
Share on other sites
Code Sage    100

I agree, I actually started with player customization, which I don't know if that was the best idea but I don't really care. Now it only seemed logical to "save" all the players choices regarding their customization. Saving and loading is a very important aspect when it comes to an RPG probably why The Legend of Zelda was one of if not the first game to actually have saving of your game.

 

PS: I realize LOZ is actually known to be an Action Adventure but many RPGs today adopted mechanics from LOZ gameplay.

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

Sign in to follow this