What configuration file format allows the inclusions of otherfiles and the inheritance of settings?

StackOverflow https://stackoverflow.com/questions/1228545

Question

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

Was it helpful?

Solution 4

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

OTHER TIPS

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

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top