- Home /
Serialize Variables from Items in an Array
Hello, I have created a base class with a few variables able to be assigned from it. In another script, I have created an array of this class. Would it be possible to create these classes here, in the array, and assign their variables? Sorry if this makes no sense, and thanks in advance.
Answer by nicholasw1816 · Dec 08, 2018 at 09:34 PM
Use a forloop and store each item, and save it in a file. You'll need a public [System.Serializable] class as well... better yet... Here's the sweet goodness code BOIYY!!!$$$. Feel free to ask me questions. I'll help you with integrating it into your setup/scripts. And just in case you need to save them in a file, I added that aswell. If you don't need to save them in a file, I will simplify it for you.
public class loadSaveFunctions : MonoBehaviour
{
//===============================================
//Item = your class && inventory == your array of that class
//===============================================
public List<Item> inventory;
public int maxItems = 50;
//===============================================
//Below is, Saving each classes variables from the inventory array
//into a file.
//===============================================
public void SaveSlotOne()
{
BinaryFormatter Ibf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/Inventory1.dat");
InventorySaver toSave = new InventorySaver();
//Now loop, and save each item
for (int i = 0; i < inventory.Count; i++)
{
if (inventory[i].itemName != null)
{
toSave.saveitemName[i] = inventory[i].itemName;
toSave.saveitemID[i] = inventory[i].itemID;
toSave.saveitemDam[i] = inventory[i].itemDam;
}
if (i == maxItems)
{
Ibf.Serialize(file, toSave);
file.Close();
break;
}
}
}
//===============================================
//Below is, Loading each class variables from save file
//into your inventory array.
//===============================================
private void LoadSlotOne()
{
if (File.Exists(Application.persistentDataPath + "/Inventory1.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/Inventory1.dat", FileMode.Open);
InventorySaver toLoad = (InventorySaver)bf.Deserialize(file);
file.Close();
//Now loop, and load each item
for (int i = 0; i < inventory.Count; i++)
{
if (toLoad.saveitemName[i] != null)
{
inventory[i].itemName = toLoad.saveitemName[i];
inventory[i].itemID = toLoad.saveitemID[i];
inventory[i].itemDam = toLoad.saveitemDam[i];
}
if (i == maxItems)
{
break;
}
}
}
}
}
//=====================================
//Below is, The public serializable class I said to add
//but NOT in Mono
//=====================================
[System.Serializable]
public class InventorySaver
{
//size of array = maxItems
public string[] saveitemName = new string[50];
public int[] saveitemID = new int[50];
public float[] saveitemDam = new float[50];
}
Your answer
Follow this Question
Related Questions
Why is my C# array of objects fully populated when some of them should be null? 3 Answers
Finding the Sum of Values of Multiple GameObjects in an Array + Variable Sized arrays 0 Answers
Serialized Array: Trying to wrap my head around it 1 Answer
Array not updating in inspector 0 Answers
Multiple Cars not working 1 Answer