【问题标题】:What configuration file format allows the inclusions of otherfiles and the inheritance of settings?什么配置文件格式允许包含其他文件和继承设置?
【发布时间】:2010-11-16 17:53:07
【问题描述】:

我正在编写一个基于 C++ 的多人游戏。

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

游戏角色通常不会共享相同的属性,或使用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

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

【问题讨论】:

    标签: c++ configuration-files file-format


    【解决方案1】:

    JSON 是最简单的文件格式,有成熟的库,你可以解释它来做任何你想做的事情。

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

    【讨论】:

    • 这将如何与包含其他文件一起工作。我想我想要一些像 CSS 的东西。我真的不想计算出用户代码中的所有包含继承(我希望库来完成这项工作)因此我可以输入类似的内容;旋转 = 查找(“SlowMotherShip.Rotation”);它会计算出 Rotation 值为 5。
    • 那我想我没有给你一个好的答案。我不知道有任何文件格式库知道对象之间的层次关系。这并不是说它不存在(开源世界比我的经验要大得多)。我以前写过类似的东西。格式是简单的 JSON,解析器只知道如何处理一些“关键字”,如“继承”(IIRC,我们使用关键字“super”)。
    • 啊,我明白了,一切似乎都接近我想要的,但还不够接近。我想我将不得不编写自己的库,或者修改现有的库;
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      您可能想查看某种frame-based 表示,因为这似乎正是您所说的。该维基百科页面链接到一些现有的实现,您可能可以使用或创建自己的实现。

      【讨论】:

        【解决方案4】:

        经过大量搜索,我找到了一个很好的解决方案,使用 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++程序

        【讨论】:

        • 如果你打算这样做,你可以在你的程序中包含 Python、Ruby、Javascript 或任何其他解释器。注意安全,如果该语言有一个 execute() 函数,你不希望配置文件可以格式化 c:
        • 没错,考虑到它,脚本语言开始变得更有意义,而不是静态库,因为它被用于关卡设计。安全问题是我没有想到的,听说可以在沙盒模式下运行,我会研究一下。
        猜你喜欢
        • 2015-06-29
        • 1970-01-01
        • 2010-10-03
        • 1970-01-01
        • 2012-01-12
        • 2018-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多