1
\$\begingroup\$

I wrote a script to save and load game statistic. It works fine. In this script, two levels have it own score and timer. So, I have to write level by level it own score and timer. The issue is if I have 100 or 200 levels in my game. I have to write for every level. Yes will work, but will take time. So, I was thinking if there any simple method to shift the number of lines like use List or anything else ?

public gameStatic dr;

void Awake()    {
    Load_game_end();
}


   public void Save_game_end()  {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream data_file = File.Create(Application.persistentDataPath + "/WarWorld.datt");

    level_static data = new level_static();
    data.level1tv = dr.level1t; // level one
    data.level2tv = dr.level2t; // level two
    //
    data.timer1 = dr.finalTime1; // level one
    data.timer2 = dr.finalTime2; // level two
    //
    data.total_money = dr.total_money;


    bf.Serialize(data_file, data);
    data_file.Close();

    Debug.Log("Game full saved");

}

public void Load_game_end() {
    if (File.Exists (Application.persistentDataPath + "/WarWorld.datt"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream data_file = File.Open (Application.persistentDataPath + "/ballworlds.datt", FileMode.Open);
        level_static data = (level_static)bf.Deserialize(data_file);
        data_file.Close();

        dr.level1t= data.level1tv;
        dr.level2t = data.level2tv;
        //
        dr.finalTime1 = data.timer1;
        dr.finalTime2 = data.timer2;
        //
        dr.total_money = data.total_money;

        Debug.Log("Game full loaded");
    }

    else {
        Debug.Log("save not found");
    }

}

public void delete_save_file() {
    File.Delete(Application.persistentDataPath +"/WarWorld.datt");
    Debug.Log("delete save file");

}

[Serializable]
class level_static
{
    public float total_money; 
    //
    public float level1tv; // height score level 1
    public float level2tv;

    //
    public float timer1; //  level 1 timer
    public float timer2;


}
\$\endgroup\$
  • \$\begingroup\$ Using a list sounds like a great idea. How have you tried putting that idea into practice? \$\endgroup\$ – DMGregory Feb 9 at 12:14
  • \$\begingroup\$ I was think in something like this. Than, I have to use For function to count how many levels in the list. So, now I have to select first level....I lost. Do you have any clue for me ? \$\endgroup\$ – user43474 Feb 9 at 13:53
2
\$\begingroup\$
   public void Save_game_end()  {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream data_file = File.Create(Application.persistentDataPath + "/WarWorld.datt");

    level_static data = new level_static();
    data.level1tv = dr.level1t; // level one
    data.level2tv = dr.level2t; // level two
    //
    data.timer1 = dr.finalTime1; // level one
    data.timer2 = dr.finalTime2; // level two
    //
    data.total_money = dr.total_money;


    bf.Serialize(data_file, data);
    data_file.Close();

    Debug.Log("Game full saved");

}

instead of that maybe exclude data.level to params?

...Save_game_end(level, finalTime){

and for class level_static create custom constructor to set level, timer, money. Use collection to save all levels and timers.

Example:

   public gameStatic dr;

public List<level_static>  levelStaticList;

void Awake()
{
    Load_game_end();
}


public void Save_game_end()
{
    BinaryFormatter bf = new BinaryFormatter();
    FileStream data_file = File.Create(Application.persistentDataPath + "/WarWorld.datt");

    bf.Serialize(data_file, levelStaticList);
    data_file.Close();

    Debug.Log("Game full saved");
}

public void Load_game_end()
{
    if (File.Exists(Application.persistentDataPath + "/WarWorld.datt"))
    {
        BinaryFormatter bf = new BinaryFormatter();
        FileStream data_file = File.Open(Application.persistentDataPath + "/ballworlds.datt", FileMode.Open);
        List<level_static> data = (List<level_static>)bf.Deserialize(data_file);
        data_file.Close();

        //if you cant return list from file then try something different (deserialize one by one in the foreach for an example and add to list).

        levelStaticList = data;

        Debug.Log("Game full loaded");
    }

    else
    {
        Debug.Log("save not found");
    }

}

public void delete_save_file()
{
    File.Delete(Application.persistentDataPath + "/WarWorld.datt");
    Debug.Log("delete save file");

}

[Serializable]
class level_static
{
    public level_static(float level, float timer, float money)
    {
        this.level = level;
        this.timer = timer;
        total_money = money;
    }

    public float total_money;
    //
    public float level;

    //
    public float timer;
}
\$\endgroup\$
  • \$\begingroup\$ This is new. Maybe you right. but I don't know set it in load. Could you write it please. \$\endgroup\$ – user43474 Feb 9 at 14:02
  • \$\begingroup\$ Something like that \$\endgroup\$ – Robert Feb 9 at 14:51
  • \$\begingroup\$ Great. Thank you man for the script. I will try it. \$\endgroup\$ – user43474 Feb 9 at 17:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.