/// <summary> /// Removes specified item if it exists. Also tells FPEInteractionManager to execute the 'consume' script /// to ensure the item's effects take place in the game world. /// </summary> /// <param name="instanceIDToConsume">The GameObject instance ID of the inventory item to drop.</param> /// <returns>Amount of this item left in inventory post-removal</returns> public int consumeItemFromInventory(int instanceIDToConsume) { int howManyLeftAfterRemoval = -1; FPEInteractableInventoryItemScript itemToConsume = removeInventoryItem(instanceIDToConsume); if (itemToConsume != null) { howManyLeftAfterRemoval = inventoryQuantities[(int)itemToConsume.InventoryItemType]; FPEInteractionManagerScript.Instance.consumeObjectFromInventory(itemToConsume); } else { Debug.LogError("FPEInventoryManagerScript.consumeItemFromInventory() could not find inventory item with instanceID '" + instanceIDToConsume + "'"); } return(howManyLeftAfterRemoval); }
/// <summary> /// Removes item from inventory based on instance ID (e.g. if calling from inventory screen) /// </summary> /// <param name="instanceIDToRemove">The game object instance ID to remove</param> /// <returns></returns> private FPEInteractableInventoryItemScript removeInventoryItem(int instanceIDToRemove) { FPEInteractableInventoryItemScript itemToDrop = null; for (int i = 0; i < inventoryItems.Count; i++) { if (inventoryItems[i].gameObject.GetInstanceID() == instanceIDToRemove) { itemToDrop = inventoryItems[i]; if (itemToDrop.Stackable) { // If we have exactly one left, then we need to remove the actual one we have in the list from the list. if (inventoryQuantities[(int)itemToDrop.InventoryItemType] == 1) { inventoryItems.RemoveAt(i); } // Otherwise, we have more than one left. We need to create a clone of the only one we actually have in our list, and use that. else { GameObject clone = Instantiate(itemToDrop.gameObject); clone.name = itemToDrop.name; itemToDrop = clone.GetComponent <FPEInteractableInventoryItemScript>(); } } else { inventoryItems.RemoveAt(i); } inventoryQuantities[(int)itemToDrop.InventoryItemType]--; break; } } return(itemToDrop); }
/// <summary> /// Gives specified quanity of specified inventory item type. Returns true if we need to keep the /// original around. False if we can discard it (basically if it was stackable, we fake it) /// </summary> /// <param name="item">The item to add</param> /// <returns>True if we need to keep the instance we added in the world. False if we do /// not (e.g. if was stackable and we already had some we ddon't need to keep the 2nd to nth /// ones)</returns> public bool addInventoryItem(FPEInteractableInventoryItemScript item) { bool needToKeepInstanceInWorld = true; if (item.Stackable) { if (inventoryQuantities[(int)item.InventoryItemType] == 0) { inventoryItems.Add(item); } else { needToKeepInstanceInWorld = false; } } else { inventoryItems.Add(item); } inventoryQuantities[(int)item.InventoryItemType]++; return(needToKeepInstanceInWorld); }
/// <summary> /// Restores player's inventory (items, audio diaries, notes) /// </summary> /// <param name="data">The data from which to base restored inventory</param> public void restoreInventorySaveData(FPEInventorySaveData data) { FPEInventoryManagerScript invManager = FPEInventoryManagerScript.Instance; // Clear existing inventory invManager.clearInventoryItems(); // Held Object FPEHeldObjectSaveData heldObjData = data.HeldObjectData; #region HELD_OBJECT if (heldObjData.HeldSomething) { GameObject tempLoadedObject = null; // Pickup Type if (heldObjData.PickupPrefabName != "") { Object tempObject = Resources.Load(FPEObjectTypeLookup.PickupResourcePath + heldObjData.PickupPrefabName); if (tempObject != null) { tempLoadedObject = Instantiate(tempObject) as GameObject; tempLoadedObject.name = heldObjData.PickupPrefabName; tempLoadedObject.transform.localRotation = heldObjData.LocalRotation; } else { Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown Pickup named '" + heldObjData.PickupPrefabName + "'. No prefab was found with this name. This object will NOT be loaded. Ensure that all Pickup prefabs are located in the '" + FPEObjectTypeLookup.PickupResourcePath + "' sub folder of a Resources folder."); } } // Inventory Type else { if (inventoryLookupTable.ContainsKey(heldObjData.InventoryItemType)) { string tempPath = ""; if (inventoryLookupTable.TryGetValue(heldObjData.InventoryItemType, out tempPath)) { tempLoadedObject = Instantiate(Resources.Load(FPEObjectTypeLookup.InventoryResourcePath + tempPath)) as GameObject; tempLoadedObject.transform.localRotation = heldObjData.LocalRotation; } else { Debug.LogError("FPESaveLoadLogic:: Loading data could not get value for InventoryItemType '" + heldObjData.InventoryItemType + "'"); } } else { Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown InventoryItemType '" + heldObjData.InventoryItemType + "'. This object will NOT be loaded. Ensure that all Inventory Item prefabs are located in the '" + FPEObjectTypeLookup.InventoryResourcePath + "' sub folder of a Resources folder. Also ensure that there is an entry in the FPEObjectTypeLookup 'inventoryItemsLookup' Dictionary for type '" + heldObjData.InventoryItemType + "'"); } } // Lastly, put the object into player's hand FPEInteractablePickupScript pickup = tempLoadedObject.GetComponent <FPEInteractablePickupScript>(); if (pickup) { FPEInteractionManagerScript.Instance.holdObjectFromGameLoad(pickup); } else { Debug.LogError("FPESaveLoadLogic:: Loading held object for object '" + tempLoadedObject.gameObject.name + "', but its prefab had no attached FPEInteractablePickupScript component. Object will not be loaded. Check prefab."); } } #endregion #region INVENTORY_ITEMS FPEInventoryItemSaveData[] loadedItemData = data.InventoryItemData; // Create all the items, and add each to inventory GameObject tempInvObject = null; string tempInvPath; FPEInteractableInventoryItemScript tempInvItem = null; // We start at index 1 to skip over our padded 0th value for (int i = 0; i < loadedItemData.Length; i++) { if (inventoryLookupTable.ContainsKey(loadedItemData[i].InventoryItemType)) { // Added this as an if, did that break things? if (inventoryLookupTable.TryGetValue(loadedItemData[i].InventoryItemType, out tempInvPath)) { tempInvObject = Instantiate(Resources.Load(FPEObjectTypeLookup.InventoryResourcePath + tempInvPath)) as GameObject; tempInvItem = tempInvObject.GetComponent <FPEInteractableInventoryItemScript>(); if (tempInvItem != null) { FPEInteractionManagerScript.Instance.putObjectIntoInventory(tempInvItem, false); } else { Debug.LogError("FPESaveLoadLogic:: Loaded Inventory Item prefab '" + FPEObjectTypeLookup.InventoryResourcePath + tempInvPath + "' had no attached FPEInteractableInventoryItemScript component. Item will NOT be restored into player inventory"); } } else { Debug.LogError("FPESaveLoadLogic:: Loading data could not get value for InventoryItemType '" + loadedItemData[i].InventoryItemType + "'"); } } else { Debug.LogError("FPESaveLoadLogic:: Loading data encountered unknown InventoryItemType '" + loadedItemData[i].InventoryItemType + "'. This object will NOT be restored into player inventory. Ensure that there is an entry in the FPEObjectTypeLookup 'inventoryItemsLookup' Dictionary for type '" + loadedItemData[i].InventoryItemType + "'"); } } #endregion #region NOTES_AND_AUDIO_DIARIES FPENoteSaveData[] loadedNoteData = data.NoteData; List <FPENoteEntry> noteEntries = new List <FPENoteEntry>(); for (int n = 0; n < loadedNoteData.Length; n++) { noteEntries.Add(loadedNoteData[n].getNoteEntry()); } invManager.setNoteDataFromSavedGame(noteEntries); FPEAudioDiaryEntrySaveData[] loadedDiaryData = data.AudioDiaryData; List <FPEAudioDiaryEntry> diaryEntries = new List <FPEAudioDiaryEntry>(); for (int a = 0; a < loadedDiaryData.Length; a++) { diaryEntries.Add(loadedDiaryData[a].getAudioDiaryEntry()); } invManager.setAudioDiaryDataFromSavedGame(diaryEntries); #endregion }