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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Best way to store game data for initializing the game items info ?

Discussion in 'Scripting' started by TheCelt, Jun 22, 2016.

  1. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    Hello

    I am wondering what is the correct way to store game data which i can use and call upon to get the info on a given item.

    Here is a prime example of what i mean:

    Say i have a shop which sells food theres lots of food items with different prices & different effects.

    It doesn't make sense to me to have it "attached" to a game object because food types/prices has no need to exist in the game world in a physical sense.

    Now say i want a particular item lets say an apple, how would i then acquire info like the price on said apple?
     
  2. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
  3. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    Thank you pixel, will take a watch :)
     
  4. pixelfixation

    pixelfixation

    Joined:
    Feb 7, 2014
    Posts:
    29
    Np happy to help.
     
  5. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    One issue i have is the users can edit these files. Is there a way to prevent that?
     
  6. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
    Encrypt the JSON string before saving it to disk. Here's a tutorial with source code.
     
  7. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    There's so many json libraries i am so lost on what i should be using... =/ What is the go to library these days?
     
  8. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    If you're not going to change the prices you can make them hard coded into your game, one way of doing it is using a dictionary<item,price>, or just make a class for each item.
     
  9. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    That leaves little room to expand on when they have more information than just a price though.
     
  10. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,534
  11. Severos

    Severos

    Joined:
    Oct 2, 2015
    Posts:
    181
    You can work around the details of what info you want in a class, dictionary was a simple solution to implement without classes.
    If you want to save your data in a Json file (or you can use c# built in system serialize binary format) you can add a checksum/hash to your file, so if user changes the data it won't load.
     
  12. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Or just save them in your project as TextAssets
     
  13. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    Never heard of text assets is there a tutorial on it?
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  15. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    I'd recommend using something like this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4. using System.Runtime.Serialization.Formatters.Binary;
    5. using System.Runtime.Serialization;
    6. using System;
    7. using System.Collections.Generic;
    8.  
    9. public class SaveInfoNewer : MonoBehaviour
    10. {
    11.     public string data;
    12.  
    13.  
    14.     public void LoadData()
    15.     {
    16.         string data = SaveInfoNewer.DeserializeData<string>("PleaseWork.save");
    17.         Debug.Log("Loaded data is:" + data);
    18.     }
    19.     public void SaveData()
    20.     {
    21.         SaveInfoNewer.SerializeData(data, "PleaseWork.save");
    22.         Debug.Log("Saved data");
    23.     }
    24.     public static void SerializeData<T>(T data, string path)
    25.     {
    26.  
    27.         FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    28.         BinaryFormatter formatter = new BinaryFormatter();
    29.         try
    30.         {
    31.             formatter.Serialize(fs, data);
    32.             Debug.Log("Data written to " + path + " @ " + DateTime.Now.ToShortTimeString());
    33.         }
    34.         catch (SerializationException e)
    35.         {
    36.             Debug.LogError(e.Message);
    37.         }
    38.         finally
    39.         {
    40.             fs.Close();
    41.         }
    42.     }
    43.  
    44.     public static T DeserializeData<T>(string path)
    45.     {
    46.  
    47.         T data = default(T);
    48.  
    49.         if (File.Exists(path))
    50.         {
    51.             FileStream fs = new FileStream(path, FileMode.Open);
    52.             try
    53.             {
    54.                 BinaryFormatter formatter = new BinaryFormatter();
    55.                 data = (T)formatter.Deserialize(fs);
    56.                 Debug.Log("Data read from " + path);
    57.             }
    58.             catch (SerializationException e)
    59.             {
    60.                 Debug.LogError(e.Message);
    61.             }
    62.             finally
    63.             {
    64.                 fs.Close();
    65.             }
    66.         }
    67.  
    68.         return data;
    69.     }
    70. }
    71.  
    While it looks complex, once you understand it it is pretty easy. Also it is pretty secure.

    If you don't really care about the efficiency or security of your code, you could just use PlayerPrefs
     
  16. TheCelt

    TheCelt

    Joined:
    Feb 27, 2013
    Posts:
    724
    Thank you :)