Search Unity

Saving a Struct issue.

Discussion in 'Scripting' started by larswik, Nov 19, 2017.

  1. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    I have game data that I am saving to the hard drive when the user hit save and I hits a save button. I have a Class called PlayerData that I use to store variables in to and then save it. I am having an issue with the Struct Array that I created not being recognized in the Player Class where I am assigning it. The String[] arrays work fine, but not the struct.

    The last part is where I am stuck. Once the 'rd[]' array is finished I should be assigning it like this but the diArray in the Player Class won't recognize a struct and get an error, so I can't assign it. If I explained the problem well enough, how can I fix it? The 1 line of code below is what I need to do. But on line 14 of PlayerData I get 3 errors "Unexpected [". Unexpected ]" and Unexpected ";". I am creating a Struct array in the Database script and need to send that to the PlayerData script to save.

    Code (CSharp):
    1. playersData.diArray = rd;
    Code (CSharp):
    1. using System;
    2. [Serializable]
    3.     public class PlayersData
    4.     {
    5.         public int level;
    6.         public string pNameLeft;
    7.         public string pNameRight;
    8.         public int LHealth;
    9.         public int RHealth;
    10.         public int LGold;
    11.         public int RGold;
    12.         public string[] lArray;
    13.         public string[] rArray;
    14.         public struct[] diArray;
    15.     }
    I have a GameObject that has a Database_Script attached to it that I use to set and get the saved game where I instantiate the the PlayerData class.

    Code (CSharp):
    1. public class Database_Script : MonoBehaviour {
    2. public PlayersData playersData;
    3. public struct defenceItem{
    4.         public string name;
    5.         public int HP;
    6.         public float pX;
    7.         public float pY;
    8.         public float pZ;
    9.         public float sX;
    10.         public float sY;
    11.         public float sZ;
    12.     };
    13. public defenceItem[] rd;
    14.  
    15. public void saveGame(){
    16.         Stream stream = File.Open(Application.dataPath + SAVE_FILE + FILE_EXTENSION, FileMode.OpenOrCreate);
    17.         BinaryFormatter bf = new BinaryFormatter();
    18.  
    19.         playersData.level = gameScript.level;
    20.         playersData.isCameraRight = (Camera.main.transform.position.x > 0)? true : false ;
    21.         playersData.pNameLeft = lCastleScript.playerName;
    22.         playersData.pNameRight = rCastleScript.playerName;
    23. addDisposableObjects();
    24. }
    There is a method that I use to get all the on screen GameObjects to save some data of theirs and then add them to the struct[] array.

    Code (CSharp):
    1. void addDisposableObjects(){
    2.         GameObject go = GameObject.Find ("disposable_gameObjects");
    3.         int count = go.transform.childCount;
    4.         rd = new defenceItem[count];
    5.         for(int i = 0 ; i < count ; i++){
    6.             print("There is a: " + go.transform.GetChild(i).name);
    7.             rd[i].name = go.transform.GetChild(i).name;
    8.             rd[i].HP = (go.transform.GetChild(i).name == "Dragon_Red")? go.transform.GetChild(i).GetComponent<Script_Dragon_R>().HPdamage : 0;
    9.  
    10.             rd[i].pX = go.transform.GetChild(i).position.x;
    11.             rd[i].pY = go.transform.GetChild(i).position.y;
    12.             rd[i].pZ = go.transform.GetChild(i).position.z;
    13.  
    14.             rd[i].sX = go.transform.GetChild(i).localScale.x;
    15.             rd[i].sY = go.transform.GetChild(i).localScale.y;
    16.             rd[i].sZ = go.transform.GetChild(i).localScale.z;
    17.         }
    18.     }
    Thanks.
     
  2. Havokki

    Havokki

    Joined:
    Jun 28, 2015
    Posts:
    118
    Code (CSharp):
    1. public struct[] diArray;
    You need to specify what struct you want to have there. Now you're only telling that there should an array of some unknown structs.

    As far as I understand, you want this:
    Code (CSharp):
    1. public defenceItem[] diArray;
     
  3. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Thanks for the assistance. I spent most of today working on it and I get one error or another. I am trying to add the structs to a Dictionary or Array and I seems to get the same error.

    I have spent most of the day looking this up and reading about it but I am not getting it.

    My Player Class seems to be the issue.
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3.  
    4. [Serializable]
    5.  
    6.     public class PlayersData
    7.     {
    8.  
    9.     public struct defenceItemPlayer{
    10.         public string name;
    11.         public int HP;
    12.         public float pX;
    13.         public float pY;
    14.         public float pZ;
    15.         public float sX;
    16.         public float sY;
    17.         public float sZ;
    18.  
    19.     }
    20.  
    21.          public defenceItemPlayer diStruct;
    22.         public Dictionary<int, defenceItemPlayer> structDict = new Dictionary<int, defenceItemPlayer>();
    in my Database script I try to add the items to the struct and then add the struct to the Dictionary with this method.

    Code (CSharp):
    1.  
    2.     void addDisposableObjects(){
    3.  
    4.         GameObject go = GameObject.Find ("disposable_gameObjects");
    5.         int count = go.transform.childCount;
    6.  
    7.         for(int i = 0 ; i < count ; i++){
    8.             playersData.diStruct = new PlayersData.defenceItemPlayer();
    9.  
    10.             playersData.diStruct.name = go.transform.GetChild(i).name;
    11.             playersData.diStruct.HP = (go.transform.GetChild(i).name == "Dragon_Red")? go.transform.GetChild(i).GetComponent<Script_Dragon_R>().HPdamage : 0;
    12.  
    13.             playersData.diStruct.pX = go.transform.GetChild(i).position.x;
    14.             playersData.diStruct.pY = go.transform.GetChild(i).position.y;
    15.             playersData.diStruct.pZ = go.transform.GetChild(i).position.z;
    16.  
    17.             playersData.diStruct.sX = go.transform.GetChild(i).localScale.x;
    18.             playersData.diStruct.sY = go.transform.GetChild(i).localScale.y;
    19.             playersData.diStruct.sZ = go.transform.GetChild(i).localScale.z;
    20.  
    21.             playersData.structDict.Add(i, playersData.diStruct);
    22.         }
    23.     }
    24.  
    Thanks again for the help.
     
  4. Havokki

    Havokki

    Joined:
    Jun 28, 2015
    Posts:
    118
    The serialization error is referring to a missing "[Serializable]". Add that attribute to your defenceItemPlayer the same way you have added it to PlayersData. The attribute does not automatically apply to all nested types, so they need to be marked serializable as well.
     
  5. larswik

    larswik

    Joined:
    Dec 20, 2012
    Posts:
    312
    Hi Yozaro, I am back from the holidays and digging in to this project again. I followed your instructions and it worked just fine. This was the first time I have ever tried to save a game and use [Serialize]. I thought you only needed to declare this once. I was taking the Scope in to account and thought that it would have also covered the defenceItemPlayer Struct, but I guess not. Thanks again for your help solving this problem!

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3.  
    4. [Serializable]
    5.  
    6.     public class PlayersData
    7.     {
    8.     [Serializable]
    9.     public struct defenceItemPlayer{
    10.         public string name;
    11.         public int HP;
    12.         public float pX;
    13.         public float pY;
    14.         public float pZ;
    15.         public float sX;
    16.         public float sY;
    17.         public float sZ;
    18.  
    19.     }