/* This function takes in a list of strings (stored in the Inventory helper class) and returns a list * of InventoryItems. We need to do this because Unity doesn't serialize ScriptableObjects with JSON */ public List <InventoryItem> ParseInventoryFromReference(Inventory inventory) { List <InventoryItem> parsedInventory = new List <InventoryItem>(); foreach (string item in inventory.savedInventory) { /* This code is very complex for beginners to wrap their head around, but suffice it to say that we are using the * System.Linq include which lets us sort and manage lists in a very shorthand form. What we are actually doing * is querying a list of the custom struct HashtablePair (Declared at the bottom of the script) for the string/ID * we are looking for, and if we find it we return the associated InventoryItem */ if (hashtable.Any(toCheck => toCheck.key == item)) { HashtablePair toAdd = hashtable.SingleOrDefault(toCheck => toCheck.key == item); parsedInventory.Add(toAdd.value); } else { Debug.LogWarning("Could not parse item with key: '" + item + "'"); } } return(parsedInventory); }
void Update() { //This code is only executed in Edit mode. In here we update our list of references if (!Application.IsPlaying(gameObject)) { //We start by loading in all the ScriptableObjects we can find of the correct type and putting them in an array Resources.LoadAll("", typeof(InventoryItem)); InventoryItem[] availableItemsHelper = Resources.FindObjectsOfTypeAll <InventoryItem>(); //Using the HashtablePair struct (declared at the bottom of this class) we create a list of ScriptableObjects and an identifier (key) we can search for that object with hashtable = new List <HashtablePair>(); foreach (InventoryItem item in availableItemsHelper) { HashtablePair nextPair = new HashtablePair { value = item, key = item.name }; hashtable.Add(nextPair); } } }