x


How do I load a config (.cfg) or ini file?

I want my game to load a config (.cfg) or ini file from outside the game directory, and also have it save to it when you save game settings.

The reason I want to do this is because I want the to give people the option to fine tune the settings, and set it to things not available in the options. Many PC games do this, you can edit the config.cfg/config.ini file to add/remove graphic effects, and change the resolution if the game doesn't support the resolution you use, you can set it in the config file, I want to have a config file for my game to let people to the same thing.

I figured that if I loaded the file from outside the game folder, it will still be accessible to the user after I build the game, I would just have the move the to the build game's directory.

How would I go about doing this? Preferably in C#. I know how to get the game's directory, but I don't know much beyond that, and I never got any answers when I looked it up.

more ▼

asked Aug 05, 2013 at 02:26 PM

Cobra! gravatar image

Cobra!
136 84 67 71

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

You can store your settings in on a prefab object(via a custom settings script on it) and you can serialize (generate text from it) and deserialize (load from text) the prefab object into/from the config file you created.

To serialize: http://answers.unity3d.com/questions/264347/serialization.html

NOTICE: This example is using binary serialization. You would want to convert it to text serialization.

To read/write a text file: http://www.csharp-station.com/howto/readwritetextfile.aspx

more ▼

answered Aug 05, 2013 at 02:42 PM

Xtro gravatar image

Xtro
2.8k 18 13 25

Okay, I looked it up, and found a way to serialize ini files, and the serialization works great, but the deserialization is a different story.

It saves just fine, although I have a bit of a problem concering the directory/file, but I can deal with that later. I'm always getting an error when trying to load what I saved.

I just used a string to (de)serialize for a test, here's my code:

     public class SerializeINI
     {
     public static void Serialize(string name, string GamePath)
     {
         System.Text.StringBuilder sb = new System.Text.StringBuilder();
         sb.AppendLine("name=" + name);
         System.IO.StreamWriter writer = new System.IO.StreamWriter(GamePath);
         writer.Write(sb.ToString());
             writer.Close();
     }
  
     public static string DeSerialize(string name, string GamePath)
     {
         System.IO.StreamReader reader = new System.IO.StreamReader(GamePath);
         string line;
         while((line = reader.ReadLine()) != string.Empty)
         {
             string[] id_value = line.Split('=');
             switch (id_value[0])
             {
                 case "name":
                     name = id_value[1].ToString();
                     break;
             }
         }
             reader.Close();
             return name;
     }
 }
     // Update is called once per frame
     void Update () 
     {
         if (load)
         {
         if (!done)
         {
             name = SerializeINI.DeSerialize(name, gamePath);
             done = true;
         }
         else
         {
             //Destroy(gameObject);
         }
         }
     }

I'm getting an error saying: "NullReferenceException: Object reference not set to an instance of an object ConfigScript+SerializeINI.DeSerialize (System.String name, System.String GamePath) (at Assets/Temporary Assets/Scripts/ConfigScript.cs:47) ConfigScript.Update () (at Assets/Temporary Assets/Scripts/ConfigScript.cs:66)"

I'm assuming the numbers are meant to be what lines the error occurred, line 66 is where it's being called and line 47 is "string[] id_value = line.Split('=');"

I can't see anything wrong here, what am I doing wrong?

Aug 05, 2013 at 10:04 PM Cobra!

Try null instead of string.Empty

Aug 06, 2013 at 01:59 AM Xtro

It works, thank you! One more question, when I tried to add a file name to my extension e.g. "config.ini", I got an error saying "Assets/Temporary Assets/Scripts/ConfigScript.cs(26,30): error CS1009: Unrecognized escape sequence `c'", so I tried ""/" + "config.ini", and I still got an error, how do I add a file at the end of an file path?

Aug 06, 2013 at 10:15 AM Cobra!

in path string use double back slash for one backslash. like:

"c:gameswowconfig.ini"

or in C# you can do this:

@"c:gameswowconfig.ini"

don't forget the mark my post as an answer. Thank you :)

Aug 06, 2013 at 01:33 PM Xtro
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x10950
x375
x68
x44
x18

asked: Aug 05, 2013 at 02:26 PM

Seen: 4777 times

Last Updated: Aug 06, 2013 at 01:33 PM