什么配置文件格式允许包含其他文件和继承设置?

发布于 2024-07-29 22:00:18 字数 713 浏览 1 评论 0原文

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

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

游戏角色通常不会共享相同的属性,或者使用基本格式

例如:

一种允许我做这样的事情的格式:

#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'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼前雾蒙蒙 2024-08-05 22:00:23

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

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

揪着可爱 2024-08-05 22:00:22

您可能想查看某种基于框架的表示,因为看起来这正是你说的是。 该维基百科页面链接到一些现有的实现,您可能可以使用这些实现,或者创建您自己的实现。

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.

瀟灑尐姊 2024-08-05 22:00:21

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

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

神妖 2024-08-05 22:00:20

JSON 是最简单的文件格式,具有成熟的库,您可以解释它来执行您想要的任何操作。

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

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 
    } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文