Best Way to Save Player Information?

Started by
12 comments, last by Code Sage 9 years, 2 months ago

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?

Advertisement

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

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.

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.

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?

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.

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.

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! :)

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

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?

This topic is closed to new replies.

Advertisement