I am trying to figure out the best and most secure way to store game related data for my upcoming project. Right now I am focusing on everything but player specific data. Let me try to give you an idea as to what I am trying to talk about. I am designing a RPG that is similar in style to the Diablo series. Some examples of data I would be storing would include item components (since I would have the same dynamic creation of item as diablo), npcs, mobs/enemies, skills/spells, etc…
My first idea was to store this information inside of a XML file but then though that pulling data from that would not be very optimized. Then I though of storing it inside of a database file and querying data from that but I am not sure that is very secure. Is there a standard way of doing this besides creating a custom file format like diablo did with the mpq file (which were cracked anyways)?
I think your best balance between ease of coding, and resistance to casual value tweaking is to save it as a binary file.
It is actually quite easy with .NET (as in 20 or so lines), just remember to use a custom SerializationBinder as the namespace of your assemblies compiled by Unity changes all the time, and that can mess up type references with binary serialization. A few searches on google should help you figure out how to do that.
Read speed of XML shouldn’t be a huge problem if that is your reason for shying away from it, as you should be pre-loading most of your gameplay data anyway.
You might also want to consider if modding will be part of the fun for your players, as it was for the Civ series.
This is assuming your game will be single player, otherwise things get a lot more complex, of course.
I guess I will take a look at saving the XML file as a binary file.
While I would like to make modding the game possible, I think adding multiplayer support (like 4-8 max players) is more important and not sure how I would want to handle both modding and multiplayer at this time.
In most cases you can serialize the class to binary, no need for the XML “middle man” step if you are going to use binary. Google BinaryFormatter and SerializationBinder.
For multiplayer however, the item stats, behaviour attributes and also any player-specific stats must be stored/used on the server, not the client. Never trust the client.
In that case, you can store the data how you want, with a database being the more obvious choice.
I’m currently working on a project to create a generic platform for TBS, Collaborative RPG, or really any type of game that is able to run in high-latency environments relying on RPC’s with an authoritative server.
One of the objectives is to simplify save games, but I’m also trying to create a single solution that takes care of the GUI’s that deal with single and network player, profile management, audio/visual/control settings and so on.
Currently I’m serializing out to XML (Human readable, good for testing) and I simply split it into a file for profiles, a file that lists all save games, and then individual save game files so there isn’t any contention on one main xml file, but eventually I aim to make the serialization process more of a “plugin” interface that allows for encrypted binary formats or object relational mapping to a standard database schema.
Well as far as serializing the player object to a binary data file, that seems like it should work, hope that making an object Serializable does not add a ton of overhead as I will have to do that for quite a few objects but I guess I can do some speed testing on that at some point plus I am really only serializing and deserializing when they save or load the game. I also know that without my code the player will not be able to deserialize the file.
Aside from whether or not XML is the best format to store game data like items, enemies, etc… as far as pulling only what I need to the level they are on I also don’t want it to be an easy process for them to modify the game data (unless I give them the tools) so I guess I still need to do a little more research on that.
You can just add a encryption decorator to your output/input streams if you’re writing out to XML and you want to avoid tampering, have a look at the standard .NET CryptoStream class.
Could probably add that onto I binary output stream too… or I hope so anyway.
Depending on how you store you game state data it can be easy or difficult, personally I isolate my state information in an easy to serialize class avoiding direct object references, so so serializing and de-serializing is as easy as:
// Serialize the current game
XmlSerializer gameSerializer = new XmlSerializer(typeof(GameState));
TextWriter writeStream = new StreamWriter(Application.dataPath + "/Game" + gameInfo.reference + ".xml");
gameSerializer.Serialize(writeStream, game);
writeStream.Close();
// Deserialize the current game state
XmlSerializer gameSerializer = new XmlSerializer(typeof(GameState));
FileStream readStream = new FileStream(Application.dataPath + "/Game" + gameInfo.reference + ".xml", FileMode.Open);
game = (GameState) gameSerializer.Deserialize(readStream);
readStream.Close();
Well this is how I am planning to handle the player state. I am going to have a class called something like player and this class is going to hold all the information about the player including name, level, items (which will be an array of item objects), skills ( which will be an array of skill objects), and so forth. I think with the type of game I am building, my player is going to need to reference other objects. If you have a better suggestion on how to store my player data, I am open to other people’s suggestions.
I agree with hogus about creating specific container classes for serialization, nice and clean.
It will save you a lot of grief if you come up with a unified way to identify objects without direct pointer references. Not just for serialization, but for a lot of other aspects of the game.
How you do it is up to you and your code structure, but I highly recommend it. A bit of work, but it pays off big time in the end with projects of this size.
Well let’s take items as an example for this. The only other way I see being about to store this data (or any other data like this) without storing it as an object or list of objects in the player class would be a string. For example, if I want to store an item that was a 2 handed sword with the name of “Eagle Sword of Truth” that added 2 to strength and 3 to wisdom I could store it as a string like:
“2hdswdeagswdtrustr2wis3”
Doing something like this, I only see the downsides and don’t see any positives.
One downside I see is that it does not seem that it would be a very flexible solution. If I decide somewhere down the road I need to make a major change to the item class for whatever reason, I now might have to also make major changes in code the saves out and reads in this string format where if I stored it as a list of item objects, all I need to do it update the item class and it will be fine. Another downside, or possible downside, is that creating/parsing a string like that seems like it would be a very slow process. This is not a major concerns I would only be doing the creating/parsing during saving/loading a saved game. Also the added complexity with no real benefit gain is not good (but maybe I just don’t see the benefit yet).
Without some sort of custom string format, how else can I store objects in another object without a direct reference? I come from a background in c++ and in everything I have done, I was never told having objects in objects was a bad idea (except for coupling which I will explain next).
It is also important to note that in order to keep coupling down to a minimum, I am planning on using interfaces so for this example, instead of a list of item objects it would really be a list of objects that have the item interface.
I think you are misunderstanding me. When performing serialization, you need to build a way to re-connect complex object hierarchies, and other references. It can be a big help to do this in a flexible way early on, as it has uses for other things as well.
I am talking about inter-object links for the purposes of serialization, and also a system (perhaps an object database) to allow referring to a specific object without needing a pointer to its memory location.
There is nothing wrong with having object references within other objects, and I never said there was.
I am talking about re-building such objects from data, and also simplifying the construction of these objects at runtime for some other cases.
Having a solid object ID system allows you to save a player inventory list with the ID or name of the objects the player owns. When you re-load that file, you use the ID or name to look up the object, create an instance of it as needed and so on.
This can be done with object definition and stats files as well. One such file for a specific weapon could include its ID, stats, active abilities and behaviours, and so on.
This can also help with other runtime object construction, editors, etc. It can pay off to build it well early, rather than building it when you need serialization.
Yea, I did misunderstand you, sorry to put words into your mouth.
Once I figure out how to save my game data, then I should be able to serialize object like items as ids, just not sure quite yet if XML is the best route.
It doesn’t matter where you serialize to. The issue regarding object references is the same.
I’ve decided to just go ahead and solve the issue with XML. My logic is that if I can get saving and loading to work well from that perspective the internal object structure is correct, and if I keep the actual serialization portion simple in the process it should be easy to change to a different technology.
Of course also remember that you can use the fact that you are referencing by an identifier, rather than a memory location to your advantage if you decide to work on multiplayer games.
Remember you are not serializing objects as IDs, but you are referring to an object by its ID.
Think of a webmail application. As you know, when it shows your inbox, it (usually) isn’t downloading any of the messages, but it pulls the subject of the email and generates a hyperlink with the email message’s ID number.
So you click the link, and the server goes off to find the email by that ID. Folder move operations, marking as unread, replying, etc all use the ID number, and never the actual message data.
We are talking about a similar setup. Instead of saving all the details of each item with the player data, you save a list of the IDs of the items. Then you can “look up” the actual objects using the ID number when you need to.
To do this properly requires a good deal of forethought, which is why I suggest you work on it as one of your first components before you do any heavy gameplay coding. And as hogus said the end data format doesn’t really make any difference for this aspect. Get it working, and you can adjust it to nearly any format quite easily.
I hope that’s a bit more clear, good luck!
-Jeremy