Search Unity

One JSON file, multiple objects, multiple scenes...

Discussion in '2D' started by YoYoYoYo_111_PiPi, Oct 21, 2016.

  1. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Hi, when saving data of multiple objects to ONE .json file, how to do it, so you can save data of objects from different scenes? Here I am saving position change, and I've managed to save the change of two different objects.

    But how to save data from multiple scenes without having to create each separate .json file, for each scene... or is it even possible to store all that by having just one data file? In this approach from above you create an empty GameObject, add the Serializer script (second one) and fill the slots with the objects whose data you want to save. If we would cary this script around the slots would get empty and it becomes useless.
     
    Last edited: Oct 26, 2016
  2. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Why would you save them separately anyways from scene to scene
     
  3. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    No, I want one .json file, but i want to have multiple scenes... Don't people use more scenes in the game, and aren't certain objects just in certain scenes?

    EDIT: Cause this approach uses 'publicGameObject object;', slots need to be populated with the objects in question for the script to work. I can't add objects from another scene to it, right? Maybe my logic is off on the whole thing.
     
    Last edited: Oct 22, 2016
  4. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    So it's a form of map your after, if it is there are several ways to do this, not necessarily with multiple scenes.

    for example I have a game that has 50 levels, do you think I create 50 scene's no I create one scene for the world and several other scene e.g.

    Main Menu Scene
    Character Selection Scene
    Play Scene
    Game Scene
    Information Scene

    further to our email between each other when I send you the Persistant state object project etc.

    Here is some code yes I know it's in VB.net, I'm fluent in C# and VB, but I've been coding up a prototype in VB for a possible Unity game.

    This example will write a binary file for you in android it's a much simpler than the original I gave you.

    Code (CSharp):
    1.  
    2. Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    3. Dim mPath As String = Application.StartupPath & "\map.dat"
    4.  
    5.   If File.Exists(mPath) = True Then
    6.    File.Delete(mPath)
    7.   End If
    8.  
    9.   Using mWriter As BinaryWriter = New BinaryWriter(New FileStream(mPath, FileMode.Create))
    10.  
    11.    For Row As Int32 = 0 To mCell.GetUpperBound(0)
    12.     For Column As Int32 = 0 To mCell.GetUpperBound(1)
    13.      mWriter.Write(mCell(Column, Row).Colour.ToArgb())
    14.     Next
    15.    Next
    16.  
    17.    mWriter.Flush()
    18.    mWriter.Close()
    19.   End Using
    20.  
    21. End Sub
    22.  
    23. Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    24. Dim mPath As String = Application.StartupPath & "\map.dat"
    25.   If File.Exists(mPath) = True Then
    26.    Using mReader As BinaryReader = New BinaryReader(New FileStream(mPath, FileMode.Open))
    27.  
    28.     For Row As Int32 = 0 To mCell.GetUpperBound(0)
    29.      For Column As Int32 = 0 To mCell.GetUpperBound(1)
    30.        mCell(Column, Row).Colour = Color.FromArgb(mReader.ReadInt32)
    31.      Next
    32.     Next
    33.  
    34.     Refresh()
    35.    End Using
    36.   End If
    37. End Sub
    38.  
     
  5. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    I wanted to make a point and click adventure game... like Monkey Island. So, I thought people use different scenes for different... scenes, for the lack of the better term. My mistake as a beginner, I guess. I did saw on the internet that people just create one scene, and then change cameras, but i didn't know if all games were made in such manner... so if you can't save data of different scenes in the same data file with Unity, then you can't. That's what I wanted to find out.
     
  6. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    this does depend on what sort of game your making, Monkey Island is an old Amiga game I believe, this game does not use any form of mapping in the 2D sense, these might be layered backgrounds behind the player and in-front of the play implying depth, similar to a platform game in some ways.

    it's using the tools to fit your needs.
     
  7. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    OK, but just answer me this... is it possible with Unity to save multiple scenes data in one place, and later use that file to load all the date that we need, for each and and all the scenes, when necessary... besides the fact that we can do all the levels in one scene. Just need to know if that's completely impossible.
     
  8. lollypap54

    lollypap54

    Joined:
    Oct 19, 2016
    Posts:
    8
    Just make one object with script that protects object destroying itself when loading other scene :
    Code (CSharp):
    1. private static bool created = false;
    2.     void Start () {
    3.         if (!created) {
    4.             GameObject.DontDestroyOnLoad (transform.gameObject);
    5.             created = true;
    6.         } else {
    7.             Destroy (gameObject);
    8.         }
    9.     }
    And store your data in that object :
    Code (CSharp):
    1. public static bool exampleData;
    2.  
    3. private static bool created = false;
    4.     void Start () {
    5.         if (!created) {
    6.             GameObject.DontDestroyOnLoad (transform.gameObject);
    7.             created = true;
    8.         } else {
    9.             Destroy (gameObject);
    10.         }
    11.     }
    And then, you can access/set these variables like this: yourScriptName.exampleData = false;

    I hope i understood your question correctly :/ Sorry it's C#, but that's just for example, idk JS.
     
    YoYoYoYo_111_PiPi likes this.
  9. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    It's ok, rest is also C#, I'll look into the code/method you've written, and see if it works for what I need. Thanx, I'll get back to you...

    EDIT: Though as I've mentioned earlier, there is a problem with just using a DontDestroyOnLoad function for this script.
     
    Last edited: Oct 23, 2016
  10. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    I needed to do something similar for my game. Essentially I created a hierarchical structure to save and load transform and other data as necessary. My game has 6 stages, each of which has 10 levels. Each level is a Unity scene. I have a single data object which contains an array of stage objects. Each stage object has an array of level objects. Each level object has an array of items. When I serialize and save/load my one data object, the whole tree is saved/loaded along with it all in one easy to use data file. I'm using XML Serialization, but the principle would be the same for json.

    Code (CSharp):
    1. [XmlRoot("ItemCollection")]
    2. public class ItemCollection
    3. {
    4.     //My game has 6 stages, which are stored in a list of stages
    5.     [XmlArray("Stages"),XmlArrayItem("Stage")]
    6.     public List<Stage> stages = new List<Stage>();
    7. }
    8.  
    9. public class Stage
    10. {
    11.  
    12.     [XmlAttribute ("stage")]
    13.     public string stage;
    14.  
    15.     //Each stage has 10 levels, which are stored in a list of levels
    16.     [XmlArray("Levels"),XmlArrayItem("Level")]
    17.     public List<Level> levels = new List<Level>();
    18.  
    19.     public void Initialize (string _stage)
    20.     {
    21.         stage = _stage;
    22.     }
    23. }
    24.  
    25. public class Level
    26. {
    27.  
    28.     [XmlAttribute("level")]
    29.     public string levelName;
    30.     public bool starOne = false;
    31.     public bool starTwo = false;
    32.     public bool starThree = false;
    33.     public bool isLocked = true;
    34.  
    35.     //Each level has a varying amount of items, which are stored in a list of items.
    36.     [XmlArray ("Items"), XmlArrayItem ("Item")]
    37.     public List<Item> items = new List<Item>();  
    38.  
    39.     public void Initialize (string scene, int _level, bool _starOne, bool _starTwo, bool _starThree, bool _isLocked)
    40.     {
    41.         levelName = scene + _level.ToString ();
    42.         starOne = _starOne;
    43.         starTwo = _starTwo;
    44.         starThree = _starThree;
    45.         isLocked = _isLocked;
    46.     }
    47. }
    48.  
    49. public class Item
    50. {
    51.     //Ultimately, each gameObject in my game that I need to save data for is mapped via id to an Item object
    52.     //When a new scene is loaded, each gameObject in the scene gets it's saved transform data from the corresponding Item object.
    53.  
    54.     [XmlAttribute("name")]
    55.     public string name;
    56.     public int id;  
    57.    
    58.     public Vector3 position;
    59.     public Quaternion rotation;
    60.     public Vector3 scale;
    61.    
    62.     public bool isActive;
    63.    
    64.     public void Initialize (string _name, Vector3 _position, Quaternion _rotation, Vector3 _scale, bool _isActive, int _id)
    65.     {
    66.         id = _id;
    67.         name = _name;
    68.         position = _position;
    69.         rotation = _rotation;
    70.         scale = _scale;
    71.         isActive = _isActive;
    72.     }
    73. }
    74.  
    75.  
    I'm not sure if this will work for you, but hopefully it will at least point you in the right direction.
     
  11. YoYoYoYo_111_PiPi

    YoYoYoYo_111_PiPi

    Joined:
    Jun 23, 2016
    Posts:
    80
    Thank you very much, I'll hit you up for more info if necessary.