public void UseItem(Transform Item, int n) { Item EquipmentItemScript = Item.GetComponent <Item>(); //Getting the item script. if (n == -1) //If we didn't define the target equipment slot. { for (int t = 0; t < InvEquipment.EquipmentSlots.Length; t++) //Loop through all the equipment slots to find one. { if (InvEquipment.EquipmentSlots[t].Name == EquipmentItemScript.EquipmentSlot) { n = t; } } } if (n != -1) { if (InvEquipment.EquipmentSlots[n].Name == EquipmentItemScript.EquipmentSlot) //Check if the item equipment slots matches the current equipment slot. { /*InvManager.RemoveItem(Item, 1); //Remove this item from the inventory content. * CustomEvents.OnPlayerEquipItem(EquipmentItemScript.Name, EquipmentItemScript.EquipmentSlot);*/ if (InvEquipment.EquipmentSlots[n].IsTaken == false) //Check if the slot is not taken. { EquippedItem = InvManager.FreeItem(Item, 1); //Free this item from the inventory content. CustomEvents.OnPlayerEquipItem(EquipmentItemScript, InvEquipment.EquipmentSlots[n].GroupID); //Add the item to the equipment slot IsEquippingItem = true; EquippedItemSlot = n; //Changing the settings of the item and parenting it to the inventory manager again. if (EquippedItem.GetComponent <Collider>() != null) { EquippedItem.transform.GetComponent <Collider>().isTrigger = true; } EquippedItem.transform.GetComponent <Renderer>().enabled = false; EquippedItem.transform.parent = gameObject.transform; EquippedItem.transform.localPosition = Vector3.zero; EquippedItem.gameObject.SetActive(false); return; } else if (InvEquipment.EquipmentSlots[n].IsTaken == true) //Check if the slot is taken. { Item TempScript = InvEquipment.EquipmentSlots[n].Item.GetComponent <Item>(); if (InvManager.Exist(InvEquipment.EquipmentSlots[n].Item) && TempScript.Name == EquipmentItemScript.Name) //If we already have that item in the inventory. { //No action. return; } else if (InvManager.Items < InvManager.MaxItems) //If we still have room for a new item: { InvManager.AddItem(InvEquipment.EquipmentSlots[n].Item); //Remove this item from the equipment slot and add it to the inventory. CustomEvents.OnPlayerUnEquipItem(TempScript, InvEquipment.EquipmentSlots[n].GroupID); InvEquipment.DesactivateEqObj(TempScript.Name); for (int x = 0; x < TempScript.Attributes.Length; x++) { for (int j = 0; j < InvEquipment.Attributes.Length; j++) { if (InvEquipment.Attributes[j].Name == TempScript.Attributes[x].Name) { InvEquipment.Attributes[j].Value -= TempScript.Attributes[x].Value; } } } EquippedItem = InvManager.FreeItem(Item, 1); //Free the item from the inventory content. CustomEvents.OnPlayerEquipItem(EquipmentItemScript, InvEquipment.EquipmentSlots[n].GroupID); //Add the item to the equipment slot IsEquippingItem = true; EquippedItemSlot = n; //Changing the settings of the item and parenting it to the inventory manager again. if (EquippedItem.GetComponent <Collider>() != null) { EquippedItem.transform.GetComponent <Collider>().isTrigger = true; } EquippedItem.transform.GetComponent <Renderer>().enabled = false; EquippedItem.transform.parent = gameObject.transform; EquippedItem.transform.localPosition = Vector3.zero; EquippedItem.gameObject.SetActive(false); return; } else { Debug.Log("No room for a new item!"); return; } } } } }
void LoadEquipments() { int TempInt = PlayerPrefs.GetInt("SavedEquipments"); //Getting the number of the saved equipment slots GameObject TempObj; for (int i = 1; i < TempInt; i++) //Starting a loop in the equipment slots. { if (Resources.Load(PlayerPrefs.GetString("EqName" + i.ToString()) + "C", typeof(GameObject))) { TempObj = Instantiate(Resources.Load(PlayerPrefs.GetString("EqName" + i.ToString()) + "C", typeof(GameObject)), MyTransform.position, MyTransform.rotation) as GameObject; } else { TempObj = Instantiate(DefaultItem, MyTransform.position, MyTransform.rotation).gameObject; } Item ItemScript = TempObj.GetComponent <Item>(); //Getting the item script. ItemScript.ItemType = PlayerPrefs.GetInt("EqItemType" + i.ToString()); //Loading the item's type ItemScript.MinDistance = PlayerPrefs.GetFloat("EqMinDistance" + i.ToString()); //Loading the item's pickup minimum distance. ItemScript.Name = PlayerPrefs.GetString("EqName" + i.ToString()); //Loading the item's name. ItemScript.Amount = PlayerPrefs.GetInt("EqAmount" + i.ToString()); //Loading the item's amount. ItemScript.Weight = PlayerPrefs.GetInt("EqWeight" + i.ToString(), 1); //Loading the item's weight ItemScript.ShortDescription = PlayerPrefs.GetString("EqShortDescription" + i.ToString()); //Loading the item's short description. ItemScript.Icon = Resources.Load(ItemScript.Name, typeof(Sprite)) as Sprite; ItemScript.Currency = PlayerPrefs.GetString("EqCurrency" + i.ToString()); //Loading the item's currency type. ItemScript.CurrencyAmount = PlayerPrefs.GetInt("EqCurrencyAmount" + i.ToString()); //Loading the item's currency amount. ItemScript.EquipmentSlot = PlayerPrefs.GetString("EqEquipmentSlot" + i.ToString()); //Loading the item's equipment slot. ItemScript.SkillSlot = PlayerPrefs.GetString("EqSkillSlot" + i.ToString()); //Loading the item's skill bar slot. ItemScript.IsStackable = false; //Loading the item's equipment attributes: if (PlayerPrefs.GetString("EqHasAttributes" + i.ToString()) == "Yes") { ItemScript.Attributes = new Item.ItemAttributes[PlayerPrefs.GetInt("EqAttributesAmount" + i.ToString())]; int a = 0; for (a = 0; a < ItemScript.Attributes.Length; a++) { ItemScript.Attributes[a] = new Item.ItemAttributes(); } for (a = 0; a < ItemScript.Attributes.Length; a++) { ItemScript.Attributes[a].Name = PlayerPrefs.GetString("EqAttributeName" + a.ToString() + i.ToString()); ItemScript.Attributes[a].Value = PlayerPrefs.GetInt("EqAttributeValue" + a.ToString() + i.ToString()); } } //Adding the loaded items to their equipment slots: for (int t = 0; t < EquipmentSlots.Length; t++) //Starting a loop in the equipment slots. { if (EquipmentSlots[t].Name == ItemScript.EquipmentSlot && PlayerPrefs.GetInt("EqSlotID" + i.ToString()) == t) //If the current loaded item matches one of the equipment slots. { //Equip the item and set the slot to taken. EquipmentSlots[t].Item = TempObj.transform; EquipmentSlots[t].IsTaken = true; CustomEvents.OnPlayerEquipItem(ItemScript, EquipmentSlots[t].GroupID); //Changing the settings of the item and parenting it to the inventory manager again. if (EquipmentSlots[t].Item.GetComponent <Collider>() != null) { EquipmentSlots[t].Item.transform.GetComponent <Collider>().isTrigger = true; } EquipmentSlots[t].Item.transform.GetComponent <Renderer>().enabled = false; EquipmentSlots[t].Item.transform.parent = gameObject.transform; EquipmentSlots[t].Item.transform.localPosition = Vector3.zero; EquipmentSlots[t].Item.gameObject.SetActive(false); ActivateEqObj(ItemScript.Name); } } for (int x = 0; x < ItemScript.Attributes.Length; x++) { for (int j = 0; j < Attributes.Length; j++) { if (Attributes[j].Name == ItemScript.Attributes[x].Name) { Attributes[j].Value += ItemScript.Attributes[x].Value; } } } } }