Mutating Data Structures

Like everyone else, I'm writing a multiplayer game. This will differ from typical games in that it uses a play-by-email (PBEM) philosophy - a person can plan his next move, submit it to a central server before the deadline, and after all the moves have been accepted, the server will process them and return the results to the players.

However I have a problem that probably has cropped up in other multiplayer formats, so I'm looking for some suggestions and/or the benefit of experience here.

I can define an arbituary player class which in itself is an abstract data type, that is, it stores varies pieces of information such as the strength, speed, damage potential and ability to resist damage. My original idea was to make that class serializable. If I decide to expand the playing field to three players, five players, whatever, I simply call new player( <player unique id> ) and get an object with all of the relevant attributes for that player.

However, if I decide later to add a new attribute to the basic player class - for example, regeneration/healing rate, I suspect that it would render all previously serialized objects invalid because the class no longer matches the object. Is this right?

I would like to avoid explicitly writing out each attribute and reading back in those attributes for several reasons, for example, I want to hide the values from prying eyes and yet I don't want to go through the hassle of encryption/decryption.

I essentially would like to be able to add attributes as I go along, have all previous "saved" objects remain valid, and ideally, if I delete an attribute from the class, the corresponding value will simply be ignored and not rewritten back to the save file when the object is next saved.

Any ideas?

[1805 byte] By [TheDavida] at [2007-9-29 6:46:59]
# 1

Just a thought, could you use a container class to store your player parameters?

For example, instead of hard coding all the parameters into the Player object, give the Player object a Map called params. Use the map to bind String names to the associated value. Thus, you could add a regeneration rate down the road by simply adding the line

params.add("Regen_Rate", new Integer(25));

When the Player object is deserialized, whatever is in the map will come with it, but since the attributes are stored loosely coupled via the map, deserialization will not be broken when new attributes are added.

Just my two cents,

Ryan

DeltaHat2002a at 2007-7-14 20:47:51 > top of Java-index,Other Topics,Java Game Development...