1

我正在编写一个基于 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

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

4

4 回答 4

2

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

{
    "FastSpaceship" : {
        "Speed" : 10,
        "Rotation" : 5 
    },
    "MotherShip" : {
        "Inherits" : "FastSpaceship",
        "ShieldRecharge" : 4,
        "WeaponA": {
            "Power": 10,
            "Range": 20,
            "style": "fireball"
        }
    },
    "SlowMotherShip": {
        "Inherits": "MotherShip",
        "Speed": 4 
    } 
}
于 2009-08-04T17:00:28.807 回答
1

亚美尔?它就像没有逗号和引号的 JSON。

于 2009-08-04T17:48:06.790 回答
0

经过大量搜索后,我找到了一个使用Lua的非常好的解决方案

我发现 Lua 最初被设计为一种配置文件语言,但后来演变成一种完整的编程语言。

例子

实用程序.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

全局设置.lua

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

我的宇宙飞船.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++ 程序

于 2009-08-05T16:03:35.563 回答
0

您可能想查看某种基于帧的表示,因为这似乎正是您在谈论的内容。该维基百科页面链接到一些现有的实现,您可能可以使用或创建自己的实现。

于 2009-08-04T16:44:35.000 回答