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.
playersData.diArray = rd;
using System;
[Serializable]
public class PlayersData
{
public int level;
public string pNameLeft;
public string pNameRight;
public int LHealth;
public int RHealth;
public int LGold;
public int RGold;
public string[] lArray;
public string[] rArray;
public struct[] diArray;
}
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.
public class Database_Script : MonoBehaviour {
public PlayersData playersData;
public struct defenceItem{
public string name;
public int HP;
public float pX;
public float pY;
public float pZ;
public float sX;
public float sY;
public float sZ;
};
public defenceItem[] rd;
public void saveGame(){
Stream stream = File.Open(Application.dataPath + SAVE_FILE + FILE_EXTENSION, FileMode.OpenOrCreate);
BinaryFormatter bf = new BinaryFormatter();
playersData.level = gameScript.level;
playersData.isCameraRight = (Camera.main.transform.position.x > 0)? true : false ;
playersData.pNameLeft = lCastleScript.playerName;
playersData.pNameRight = rCastleScript.playerName;
addDisposableObjects();
}
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.
void addDisposableObjects(){
GameObject go = GameObject.Find ("disposable_gameObjects");
int count = go.transform.childCount;
rd = new defenceItem[count];
for(int i = 0 ; i < count ; i++){
print("There is a: " + go.transform.GetChild(i).name);
rd[i].name = go.transform.GetChild(i).name;
rd[i].HP = (go.transform.GetChild(i).name == "Dragon_Red")? go.transform.GetChild(i).GetComponent<Script_Dragon_R>().HPdamage : 0;
rd[i].pX = go.transform.GetChild(i).position.x;
rd[i].pY = go.transform.GetChild(i).position.y;
rd[i].pZ = go.transform.GetChild(i).position.z;
rd[i].sX = go.transform.GetChild(i).localScale.x;
rd[i].sY = go.transform.GetChild(i).localScale.y;
rd[i].sZ = go.transform.GetChild(i).localScale.z;
}
}
Thanks.