Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Not saving items collected next play with binary saving system

Discussion in 'Scripting' started by Mtrco, Dec 27, 2019.

  1. Mtrco

    Mtrco

    Joined:
    Dec 27, 2019
    Posts:
    5
    Hi guys
    I'm new at unity
    I have a problem with saving items collected like coins and stars in binary saving system.
    How can I save coins collected and not show next play.
    Ofcorse I can do it for few number but it's too much repeating for more item I have 90 stars that after player collected them,they should be disappeard
    Please help me
     
    Last edited: Dec 27, 2019
  2. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Hi,
    Simple way to do it is by creating a list of objects and save that list into your binary file.
    Code (CSharp):
    1. public class CoinStarItem
    2. {
    3.    public int Index {get; set;}
    4.    public bool Visible {get; set;}
    5.    public GameObject ActualGameObject {get; set;}
    6.    public Vector3 position {get; set;}
    7.    public TypeOfItem ItemType {get; set;}
    8. }
    Then you have to save this list infos into the binary file..
    Code (CSharp):
    1. List<CoinStarItem> items;
    Actualy you need to save (index, visibility, item type and position)

    And at loading, repopulate the list and apply visibility;
     
  3. Mtrco

    Mtrco

    Joined:
    Dec 27, 2019
    Posts:
    5


    Thank u very much
    But I dont use set and get for saving
    This way : index=database.index ;
    How can I save this variable to the list
    And how to use list after load
     
  4. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    Loading should be something like this:
    Code (CSharp):
    1.  
    2. foreach(var database in databaseFields)
    3. {
    4.   CoinStarItem item = new CoinStarItem();
    5.  
    6.   TpeOfItem t = database.typeOfItem;
    7.   GameObject go = CreateInstance(t == Coin ? coinPrefab : starPrefab);
    8.   item.Index = database.index;
    9.   item.Visible = database.itemVisible;
    10.   item.position = database.position;
    11.   go.SetActive(item.Visible);
    12.   go.transform.position = item.position;
    13.   item.ActualGameObject = go;
    14.  
    15.   items.Add(item);
    16. }
    Save should be something like:
    Code (CSharp):
    1. foreach(var item in items)
    2. {
    3.   //now just populate your database
    4.   database.index = item.Index;
    5.   etc...
    6.   save(database field);
    7. }
     
  5. Mtrco

    Mtrco

    Joined:
    Dec 27, 2019
    Posts:
    5
    Sorry but I can't understand what should I'd do

    Here is my code
    Code (CSharp):
    1. using System.Collections;
    2. using System;
    3. using System.IO;
    4. using System.Collections.Generic;
    5.  
    6. using System.Runtime.Serialization.Formatters.Binary;
    7. using UnityEngine;
    8.  
    9. public class DataManagement : MonoBehaviour
    10. {
    11.     public static DataManagement datamanagement;
    12.  
    13.  
    14.     public int index;
    15.     public bool visible;
    16.     public GameObject ActualGameobject;
    17.     public Vector3 posision;
    18.  
    19.     private void Awake()
    20.     {
    21.         if (datamanagement == null)
    22.         {
    23.             DontDestroyOnLoad(gameObject);
    24.             datamanagement = this;
    25.         }
    26.         else if (datamanagement != this)
    27.         {
    28.             Destroy(gameObject);
    29.         }
    30.     }
    31.  
    32.  
    33.     public void SaveData()
    34.     {
    35.         BinaryFormatter binaryForm = new BinaryFormatter();
    36.         FileStream file = File.Create(Application.persistentDataPath + "/gameInfo8.mtr");
    37.         gameData data = new gameData();
    38.  
    39.         data.index = index;
    40.         data.visible = visible;
    41.         data.ActualGameobject = ActualGameobject;
    42.         data.posision = posision;
    43.  
    44.         binaryForm.Serialize(file, data);
    45.         file.Close();
    46.     }
    47.  
    48.  
    49.     public void LoadData()
    50.     {
    51.         if (File.Exists(Application.persistentDataPath + "/gameInfo8.mtr"))
    52.         {
    53.             BinaryFormatter binaryForm = new BinaryFormatter();
    54.             FileStream file = File.Open(Application.persistentDataPath + "/gameInfo8.mtr", FileMode.Open);
    55.             gameData data = (gameData)binaryForm.Deserialize(file);
    56.             file.Close();
    57.  
    58.          
    59.  
    60.         }
    61.  
    62.         else { Debug.Log("No dataBase"); }
    63.     }
    64.  
    65.  
    66.  
    67.  
    68.     [Serializable]
    69.  
    70.     class gameData
    71.     {
    72.  
    73.         public int index;
    74.         public bool visible;
    75.         public GameObject ActualGameobject;
    76.         public Vector3 posision;
    77.  
    78.     }
    79.  
    80.  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87. }
     
  6. adi7b9

    adi7b9

    Joined:
    Feb 22, 2015
    Posts:
    181
    To save/load: https://wiki.unity3d.com/index.php/Saving_and_Loading_Data:_XmlSerializer

    GameData
    Code (CSharp):
    1. public enum eTypeOfGameData
    2. {
    3.     NONE = 0,
    4.     COIN,
    5.     STAR
    6. }
    7.  
    8. public class GameData
    9. {
    10.     public int Index {get; set;}
    11.     public bool Visible {get; set;}
    12.     public Vector3 Position {get; set;}
    13.     public eTypeOfGameData TypeOfGameData {get; set;}
    14. }
    Code (CSharp):
    1. //this is coin/star class and you should attach this to your coin/star object.. also is not completed
    2. public class CoinStar
    3. {
    4.     private GameData MyGameData;
    5.     private bool StatusVisibility;
    6.  
    7.     public void SetGameData(GameData gameData)
    8.     {
    9.         MyGameData = gameData;
    10.         StatusVisibility = gameData.Visible;
    11.         transform.position = MyGameData.Position;
    12.     }
    13.  
    14.     public void SetVisibility(bool value)
    15.     {
    16.         MyGameData.Visible = value;
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         //your code here
    22.    
    23.         if (StatusVisibility != MyGameData.Visible)//visibility changed
    24.         {
    25.             StatusVisibility = MyGameData.Visible;
    26.             if (!MyGameData.Visible)//this is invisible
    27.             {
    28.                 GetComponent<Renderer>().enabled = false;
    29.                 //gameObject.active = false; if you want to disable the gameObject
    30.             }
    31.             else
    32.             {
    33.                 GetComponent<Renderer>().enabled = true;
    34.             }
    35.             SaveToFile();//here you should call your method to save this new stuff into your file
    36.         }
    37.     }
    38. }
    https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnCollisionEnter2D.html
    Code (CSharp):
    1. //when player interact with your coin/star just call
    2. col.gameObject.GetComponent<CoinStar>().SetVisibility(false);
    Code (CSharp):
    1. //Load data should return a bool .. to tell you if everything was ok
    2. //place try.. catch in LoadData
    3. //After LoadData()
    4. public void SetGameData(List<GameData> gameDatas)
    5. {
    6.     foreach(var gd in gameDatas)
    7.     {
    8.         if (gd.TypeOfGameData == eTypeOfGameData.COIN)
    9.         {
    10.             InstantiateObject(coinPrefab, gd);
    11.         }
    12.         else if (gd.TypeOfGameData == eTypeOfGameData.STAR)
    13.         {
    14.             InstantiateObject(starPrefab, gd);
    15.         }
    16.     }
    17. }
    18. public void InstantiateObject(GameObject prefabObject, GameData gd)
    19. {
    20.     //https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
    21.     GameObject go = Instantiate(prefabObject);
    22.     CoinStar cs = go.GetComponent<CoinStar>();
    23.     cs.SetGameData(gd);
    24.     //you should always check if go, cs, prefab object is not null, else it will crash when one of those is null
    25. }
    26.