views:

151

answers:

4

I'm writing a Multiplayer C++ based game.

I need a flexible file format to store information about the game charactors.

The game charactors will often not share the same attributes, or use a basew

For example:

A format that would allow me to do something like this:

#include "standardsettings.config"  
//include other files which this file 
//then changes

FastSpaceship:
    Speed: 10  //pixels/sec
    Rotation: 5  //deg/sec

MotherShip : FastSpaceship //inherits all the settings of the Spaceship ship
    ShieldRecharge: 4
    WeaponA [ power:10,
              range:20,
              style:fireball]        

SlowMotherShip : MotherShip //inherits all the settings of the monther ship
    Speed: 4    // override speed

I've been searching for a pre-existing format that does all this, or is similar, but with no luck. I'm keen not to reinvent the wheel unless I have to, so i was wondering if anyone knows any good configuration file formats that support these features

A: 

You might want to check out some sort of frame-based representation, since it seems that's exactly what you're talking about. That wikipedia page links to a few existing implementations that maybe you could use, or create your own.

rmeador
+1  A: 

JSON is about the simplest file format around, has mature libraries, and you can interpret it to do anything you want.

{
    "FastSpaceship" : {
        "Speed" : 10,
        "Rotation" : 5 
    },
    "MotherShip" : {
        "Inherits" : "FastSpaceship",
        "ShieldRecharge" : 4,
        "WeaponA": {
            "Power": 10,
            "Range": 20,
            "style": "fireball"
        }
    },
    "SlowMotherShip": {
        "Inherits": "MotherShip",
        "Speed": 4 
    } 
}
mos
How would this work with including other files. I want something a bit like CSS i guess.I don't really want to have to work out all the includes the inheritance in user code (i'd like the library to do this work)Hence i could type something like;rotation = lookup("SlowMotherShip.Rotation"); and it would work out the Rotation value is 5.
docflabby
I guess I don't have a good answer for you, then. I don't know of any file format library that knows about hierarchical relationships between objects. That's not to say it doesn't exist (the open source world is far larger than my experience with it).I have written something like this before though. The format was simple JSON and the parser just knew how to deal with a few "keywords" like "inherits" (IIRC, we used the keyword "super").
mos
Ah I see, everthing seems to be close to what i want but not close enough. I guess i'm going to have to write my own library, or adapt an existing one;
docflabby
+1  A: 

YAML? It's like JSON without the commas and quotes.

AShelly
A: 

After alot of searching i've found a pretty good solution using Lua

Lua I found out was originally designed as a configuration file language, but then evolved into a complete programming language.

Example

util.lua

-- helper function needed for inheritance
function inherit(t)            -- return a deep copy (incudes all subtables) of the table t
  local new = {}             -- create a new table
  local i, v = next(t, nil)  -- i is an index of t, v = t[i]
  while i do
    if type(v)=="table" then v=inherit(v) end -- deep copy
    new[i] = v
    i, v = next(t, i)        -- get next index
  end
  return new
end

globalsettings.lua

require "util"
SpaceShip = {
    speed = 1,
    rotation =1
}

myspaceship.lua

require "globalsettings"  -- include file

FastSpaceship = inherits(SpaceShip)
FastSpaceship.Speed = 10
FastSpaceship.Rotation = 5

MotherShip = inherits(FastSpaceship)
MotherShip.ShieldRecharge = 4
ShieldRecharge.WeaponA = {
        Power = 10,
        Range = 20,
        Style = "fireball"

SlowMotherShip = inherits(MotherShip)
SlowMotherShip.Speed = 4

Using the print function in Lua its also easy to test the settings if they are correct. Syntax isn't quite as nice as I would like it, but its so close to what I want, i'm not gonna mind writing out a bit more.

The using the code here http://windrealm.com/tutorials/reading-a-lua-configuration-file-from-c.php I can read the settings into my C++ program

docflabby
If you going to do that you could just include Python, Ruby, Javascript or any other interpreter into your program. Be careful of security, if the language has an execute() function you don't want config files that can format c:
Martin Beckett
Thats true, thinking about it a scripting language starts to make more sence rather than a static library as this is being used for the level design. The security problem is something I hadn't thought of, I've heard it can be run in a sandbox mode, i shall look into this.
docflabby