2009-08-04 61 views
1

我在写一个基于多人C++的游戏。什么配置文件格式允许包含其他文件和设置的继承?

我需要一个灵活的文件格式来存储关于游戏角色的信息。

游戏charactors往往不能共享相同的属性,或使用basew

例如:

,让我做一些像这样的格式:

#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 

我我一直在寻找一种预先存在的格式,可以完成所有这些,或者是相似的,但是没有运气。我很想不要重新发明轮子,除非必须,所以我想知道是否有人知道任何支持这些功能的良好配置文件格式

回答

-1

搜索的很多之后,我已经找到了使用Lua

Lua的一个很好的解决方案,我发现原本被设计为一个配置文件语言,但后来演变成一个完整的编程语言。

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 

使用在Lua打印功能也其易于测试该设置,如果他们是正确的。语法并不像我想要的那么好,但它与我想要的非常接近,我不会介意多写点东西。

的利用代码在这里http://windrealm.com/tutorials/reading-a-lua-configuration-file-from-c.php我可以读取设置成我的C++程序

0

您可能想查看某种frame-based表示法,因为它似乎是正是你在说什么。该wikipedia页面链接到一些现有的实现,也许你可以使用,或创建自己的。

1

JSON是关于简单的文件格式左右,具有成熟的图书馆,你可以把它解释你想要的任何东西。

{ 
    "FastSpaceship" : { 
     "Speed" : 10, 
     "Rotation" : 5 
    }, 
    "MotherShip" : { 
     "Inherits" : "FastSpaceship", 
     "ShieldRecharge" : 4, 
     "WeaponA": { 
      "Power": 10, 
      "Range": 20, 
      "style": "fireball" 
     } 
    }, 
    "SlowMotherShip": { 
     "Inherits": "MotherShip", 
     "Speed": 4 
    } 
} 
+0

如何将与包括其他文件,这项工作。我想要的东西有点像CSS。 我真的不希望有制定出所有包含在用户代码继承(我想在图书馆做这个工作) 因此我可以键入类似; rotation = lookup(“SlowMotherShip.Rotation”);并且它会计算出旋转值为5. – Kingsley 2009-08-05 13:32:54

+0

那么我想我没有很好的答案。我不知道任何知道对象间层次关系的文件格式库。这并不是说它不存在(开源世界远远大于我的经验)。 虽然我写过类似的东西。格式很简单,解析器只知道如何处理一些像“继承”(IIRC,我们使用关键字“super”)的“关键字”。 – moswald 2009-08-05 14:11:21

1

YAML?这就像没有逗号和引号的JSON。

相关问题