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

Having trouble saving more than 1 object.

Discussion in 'Scripting' started by SwatHound, Apr 29, 2021.

  1. SwatHound

    SwatHound

    Joined:
    Jun 7, 2018
    Posts:
    14
    I'm having trouble saving and loading more than 1 game object.

    I have an array (should I use lists?) to hold the NPCs in my "party".

    If I have only 1 NPC, I can save and load their data perfectly.

    I'm trying to save more than 1 NPCs data at a time.

    I'm not sure if I should be using "foreach (GameObject FRIEND in NPCs)" or for(int i=0; i < NPCs.Length; i++) to iterate through the array holding the NPCs.

    I am using TigerForge's Easy File Save - https://assetstore.unity.com/packages/tools/input-management/easy-file-save-161484


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using TMPro;
    6. using TigerForge;
    7. using QuantumTek.QuantumAttributes;
    8. using QuantumTek.QuantumAttributes.Demo;
    9.  
    10. public class mySave: MonoBehaviour
    11. {
    12.     EasyFileSave myFile;
    13.  
    14.  
    15.     QA_ExperienceHandler XPScript;
    16.     GainFriendXP  FriendScriptUI;
    17.  
    18.  
    19.  
    20.  
    21.     int FriendLvl;
    22.     int FriendCurrentXP;
    23.     int FriendMaxXP;
    24.  
    25.  
    26.     public GameObject[] NPCs;
    27.  
    28.     private void Start()
    29.     {
    30.         myFile = new EasyFileSave("SaveFile_1");
    31.  
    32.        
    33.  
    34.          foreach (GameObject FRIEND in NPCs)
    35.          {
    36.              Debug.Log("Hey!");
    37.  
    38.              XPScript = FRIEND.GetComponent<QA_ExperienceHandler>();
    39.              FriendScriptUI = FRIEND.GetComponent<GainFriendXP>();
    40.  
    41.  
    42.          }
    43.     }
    44.  
    45.  
    46.     public void SaveGame()
    47.     {
    48.         Debug.Log(">> I'M GOING TO SAVE SOME DATA!" + "\n");
    49.  
    50.        
    51.         foreach (GameObject FRIEND in NPCs)
    52.         {
    53.  
    54.  
    55.             FriendLvl = XPScript.Level;
    56.             FriendCurrentXP = ((int)XPScript.XP);
    57.             FriendMaxXP = ((int)XPScript.MaxXP);
    58.  
    59.  
    60.             myFile.Add("Friendlvl", FriendLvl);
    61.             myFile.Add("FriendCurrentXP", FriendCurrentXP);
    62.             myFile.Add("FriendMaxXP", FriendMaxXP);
    63.  
    64.  
    65.             Debug.Log("Saving File! x3");
    66.         }
    67.  
    68.  
    69.         myFile.Save();
    70.         Debug.Log("File Saved");
    71.     }
    72.  
    73.  
    74.     public void LoadGame()
    75.     {
    76.         if (myFile.Load())
    77.         {
    78.              
    79.              foreach (GameObject FRIEND in NPCs)
    80.              {
    81.  
    82.                 Debug.Log("loading... ");
    83.  
    84.                 int FriendLvl = myFile.GetInt("Friendlvl");
    85.                 int FriendCurrentXP = myFile.GetInt("FriendCurrentXP");
    86.                 int FriendMaxXP = myFile.GetInt("FriendMaxXP");
    87.  
    88.                 Debug.Log("Friend lvl " + FriendLvl);
    89.                 Debug.Log("Current XP " + FriendCurrentXP);
    90.                 Debug.Log("Max XP " + FriendMaxXP);
    91.  
    92.  
    93.                 XPScript.Level = FriendLvl;
    94.                 XPScript.XP = FriendCurrentXP;
    95.                 XPScript.MaxXP = FriendMaxXP;
    96.  
    97.                 FriendScriptUI.UpdateUI();
    98.  
    99.                 myFile.Dispose();
    100.  
    101.              }
    102.         }
    103.        
    104.  
    105.     }
    106.  
    107. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Lines 60 to 62 look suspicious to me. How would you tell the different pieces apart? I presume the first argumnet is the key ("Friendlvl") and the second arg is the data... is it just a Dictionary? If so, you need to have unique keys.
     
  3. SwatHound

    SwatHound

    Joined:
    Jun 7, 2018
    Posts:
    14
    ah Kurt, just the individual I was hoping would comment. You helped me with a saving situation about a month ago, thanks for that :p


    I was wondering is there no way to cycle through and say "for every GameObject in this array, save XYZ"

    I guess maybe it is not that simple. Every single saving video i've seen has been more involved with

    "Heres how to save the players hp, items, money, whatever!" but I have not seen one that handles ... I guess.. saving multiple party members stats?


    I guess I was hoping for a scalable solution. Is the answer really to rewrite "NPC1friendlvl, NPC2friendlvl" and so on?...
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    I don't know what all that easy save thing does, but for serialization, it's generally not adequate to merely save all the changing data.

    Somehow you have to discern what is what when it comes back so there has to be some mechanism to correlate these things back to where they go.

    It would sort of be like writing down ONLY the phone numbers of all of your friends in a giant list, then picking that list up a year later and now you have no idea which friend correlates to which number. Technically to save the phone numbers you don't need to save the friends, since you know who your friends are. But practically you have to get the two pieces of information back together to reconstruct something useful.

    I've scribbled about this in the past: Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384
     
  5. SwatHound

    SwatHound

    Joined:
    Jun 7, 2018
    Posts:
    14
    I'll look more into that post, thanks again Kurt-Dekker.