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.
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*.itemName != null)*
{
toSave.saveitemName = inventory*.itemName;*
toSave.saveitemID = inventory*.itemID;*
toSave.saveitemDam = inventory*.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 != null)
{
inventory_.itemName = toLoad.saveitemName*;
inventory.itemID = toLoad.saveitemID;
inventory.itemDam = toLoad.saveitemDam;
}*_
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];
}