public void AddChest(string chest) { bool isNewChest = true; for (int i = 0; i < inventory.chests.Count; i++) { if (inventory.chests[i].name == chest) { isNewChest = false; ChestInInventory chestToEdit = inventory.chests[i]; chestToEdit.amt += 1; inventory.chests[i] = chestToEdit; //Debug.Log("Added"); break; } } if (isNewChest) { ChestInInventory newChest = new ChestInInventory(); newChest.amt = 1; newChest.name = chest; inventory.chests.Add(newChest); Debug.Log("Added"); } UpdateChestUI(); SaveInventory(); }
public void RemoveChest(string chest) { for (int i = 0; i < inventory.seeds.Count; i++) { ChestInInventory chestToEdit = new ChestInInventory(); if (inventory.chests[i].name == chest) { chestToEdit = inventory.chests[i]; chestToEdit.amt -= 1; inventory.chests.RemoveAt(i); if (chestToEdit.amt > 0) { inventory.chests.Insert(i, chestToEdit); } Debug.Log("Removed chest: " + chest); UpdateChestUI(); SaveInventory(); return; } } Debug.Log("Couldn't remove chest: " + chest); UpdateChestUI(); SaveInventory(); }
void LoadInventory() { //Debug.Log("LOADING INVENTORY"); string filePath = Application.persistentDataPath + fileName; //string filePath = fileName; if (File.Exists(filePath)) { string dataAsJson = File.ReadAllText(filePath); JsonInventory jsonInventory = JsonUtility.FromJson <JsonInventory>(dataAsJson); Inventory newInventory = new Inventory() { eggs = new List <EggInInventory>(), seeds = new List <SeedInInventory>(), islands = new List <IslandStorage>(), chests = new List <ChestInInventory>() }; newInventory.currency = jsonInventory.currencyAmt; currencyUI.GetComponent <Text>().text = newInventory.currency.ToString(); for (int i = 0; i < jsonInventory.eggs.Length; i++) { EggInInventory newEgg = new EggInInventory() { name = jsonInventory.eggs[i], amt = jsonInventory.eggAmts[i] }; newInventory.eggs.Add(newEgg); } for (int i = 0; i < jsonInventory.seeds.Length; i++) { SeedInInventory newSeed = new SeedInInventory() { name = jsonInventory.seeds[i], amt = jsonInventory.seedAmts[i] }; newInventory.seeds.Add(newSeed); } for (int i = 0; i < jsonInventory.chests.Length; i++) { ChestInInventory newChest = new ChestInInventory() { name = jsonInventory.chests[i], amt = jsonInventory.chestAmts[i] }; newInventory.chests.Add(newChest); } IslandStorage newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits0.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits0[i]; newFruitInInventory.amt = jsonInventory.fruitAmts0[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits1.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits1[i]; newFruitInInventory.amt = jsonInventory.fruitAmts1[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); newStorage = new IslandStorage() { fruitInStorage = new List <FruitInInventory>() }; for (int i = 0; i < jsonInventory.fruits0.Length; i++) { FruitInInventory newFruitInInventory = new FruitInInventory(); newFruitInInventory.name = jsonInventory.fruits2[i]; newFruitInInventory.amt = jsonInventory.fruitAmts2[i]; newStorage.fruitInStorage.Add(newFruitInInventory); //Debug.Log("Added fruit"); } newInventory.islands.Add(newStorage); inventory = newInventory; //Debug.Log("LOADED INVENTORY"); } else { SaveInventory(); } }