他のファイルのインクルードと設定の継承を許可する構成ファイル形式は何ですか?
-
22-07-2019 - |
質問
マルチプレイヤー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
多くの検索の後、 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 ++プログラムに読み込むことができます
他のヒント
JSONは、最も単純なファイル形式であり、成熟したライブラリを備えています。これを解釈して、必要な処理を実行できます。
{
"FastSpaceship" : {
"Speed" : 10,
"Rotation" : 5
},
"MotherShip" : {
"Inherits" : "FastSpaceship",
"ShieldRecharge" : 4,
"WeaponA": {
"Power": 10,
"Range": 20,
"style": "fireball"
}
},
"SlowMotherShip": {
"Inherits": "MotherShip",
"Speed": 4
}
}
YAML ?カンマや引用符のないJSONのようなものです。
ある種のフレームベースの表現を調べてみてください。それがまさにあなたが話していることです。そのウィキペディアのページは、おそらくあなたが使用したり独自に作成したりできる既存の実装にリンクしています。