2009-08-04 62 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。

相關問題