Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help with saving system

Discussion in 'Editor & General Support' started by triphion, Jan 27, 2020.

  1. triphion

    triphion

    Joined:
    Jan 30, 2019
    Posts:
    2
    So, i'm making a game where the player moves through predetermined rooms, can pick up items and use items. There is also dialogue, but that is not the issue. The issue is that i'm making a save system to save things such as: active item, doors that have been opened, enemies inside the different rooms, dialogue cleared, positions and rotations etc.

    So the approach I took was to make a binary saving system.
    The system I took inspiration from was the one shown in this video:

    (He shows the reference and the subclass at 3:40)

    But the problem is that i'm trying to replicate his idea of subclasses to store different types of data, but the way he shows it, only shows the first part of it. Not how you actually get ahold of the subclasses data. The way he shows it will always return the reference and all it's data as null.

    How do I actually get ahold of solid references to the subclasses?
     
  2. knobblez

    knobblez

    Joined:
    Nov 26, 2017
    Posts:
    223
    I haven't created a saving system but here's an idea: You could create a SaveGame() class like this:
    Code (CSharp):
    1.  
    2. public bool door1;
    3. public bool door2;
    4. //...
    5.  
    6. Save(int ID bool status)
    7. {
    8.      switch(ID)
    9.      {
    10.           case 1://door1
    11.           {
    12.                door1 = status;
    13.                break;
    14.           }
    15.      }
    16. }
    And each door could have a class like this:
    Code (CSharp):
    1.  
    2.  
    3. public int ID;
    4. public bool status;//true = open
    5. public SaveGame savegame;
    6.  
    7. OpenDoor()
    8. {
    9.      status = true;
    10.      savegame.Save(ID, status);
    11. }
    This would save the door status as soon as they open it. Just food for thought.
     
    triphion likes this.
  3. triphion

    triphion

    Joined:
    Jan 30, 2019
    Posts:
    2
    Thanks for the idea! I managed to implement it into my binary saving system with a get;set; for every different subclass i wanted to save info for. So this thread is therefore closed and solved!

    Edit: Here is the subclass for the doors as well as the initializer.
    Code (CSharp):
    1. [System.Serializable]
    2. public class DoorData
    3. {
    4.     public int ID;
    5.     public bool isOpen;
    6. }
    Code (CSharp):
    1.     private List<DoorData> _doors;
    2.     public List<DoorData> Doors
    3.     {
    4.         get
    5.         {
    6.             if (_doors == null)
    7.             {
    8.                 _doors = new List<DoorData>();
    9.             }
    10.  
    11.             return _doors;
    12.         }
    13.         set
    14.         {
    15.             _doors = value;
    16.         }
    17.     }
     
    knobblez likes this.