Hello Guest

Author Topic: Issue with DataSaver  (Read 3149 times)

0 Members and 1 Guest are viewing this topic.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Issue with DataSaver
« on: July 19, 2015, 10:31:37 PM »
Havent posted this in Bugs because im not sure entirely yet.  Here is my code, which is loaded before the game window is created:

Code: [Select]
private static void LoadSettings()
        {
            Global.gameSettings = new DataSaver(Global.PATH_SETTINGS);

            if (!Directory.Exists(Global.PATH_SETTINGS))
                Directory.CreateDirectory(Global.PATH_SETTINGS);

            if (File.Exists(Global.PATH_SETTINGS + Global.FILE_SETTINGS))
            {
                Console.WriteLine("Loading settings file: " );
                Global.gameSettings.Import(Global.FILE_SETTINGS);
                Global.screenWidth = Global.gameSettings.GetInt(SettingType.SCREENWIDTH);
                Global.screenHeight = Global.gameSettings.GetInt(SettingType.SCREENHEIGHT);
                Global.fullscreen = Global.gameSettings.GetBool(SettingType.FULLSCREEN);
            }
            else
            {
                Console.WriteLine("Creating new settings file");
                Global.gameSettings.SetData(SettingType.SCREENWIDTH, Global.screenWidth);
                Global.gameSettings.SetData(SettingType.SCREENHEIGHT, Global.screenHeight);
                Global.gameSettings.SetData(SettingType.FULLSCREEN, Global.fullscreen);
                Global.gameSettings.ExportMode = DataSaver.DataExportMode.Config;
                Global.gameSettings.Export(Global.FILE_SETTINGS);
            }
        }

These are the constants and enum i use:

Code: [Select]
enum SettingType
    {
        SCREENWIDTH,
        SCREENHEIGHT,
        FULLSCREEN
    }
Code: [Select]
public static int screenWidth = 800;
        public static int screenHeight = 600;
        public const int FRAMERATE = 60;
        public static bool fullscreen = false;
        public static DataSaver gameSettings;
        public const string PATH_SETTINGS = "../../Data/";
        public const string FILE_SETTINGS = "settings.avh";
And this is the output in "settings.avh":
Code: [Select]
AntiVirusHeroes.SettingType.SCREENWIDTH=800
AntiVirusHeroes.SettingType.SCREENHEIGHT=600
AntiVirusHeroes.SettingType.FULLSCREEN=False

Now the problem is, after "gameSettings" has imported the file into the "data" dictionary located in DataSaver, It throws a "KeyNotFoundException" in this method:
Code: [Select]
public int GetInt(string key) {
            return int.Parse(data[key]);
        }
The key its trying to pair is: "AntiVirusHeroes.SettingType.SCREENWIDTH"

Ive tried a number of different things to get this to work. using different files, including default, using raw strings instead of enums, which has worked for me in the past when i used DataSaver with a single field of data to save.

Any ideas?

arcanebro

  • Member
  • Posts: 3
    • View Profile
Re: Issue with DataSaver
« Reply #1 on: July 20, 2015, 07:18:03 AM »
The reason why you messed up is that DataSaver.DataExportMode was not named properly for some reason -- this field also affects how the config file will be imported. So you just should set your settings ExportMode field to DataExportMode.Config like this:

Code: [Select]
private static void LoadSettings()
{
    Global.gameSettings = new DataSaver(Global.PATH_SETTINGS);
    Global.gameSettings.ExportMode = DataSaver.DataExportMode.Config;
...

...So your config will be readen and written as editable config file. You also may use Game.Instance.ConfigData instead of creating new DataSaver instance, but you are required to create new Game instance before using this field.

And there are just one small thing: you probably should concat path strings with System.IO.Path.Combine method instead of plain string concatenation.

Good luck with your game.

Fatesauce

  • Member
  • Posts: 26
    • View Profile
Re: Issue with DataSaver
« Reply #2 on: July 20, 2015, 01:05:23 PM »
Thanks. Thats a point i didnt consider.

Is there a rationale behind usinf System.IO.Combine? Or is it just better practice?

arcanebro

  • Member
  • Posts: 3
    • View Profile
Re: Issue with DataSaver
« Reply #3 on: July 21, 2015, 12:51:41 AM »
Thanks. Thats a point i didnt consider.

Is there a rationale behind usinf System.IO.Combine? Or is it just better practice?
This one resolves directory and file paths for you and also throws exceptions if you got some invalid characters inside strings.
Code: [Select]
string path = System.IO.Path.Combine("C:\\", "game", "data", "settings.cfg")
// => C:\\game\data\settings.cfg