Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

read data file?

Discussion in 'Scripting' started by Shizzle, Apr 16, 2011.

  1. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    hi guys,i was woundering:
    how can i make a file that will be in my game's folder (iv seen that in other games thsi file is a .dat type.) and then when the game starts it reads information from the file and import it to the file (like saving slots,lives,score,etc..)

    thanks for your help! :eek:
     
  2. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    I used this (INI FILES):

    Code (csharp):
    1.  
    2. using System;
    3. using System.Runtime.InteropServices;
    4. using System.Text;
    5.  
    6.  
    7. namespace Ini
    8. {
    9.     /// <summary>
    10.  
    11.     /// Create a New INI file to store or load data
    12.  
    13.     /// </summary>
    14.  
    15.     public class IniFile
    16.     {
    17.         public string path;
    18.  
    19.         [DllImport("kernel32")]
    20.         private static extern long WritePrivateProfileString(string section,
    21.             string key, string val, string filePath);
    22.         [DllImport("kernel32")]
    23.         private static extern int GetPrivateProfileString(string section,
    24.                  string key, string def, StringBuilder retVal,
    25.             int size, string filePath);
    26.  
    27.         /// <summary>
    28.  
    29.         /// INIFile Constructor.
    30.  
    31.         /// </summary>
    32.  
    33.         /// <PARAM name="INIPath"></PARAM>
    34.  
    35.         public IniFile(string INIPath)
    36.         {
    37.             path = INIPath;
    38.         }
    39.         /// <summary>
    40.  
    41.         /// Write Data to the INI File
    42.  
    43.         /// </summary>
    44.  
    45.         /// <PARAM name="Section"></PARAM>
    46.  
    47.         /// Section name
    48.  
    49.         /// <PARAM name="Key"></PARAM>
    50.  
    51.         /// Key Name
    52.  
    53.         /// <PARAM name="Value"></PARAM>
    54.  
    55.         /// Value Name
    56.  
    57.         public void IniWriteValue(string Section, string Key, string Value)
    58.         {
    59.             WritePrivateProfileString(Section, Key, Value, this.path);
    60.         }
    61.  
    62.         /// <summary>
    63.  
    64.         /// Read Data Value From the Ini File
    65.  
    66.         /// </summary>
    67.  
    68.         /// <PARAM name="Section"></PARAM>
    69.  
    70.         /// <PARAM name="Key"></PARAM>
    71.  
    72.         /// <PARAM name="Path"></PARAM>
    73.  
    74.         /// <returns></returns>
    75.  
    76.         public string IniReadValue(string Section, string Key)
    77.         {
    78.             StringBuilder temp = new StringBuilder(255);
    79.             int i = GetPrivateProfileString(Section, Key, "", temp,
    80.                                             255, this.path);
    81.             return temp.ToString();
    82.  
    83.         }
    84.     }
    85. }
    86.  
    Then you could do something like this w/ the above class:
    Code (csharp):
    1.  
    2. void WriteNewData()
    3.     {
    4.         ini = new IniFile(path);
    5.         ini.IniWriteValue("Stats", "strength", "0");
    6.         ini.IniWriteValue("Stats", "intelligence", "0");
    7.         ini.IniWriteValue("Stats", "dexterity", "0");
    8.         ini.IniWriteValue("Stats", "physicalAttackPower", "1");
    9.         ini.IniWriteValue("Stats", "magicalAttackPower", "5");
    10.         ini.IniWriteValue("Stats", "physicalDefense", "1");
    11.         ini.IniWriteValue("Stats", "magicDefense", "1");
    12.         ini.IniWriteValue("Stats", "critRate", "0");
    13.         ini.IniWriteValue("General", "name", actualName);
    14.         ini.IniWriteValue("General", "level", "0");
    15.         ini.IniWriteValue("General", "expToLevel", "100");
    16.         ini.IniWriteValue("General", "health", "25");
    17.         ini.IniWriteValue("General", "mana", "25");
    18.         ini.IniWriteValue("General", "accumulatedExp", "0");
    19.     }
    20.  
     
  3. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    if i understand correctly theese scripts makes an INI file,and just write it,how can i read it?
    thanks for answering too ;D
     
  4. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    the class reads them too..


    public string IniReadValue(string Section, string Key)
    {
    StringBuilder temp = new StringBuilder(255);
    int i = GetPrivateProfileString(Section, Key, "", temp,
    255, this.path);
    return temp.ToString();

    }
     
  5. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    maybe its because its C# and im using Java for most of my game,but when i try to add the file to the camera to make the loading,it says that i cant add it.
    cant you please explain me what the stuff there does,cause i didnt understand how to do it,and what to do with it :/
     
  6. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    I don't know how to use unityscript to open/edit files.
     
  7. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    is there a less complicated way or java way?
    thanks for the help..
     
  8. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    It's not complicated at all man. o_O

    Copy/paste the INIFile class into a file, in the c# script where you want to use it, put at the top "using Ini;" and that will include the class in that script. So you can do this:

    IniFile myIni = new IniFile("path to where you want to store");

    Then you can do this:

    myIni.IniReadValue("your Section", "your Key");

    or:

    myIni.IniWriteValue("your Section", "your Key", "your Value");
     
  9. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    i really dont understand it,im not really good with c# , i rether do something that i understand.
    i also found out this option of unity:
    PlayerPrefs.GetInt
    and it works but i want to know how can i know if its the first time the player makes the data or not,so if its the first time,ill put the variables in the saved data,and then if its not first time,ill load the data,and put it in my variables ?
     
  10. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    lets say it like this:
    i have a file in my game's folder that is called "gameData.ini" (or "gameData.dat" , it doesnt matter to me)
    now the file is empty,and because it is empty i want to write stuff for it trough unity,and when its not empty i want to read the variables from it and put it on mine,lets say that the variable's names are :
    "name"
    "level"
    "score"
    "slots"

    and that i would be able to write over them all the time,so it reads them at start and then on the "Update" function ,it will allways write theese variables,to have an auto save.

    how can i do that?
     
  11. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    Code (csharp):
    1. bool CheckForFile()
    2.     {
    3.         if (File.Exists(path))
    4.         {
    5.             ini = new IniFile(path); //the path includes the filename
    6.             return true;
    7.         }
    8.         else
    9.             return false;
    10.     }
    11.  
    i did that. If the file exists, I set it to a new iniFile object and read from it in later code. If not, I got a new filename from the user and called IniWriteValue(), which automatically creates the file if it doesn't exist.
     
  12. Shizzle

    Shizzle

    Joined:
    Apr 7, 2011
    Posts:
    49
    so the ful code should look like this?
    Code (csharp):
    1. using System;
    2. using System.Runtime.InteropServices;
    3. using System.Text;
    4.  
    5.  
    6. namespace Ini
    7. {
    8.     /// <summary>
    9.    
    10.     bool CheckForFile()
    11.     {
    12.         if (File.Exists(path))
    13.         {
    14.             ini = new IniFile(path); //the path includes the filename
    15.             return true;
    16.         }
    17.         else
    18.             return false;
    19.     }
    20.    
    21.     /// Create a New INI file to store or load data
    22.  
    23.     /// </summary>
    24.  
    25.     public class IniFile
    26.     {
    27.         public string path;
    28.  
    29.         [DllImport("kernel32")]
    30.         private static extern long WritePrivateProfileString(string section,
    31.             string key, string val, string filePath);
    32.         [DllImport("kernel32")]
    33.         private static extern int GetPrivateProfileString(string section,
    34.                  string key, string def, StringBuilder retVal,
    35.             int size, string filePath);
    36.  
    37.         /// <summary>
    38.  
    39.         /// INIFile Constructor.
    40.  
    41.         /// </summary>
    42.  
    43.         /// <PARAM name="INIPath"></PARAM>
    44.  
    45.         public IniFile(string INIPath)
    46.         {
    47.             path = INIPath;
    48.         }
    49.         /// <summary>
    50.  
    51.         /// Write Data to the INI File
    52.  
    53.         /// </summary>
    54.  
    55.         /// <PARAM name="Section"></PARAM>
    56.  
    57.         /// Section name
    58.  
    59.         /// <PARAM name="Key"></PARAM>
    60.  
    61.         /// Key Name
    62.  
    63.         /// <PARAM name="Value"></PARAM>
    64.  
    65.         /// Value Name
    66.  
    67.         public void IniWriteValue(string Section, string Key, string Value)
    68.         {
    69.             WritePrivateProfileString(Section, Key, Value, this.path);
    70.         }
    71.  
    72.         /// <summary>
    73.  
    74.         /// Read Data Value From the Ini File
    75.  
    76.         /// </summary>
    77.  
    78.         /// <PARAM name="Section"></PARAM>
    79.  
    80.         /// <PARAM name="Key"></PARAM>
    81.  
    82.         /// <PARAM name="Path"></PARAM>
    83.  
    84.         /// <returns></returns>
    85.  
    86.         public string IniReadValue(string Section, string Key)
    87.         {
    88.             StringBuilder temp = new StringBuilder(255);
    89.             int i = GetPrivateProfileString(Section, Key, "", temp,
    90.                                             255, this.path);
    91.             return temp.ToString();
    92.  
    93.         }
    94.     }
    95.    
    96.     void WriteNewData()
    97.     {
    98.         ini = new IniFile(path);
    99.         ini.IniWriteValue("Stats", "strength", "0");
    100.         ini.IniWriteValue("Stats", "intelligence", "0");
    101.         ini.IniWriteValue("Stats", "dexterity", "0");
    102.         ini.IniWriteValue("Stats", "physicalAttackPower", "1");
    103.         ini.IniWriteValue("Stats", "magicalAttackPower", "5");
    104.         ini.IniWriteValue("Stats", "physicalDefense", "1");
    105.         ini.IniWriteValue("Stats", "magicDefense", "1");
    106.         ini.IniWriteValue("Stats", "critRate", "0");
    107.         ini.IniWriteValue("General", "name", actualName);
    108.         ini.IniWriteValue("General", "level", "0");
    109.         ini.IniWriteValue("General", "expToLevel", "100");
    110.         ini.IniWriteValue("General", "health", "25");
    111.         ini.IniWriteValue("General", "mana", "25");
    112.         ini.IniWriteValue("General", "accumulatedExp", "0");
    113.     }
    114.    
    115. }
    im really having trouble in understanding this whole thing,cause im really bad at C#,thanks for helping me and considering my troubles :D
     
  13. Amaz1ng

    Amaz1ng

    Joined:
    Mar 11, 2011
    Posts:
    111
    rofl...

    1. Take this and copy it into a c# script file:
    Code (csharp):
    1.  
    2. using System;
    3. using System.Runtime.InteropServices;
    4. using System.Text;
    5.  
    6.  
    7. namespace Ini
    8. {
    9.     /// <summary>
    10.  
    11.     /// Create a New INI file to store or load data
    12.  
    13.     /// </summary>
    14.  
    15.     public class IniFile
    16.     {
    17.         public string path;
    18.  
    19.         [DllImport("kernel32")]
    20.         private static extern long WritePrivateProfileString(string section,
    21.             string key, string val, string filePath);
    22.         [DllImport("kernel32")]
    23.         private static extern int GetPrivateProfileString(string section,
    24.                  string key, string def, StringBuilder retVal,
    25.             int size, string filePath);
    26.  
    27.         /// <summary>
    28.  
    29.         /// INIFile Constructor.
    30.  
    31.         /// </summary>
    32.  
    33.         /// <PARAM name="INIPath"></PARAM>
    34.  
    35.         public IniFile(string INIPath)
    36.         {
    37.             path = INIPath;
    38.         }
    39.         /// <summary>
    40.  
    41.         /// Write Data to the INI File
    42.  
    43.         /// </summary>
    44.  
    45.         /// <PARAM name="Section"></PARAM>
    46.  
    47.         /// Section name
    48.  
    49.         /// <PARAM name="Key"></PARAM>
    50.  
    51.         /// Key Name
    52.  
    53.         /// <PARAM name="Value"></PARAM>
    54.  
    55.         /// Value Name
    56.  
    57.         public void IniWriteValue(string Section, string Key, string Value)
    58.         {
    59.             WritePrivateProfileString(Section, Key, Value, this.path);
    60.         }
    61.  
    62.         /// <summary>
    63.  
    64.         /// Read Data Value From the Ini File
    65.  
    66.         /// </summary>
    67.  
    68.         /// <PARAM name="Section"></PARAM>
    69.  
    70.         /// <PARAM name="Key"></PARAM>
    71.  
    72.         /// <PARAM name="Path"></PARAM>
    73.  
    74.         /// <returns></returns>
    75.  
    76.         public string IniReadValue(string Section, string Key)
    77.         {
    78.             StringBuilder temp = new StringBuilder(255);
    79.             int i = GetPrivateProfileString(Section, Key, "", temp,
    80.                                             255, this.path);
    81.             return temp.ToString();
    82.  
    83.         }
    84.     }
    85. }
    86.  
    2. Make a new c# script file and include "using Ini;" like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Ini;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    3. Make a new Ini object like this:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Ini;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.  
    8. public IniFile myini;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.     myini = new Ini("THE PATH YOU WANT HERE");
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19. }
    20.  
    4. Use the functions of the class to read/write to whatever: example:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using Ini;
    5.  
    6. public class NewBehaviourScript : MonoBehaviour {
    7.  
    8. public IniFile myini;
    9.  
    10.     // Use this for initialization
    11.     void Start () {
    12.     myini = new Ini("THE PATH YOU WANT HERE");
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update () {
    17.    
    18.     }
    19.  
    20.         void read()
    21. {
    22. myIni.IniReadValue(...);
    23. }
    24.  
    25. }
    26.  
    Beyond that...I can't really help. Try to find a javascript solution or learn c# better.

    Here is the orig page I got the code from:

    http://www.codeproject.com/KB/cs/cs_ini.aspx
     
    Last edited: Apr 16, 2011