views:

478

answers:

9

I need a way to save and load user configurations for a game (controls, graphics, etc.) to a file. I want to do this in a cross-platform manner, and if I have to use libraries, I want them to have a simple enough syntax. How do I save and load config files, and what format should I use? XML and INI seem popular but all of the parsers I have found have confusing syntax, and I have no clue where to start to get my own parser going.

By the way, the config files probably won't get too complex, so something lightweight and fast would be best.

An aside - I was thinking about using scripting to define things like items, but would a markup language like xml be better or a scripting language (like angelscript, which is what I'm using) be better?

A: 

A lot of games have stored these settings in a binary file. It would be faster than XML but nowadays I don't think it matters. Maybe explain what trouble you are having with XML files so we can help.

Jonathan Kaufman
dirkgently also suggested it. And there is already a link to Atwood's post about the angle tax bracket on the page... xml is clearly not the best that is available.
Matthieu M.
no reason to vote me down. I gave the answer at the same time. His wasn't up when I answered. Cruel.
Jonathan Kaufman
+2  A: 

Just to broaden your search, I'll point you to Jeff Atwood's own XML: The Angle Bracket Tax. Mentions YAML, JSON...

Don Wakefield
A: 

I need a way to save and load user configurations for a game (controls, graphics, etc.) to a file.

Games typically use proprietary formats for storing configuration information because they can often be misused (think of hacking up a config file to increase the number of spare lives you have or the ammo that you can employ). Such sensitive information is often written in binary and for good measure encrypted. If there is no such information that you need to store that can compromise the game-play, it is perfectly fine to go ahead with a text-based/markup based/human readable format. Binary formats have their own set of issues w.r.t platforms etc (lookup SO for posts in this regard).

I was thinking about using scripting to define things like items, but would a markup language like xml be better or a scripting language (like angelscript, which is what I'm using) be better?

XML is definitely a good choice keeping in view my remarks in the previous section. It is human readable, a number of parsers are freely available (and also it is easy to write one) etc. Scripting is a different beast altogether. Do you really need to have a script and control structures and functions? This is a design decision again, so its upto you to decide.

There is no one right answer. To start off, a text/XML format looks fine to me. But with time and increased complexity of the game you may need to move towards a DSL and binary form.

dirkgently
XML and the angle tax bracket? There are many others human-readable format, xml is definitely not my first choice there > json is lighter.
Matthieu M.
+2  A: 

What about to make a C++ class that represent your configuration and then serialize it? Look for example at boost serialization.

http://www.boost.org/doc/libs/1%5F40%5F0/libs/serialization/doc/index.html

Gaetano Mendola
Please, don't use serialization for long-term solutions.
Matthieu M.
+1  A: 

You might be interested in the Message class that is part of my networking library. It lets you store any kind of data conveniently, with as much or as little hierarchy/structure as you want to include. The data is serializable to a platform-neutral binary format. There are APIs for C, C++, Java, Python, and C#, and it's easy to use.

I've been using it to store fairly complex project files for years, and it's worked great for me.

Jeremy Friesner
+2  A: 

I am using Lua which is light weight and works well as a configuration language. As years went by I started moving more and more out to the Lua scripts until everything but the core engine was coded in Lua. In the latest iteration even the game dynamics are coded in Lua using callbacks with very little hit on performance. I'm not saying you should do this but Lua will let you grow and is very easy to implement in c/c++.

Nick
A: 

This answer is more of a question, but anyway. Is SQL database (like SQLite3) suitable for this purpose? If yes, than how could it be done?

Yelonek
sqlite3 is suitable. Though really, its more useful as a file format for storing complex data that for configuration. You need to be careful the schema will allow future extensions without breaking backwards compatibility (not difficult, just keep it simple).
Stuart
A: 

IniParser - Easy to use, standard ini syntax, C but trivial to wrap in a class.

Stuart
+1  A: 

Google Protocol Buffers

There is a long standing confusion I think, between serialization and messaging.

Serialization

Is generally short-lived and involves passing around objects.

As stated in the article, COM and CORBA are well-known examples of its uses.

Messaging

Is about communication between different entities.


The problem is that many people, when they see a messaging problem simply think: serialize and be done with it.

But this is not adapted.

Serialization of an object means being able to 'encode' the object, and restore it... what happens if suddenly you change your ways and what was one object is now 2, how do you deal with backward compatibility ?

The problem here is that if Serialization works pretty well with stable model, any change of the model may be a pain.

Messaging on the other hand decorrelates the message encoding and decoding from the current model, therefore, changes to the model just have to be treated in the encoding and decoding of the message, and the model is freed from the burden of backward compatibility.


Now for a configuration file, you probably want backward compatibility. Then it is in your best interest to elect a Messaging solution.

I suggest Google Protocol Buffers, because it's sound and proven.

  1. Forward and Backward compatibility is easy handled
  2. May generate either human readable or binary format
  3. A same message can be encoded / decoded in a wide variety of languages

I don't have to present why 1. is important. 2. means that you can simply edit the file manually to tweak it, 3. means that you can easily code a GUI or a checking script.

Matthieu M.
Sorry for getting to this so late... school was killer. Anyway, this is quite the find. I just may use this.
sonicbhoc