ما شكل ملف التكوين يسمح للشوائب من otherfiles وراثة الإعدادات؟

StackOverflow https://stackoverflow.com/questions/1228545

سؤال

وأنا أكتب المتعدد C ++ على أساس لعبة.

وأنا بحاجة إلى تنسيق ملف مرن لتخزين معلومات حول charactors اللعبة.

وو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

ولقد تم البحث عن صيغة موجودة من قبل أن يفعل كل هذا، أو غير مماثلة، ولكن مع عدم وجود الحظ. أنا حريص على عدم إعادة اختراع العجلة ما لم يكن لدي، لذلك كنت أتساءل إذا كان أي شخص يعرف أي تنسيقات الملفات التكوين الجيدة التي تدعم هذه الميزات

هل كانت مفيدة؟

المحلول 4

وبعد الكثير من البحث وجدتها حلا جيدا جدا باستخدام لوا

ولوا اكتشفت كان مصمما أصلا كلغة ملف التكوين، ولكن بعد ذلك تطورت إلى لغة برمجة كاملة.

مثال

و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

وباستخدام وظيفة الطباعة في لوا لها أيضا من السهل لاختبار الإعدادات إذا كانت صحيحة. بناء الجملة هو ليس تماما لطيفا كما أود ذلك، ولكن نهايتها حتى ما أريد، وأنا لست ستعمل الاعتبار كتابة خارج أكثر قليلا.

ووباستخدام الشفرة هنا 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 دون الفواصل والاقتباس.

وقد ترغب في التحقق من بعض نوع من تمثيل الإطار القائم، لأنه يبدو هذا هو بالضبط ما نتحدث عنه. أن وصلات صفحة ويكيبيديا على عدد قليل من التطبيقات القائمة التي ربما يمكن أن تستخدم، أو إنشاء الخاصة بك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top