read data file?

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! :o

I used this (INI FILES):

using System;
using System.Runtime.InteropServices;
using System.Text;


namespace Ini
{
    /// <summary>

    /// Create a New INI file to store or load data

    /// </summary>

    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        /// <summary>

        /// INIFile Constructor.

        /// </summary>

        /// <PARAM name="INIPath"></PARAM>

        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>

        /// Write Data to the INI File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// Section name

        /// <PARAM name="Key"></PARAM>

        /// Key Name

        /// <PARAM name="Value"></PARAM>

        /// Value Name

        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>

        /// Read Data Value From the Ini File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// <PARAM name="Key"></PARAM>

        /// <PARAM name="Path"></PARAM>

        /// <returns></returns>

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

        }
    }
}

Then you could do something like this w/ the above class:

void WriteNewData()
    {
        ini = new IniFile(path);
        ini.IniWriteValue("Stats", "strength", "0");
        ini.IniWriteValue("Stats", "intelligence", "0");
        ini.IniWriteValue("Stats", "dexterity", "0");
        ini.IniWriteValue("Stats", "physicalAttackPower", "1");
        ini.IniWriteValue("Stats", "magicalAttackPower", "5");
        ini.IniWriteValue("Stats", "physicalDefense", "1");
        ini.IniWriteValue("Stats", "magicDefense", "1");
        ini.IniWriteValue("Stats", "critRate", "0");
        ini.IniWriteValue("General", "name", actualName);
        ini.IniWriteValue("General", "level", "0");
        ini.IniWriteValue("General", "expToLevel", "100");
        ini.IniWriteValue("General", "health", "25");
        ini.IniWriteValue("General", "mana", "25");
        ini.IniWriteValue("General", "accumulatedExp", "0");
    }

if i understand correctly theese scripts makes an INI file,and just write it,how can i read it?
thanks for answering too ;D

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();

}

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 :confused:

I don’t know how to use unityscript to open/edit files.

is there a less complicated way or java way?
thanks for the help…

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”);

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 ?

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?

bool CheckForFile()
    {
        if (File.Exists(path))
        {
            ini = new IniFile(path); //the path includes the filename
            return true;
        }
        else
            return false;
    }

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.

so the ful code should look like this?

using System;
using System.Runtime.InteropServices;
using System.Text;


namespace Ini
{
    /// <summary>
	
	bool CheckForFile()
    {
        if (File.Exists(path))
        {
            ini = new IniFile(path); //the path includes the filename
            return true;
        }
        else
            return false;
    }
	
    /// Create a New INI file to store or load data

    /// </summary>

    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        /// <summary>

        /// INIFile Constructor.

        /// </summary>

        /// <PARAM name="INIPath"></PARAM>

        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>

        /// Write Data to the INI File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// Section name

        /// <PARAM name="Key"></PARAM>

        /// Key Name

        /// <PARAM name="Value"></PARAM>

        /// Value Name

        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>

        /// Read Data Value From the Ini File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// <PARAM name="Key"></PARAM>

        /// <PARAM name="Path"></PARAM>

        /// <returns></returns>

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

        }
    }
	
	void WriteNewData()
    {
        ini = new IniFile(path);
        ini.IniWriteValue("Stats", "strength", "0");
        ini.IniWriteValue("Stats", "intelligence", "0");
        ini.IniWriteValue("Stats", "dexterity", "0");
        ini.IniWriteValue("Stats", "physicalAttackPower", "1");
        ini.IniWriteValue("Stats", "magicalAttackPower", "5");
        ini.IniWriteValue("Stats", "physicalDefense", "1");
        ini.IniWriteValue("Stats", "magicDefense", "1");
        ini.IniWriteValue("Stats", "critRate", "0");
        ini.IniWriteValue("General", "name", actualName);
        ini.IniWriteValue("General", "level", "0");
        ini.IniWriteValue("General", "expToLevel", "100");
        ini.IniWriteValue("General", "health", "25");
        ini.IniWriteValue("General", "mana", "25");
        ini.IniWriteValue("General", "accumulatedExp", "0");
    }
	
}

im really having trouble in understanding this whole thing,cause im really bad at C#,thanks for helping me and considering my troubles :smile:

rofl…

  1. Take this and copy it into a c# script file:
using System;
using System.Runtime.InteropServices;
using System.Text;


namespace Ini
{
    /// <summary>

    /// Create a New INI file to store or load data

    /// </summary>

    public class IniFile
    {
        public string path;

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section,
                 string key, string def, StringBuilder retVal,
            int size, string filePath);

        /// <summary>

        /// INIFile Constructor.

        /// </summary>

        /// <PARAM name="INIPath"></PARAM>

        public IniFile(string INIPath)
        {
            path = INIPath;
        }
        /// <summary>

        /// Write Data to the INI File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// Section name

        /// <PARAM name="Key"></PARAM>

        /// Key Name

        /// <PARAM name="Value"></PARAM>

        /// Value Name

        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }

        /// <summary>

        /// Read Data Value From the Ini File

        /// </summary>

        /// <PARAM name="Section"></PARAM>

        /// <PARAM name="Key"></PARAM>

        /// <PARAM name="Path"></PARAM>

        /// <returns></returns>

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

        }
    }
}
  1. Make a new c# script file and include “using Ini;” like this:
using UnityEngine;
using System.Collections;
using Ini;

public class NewBehaviourScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
  1. Make a new Ini object like this:
using UnityEngine;
using System.Collections;
using Ini;

public class NewBehaviourScript : MonoBehaviour {

public IniFile myini;

	// Use this for initialization
	void Start () {
	myini = new Ini("THE PATH YOU WANT HERE");
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
  1. Use the functions of the class to read/write to whatever: example:
using UnityEngine;
using System.Collections;
using Ini;

public class NewBehaviourScript : MonoBehaviour {

public IniFile myini;

	// Use this for initialization
	void Start () {
	myini = new Ini("THE PATH YOU WANT HERE");
	}
	
	// Update is called once per frame
	void Update () {
	
	}

        void read()
{
myIni.IniReadValue(...);
}

}

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