Hi all,
I wrote this script a few months ago and feel the need to improve it, but I’m not really sure what the best way to go about it would be. There aren’t any performance issues so far, but I think it’s preatty hard to read and follow what the script is doing.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class ItemSlot : MonoBehaviour, IDropHandler
{
public int slotType, bagSlot, storedCount, buildingNum;
public GameObject slotItem;
public Transform bagWindow;
public Text bagWinName;
public WindowManager windowManager;
public GameObject newItem;
public ItemSlot associatedBag;
public LootTest lootTest;
public resourceLoot resourceGather;
public instanceReference instanceRef;
public buildingPlot buildingInv;
[SerializeField]
GameObject[] storedItems;
WaitForSeconds shortPause = new WaitForSeconds(0.1f);
void Start()
{
if (slotType == 20)
{
if (windowManager == null)
{
windowManager = FindObjectOfType<WindowManager>();
}
if (transform.GetComponentInChildren<ItemDragging>() != null)
{
windowManager.bagSlotted[bagSlot - 1] = true;
var slots = transform.GetComponentInChildren<itemInfo>();
bagWinName.text = slots.info.itemName;
GameObject child = transform.GetChild(0).FindChild("BagButton").gameObject;
child.SetActive(true);
child.GetComponent<ButtonID>().invBagNum = bagSlot;
makeInvSlots(slots.info.modification1);
}
else
{
windowManager.bagSlotted[bagSlot - 1] = false;
}
}
else
{
if (transform.GetComponentInChildren<ItemDragging>() != null)
{
GameObject child = transform.GetChild(0).FindChild("BagButton").gameObject;
child.SetActive(false);
}
}
}
public IEnumerator setupStuff()
{
itemInfo newItemInfo = newItem.GetComponent<itemInfo>();
ItemDragging newItemDrag = newItem.GetComponent<ItemDragging>();
ItemSlot newItemOldSlot = newItemDrag.startParent.GetComponent<ItemSlot>();
if (newItemInfo.info.typeID == slotType) //Does the item match the slot it's being put in to? Special slots, where only 1 item type is expected to be put in. Items do not stack
{
#region bagslot
if (slotType == 20) //Is this a bag slot?
{
if (transform.childCount > 0) //Is there a bag already slotted?
{
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
if (storedCount > 0 && newItemInfo.info.modification1 >= storedCount) //Are there items in this bag and can the new bag fit them?
{
if (newItemOldSlot.slotType == 20 && newItemOldSlot.transform != transform) //Was the old slot a bag slot and not this bag slot?
{
if (newItemOldSlot.storedCount > 0 && curItemInfo.info.modification1 >= newItemOldSlot.storedCount) //Were there items in the old bag and can this bag fit them?
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.startSlotType = slotType; //Set the startSlotType for the current item to this slot type. Issues with comparing startParent type caused by removing slots in preparation of moving items
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
itemsToLimbo(); //Move items in this bag to limbo
removeInvSlots(); //Remove slots in this bag
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
curItemDrag.transform.SetParent(newItemOldSlot.transform); //Moving bag from this slot to the old slot
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
int oldCount = newItemOldSlot.storedCount;
int thisCount = storedCount;
yield return shortPause;
for (var i = 0; i < oldCount; i++) //Put items in old bag into this one
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startParent = newItemOldSlot.bagWindow.GetChild(i).transform;
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startSlotType = 0;
newItemOldSlot.storedItems[i].transform.SetParent(bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount--;
storedCount++;
}
for (var i = 0; i < thisCount; i++) //Move items from this bag to the old one
{
newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = storedItems[i];
storedItems[i].GetComponent<ItemDragging>().startParent = bagWindow.GetChild(i).transform;
storedItems[i].GetComponent<ItemDragging>().startSlotType = 0;
storedItems[i].transform.SetParent(newItemOldSlot.bagWindow.GetChild(i));
storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount++;
storedCount--;
}
newItemOldSlot.Invoke("setupBagButton", 0);
setupBagButton();
}
else if (newItemOldSlot.storedCount == 0) //The old bag didn't have any items
{
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
yield return shortPause;
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.transform.SetParent(newItemOldSlot.transform);
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
itemsToLimbo();
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
removeInvSlots(); //Remove slots in this bag
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
int thisCount = storedCount;
for (var i = 0; i < thisCount; i++) //Move items from this bag to the old one. No need to worry about moving old to new
{
newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = storedItems[i];
storedItems[i].GetComponent<ItemDragging>().startParent = bagWindow.GetChild(i).transform;
storedItems[i].GetComponent<ItemDragging>().startSlotType = 0;
storedItems[i].transform.SetParent(newItemOldSlot.bagWindow.GetChild(i));
storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount++;
storedCount--;
}
newItemOldSlot.Invoke("setupBagButton", 0);
setupBagButton();
}
//No else, because that means that the current bag could not fit the items in the old bag, so the switch is illegal.
}
else if (newItemOldSlot.storedCount >= 900 && newItemOldSlot.storedCount < 999) //Did it come from a building slot?
{
GameObject newItemOldSlotGO = newItemOldSlot.buildingInv.gameObject;
//Else if for other plots
if (storedCount > 0 && newItemInfo.info.modification1 >= storedCount) //Check if there are items that need to be put in the new bag and can the new bag fit them
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject;
curItemDrag.startParent = (transform);
newItem.transform.SetParent(transform);
itemsToLimbo();
removeInvSlots();
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
int listIndex = newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].IndexOf(newItemInfo.info);
Debug.LogWarning("Removing item needs work");
if (listIndex > newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].Count)
{
Debug.LogWarning("Adding item needs work");
}
else
{
Debug.LogWarning("Inserting item needs work");
}
yield return shortPause;
int thisCount = storedCount;
for (var i = 0; i < thisCount; i++) //Move items from limbo to the new slots
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = storedItems[i];
storedItems[i].GetComponent<ItemDragging>().startParent = bagWindow.GetChild(i).transform;
storedItems[i].GetComponent<ItemDragging>().startSlotType = bagWindow.GetChild(i).GetComponent<ItemSlot>().slotType;
storedItems[i].transform.SetParent(bagWindow.GetChild(i));
}
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
}
else if (storedCount == 0) //There are no items to put in the new bag
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject;
curItemDrag.startParent = (transform);
newItem.transform.SetParent(transform);
removeInvSlots();
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
int listIndex = newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].IndexOf(newItemInfo.info);
Debug.LogWarning("Removing item needs work");
//commands.CmdRemoveItem(newItemOldSlotGO, curItemInfo.info);
if (listIndex > newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].Count)
{
Debug.LogWarning("Adding item needs work");
}
else
{
Debug.LogWarning("Inserting item needs work");
}
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
}
//No else, as that means the new bag cannot fit items currently stored, so switch is illegal.
}
else //The old slot is not a bag slot, and not a building slot, must be an inventory slot
{
if (storedCount > 0 && newItemInfo.info.modification1 >= storedCount) //Check if there are items that need to be put in the new bag and can the new bag fit them
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
itemsToLimbo(); //Move items in this bag to limbo
removeInvSlots(); //Remove slots in this bag
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
yield return shortPause;
int thisCount = storedCount;
for (var i = 0; i < thisCount; i++) //Move items from limbo to the new slots
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = storedItems[i];
storedItems[i].GetComponent<ItemDragging>().startParent = bagWindow.GetChild(i).transform;
storedItems[i].GetComponent<ItemDragging>().startSlotType = bagWindow.GetChild(i).GetComponent<ItemSlot>().slotType;
storedItems[i].transform.SetParent(bagWindow.GetChild(i));
//bagWindow.GetChild(i).GetComponent<ItemSlot>().Invoke("setupStuff", 0);
}
curItemDrag.transform.SetParent(newItemOldSlot.transform); //Moving bag from this slot to the old slot
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
//newItemOldSlot.Invoke("setupStuff", 0); //Run setupStuff for the current bag in the old slot
}
else if (storedCount == 0) //There are no items to put in the new bag
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
removeInvSlots(); //Remove slots in this bag
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
}
//No else, as that means that the new bag cannot fit the items currently stored, so the switch is illegal.
}
}
else if (storedCount == 0) //There are no items in this bag
{
if (newItemOldSlot.slotType == 20 && newItemOldSlot.transform != transform) //Is the old slot a bag slot and not this slot?
{
if (newItemOldSlot.storedCount > 0 && transform.GetChild(0).GetComponent<itemInfo>().info.modification1 >= newItemOldSlot.storedCount) //Were there items in the old bag and can this bag fit them?
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
removeInvSlots(); //Remove slots in this bag
yield return shortPause;
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
yield return shortPause;
curItemDrag.transform.SetParent(newItemOldSlot.transform); //Moving bag from this slot to the old slot
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
for (var i = 0; i < newItemOldSlot.storedCount; i++) //Move items from old bag into here
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startParent = newItemOldSlot.bagWindow.GetChild(i).transform;
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startSlotType = newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().slotType;
newItemOldSlot.storedItems[i].transform.SetParent(bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount--;
storedCount++;
//bagWindow.GetChild(i).GetComponent<ItemSlot>().Invoke("setupStuff", 0);
}
newItemOldSlot.Invoke("setupBagButton", 0);
setupBagButton();
}
else if (newItemOldSlot.storedCount == 0) //There are no items in the old bag, so both are empty
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
removeInvSlots();
yield return shortPause;
curItemDrag.transform.SetParent(newItemOldSlot.transform); //Moving bag from this slot to the old slot
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.Invoke("setupBagButton", 0);
setupBagButton();
//newItemOldSlot.Invoke("setupStuff", 0); //Run setupStuff for the current bag in the old slot
}
//No else, as that means the current bag cannot hold the old items, so the switch is illegal.
}
else if(newItemOldSlot.slotType >= 900 && newItemOldSlot.slotType < 901) //Did it come from a building inventory?
{
GameObject newItemOldSlotGO = newItemOldSlot.buildingInv.gameObject;
newItemOldSlot.newItem = transform.GetChild(0).gameObject;
curItemDrag.startParent = (transform);
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform);
removeInvSlots();
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
int listIndex = newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].IndexOf(newItemInfo.info);
Debug.LogWarning("Removing item needs work");
if (listIndex > newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].Count)
{
Debug.LogWarning("Adding item needs work");
//commands.CmdAddItem(newItemOldSlotGO, curItemInfo.info);
}
else
{
Debug.LogWarning("Inserting item needs work");
}
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
}
else //Old slot was not another bag slot or a building, must be general inventory.
{
removeInvSlots();
newItemOldSlot.newItem = transform.GetChild(0).gameObject;
curItemDrag.startParent = (transform);
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform);
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
curItemDrag.transform.Find("BagButton").gameObject.SetActive(false);
curItemDrag.transform.Find("BagButton").GetComponent<ButtonID>().invBagNum = 0;
setupBagButton();
}
}
}
else //No bag currently slotted
{
if (newItemOldSlot.slotType == 20 && newItemDrag.startParent != transform) //Was the old slot a bag slot and not this slot?
{
if (newItemOldSlot.storedCount > 0) //Were there items in the old bag?
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
int oldCount = newItemOldSlot.storedCount;
for (var i = 0; i < oldCount; i++) //Move items from old bag into here
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startParent = newItemOldSlot.bagWindow.GetChild(i).transform;
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startSlotType = newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().slotType;
newItemOldSlot.storedItems[i].transform.SetParent(bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount--;
storedCount++;
//bagWindow.GetChild(i).GetComponent<ItemSlot>().Invoke("setupStuff", 0);
}
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
setupBagButton();
}
else if (newItemOldSlot.storedCount == 0) //Old bag was empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0); //Remove slots in old bag
makeInvSlots(newItemInfo.info.modification1); //Make slots for the new bag
setupBagButton();
}
}
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.slotType < 999) //Did item come from building inventory?
{
GameObject newItemOldSlotGO = newItemOldSlot.buildingInv.gameObject;
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
setupBagButton();
print("New item old slot game object: " + newItemOldSlotGO);
Debug.LogWarning("Removing item needs work");
}
else if (newItemOldSlot.slotType == 999) //Did the item come from loot?
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
setupBagButton();
LootTest itemList = newItemOldSlot.lootTest;
itemList.items.RemoveAt(itemList.items.IndexOf(newItemInfo.info));
}
else if(newItemOldSlot.slotType == 7)
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
setupBagButton();
}
else //Old slot was not a bag slot and not loot, must have been in an inventory slot
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.associatedBag.storedCount--; //Note that an item has been removed from inventory;
setupBagButton();
}
}
}
#endregion
else //If other specific slot type
{
//Nothing, because there aren't any other special slots yet.
}
}
/*else if(slotType == 7 && newItemInfo.info.typeID == 20)
{
ItemSlot slot = GetComponent<backSlot>().bagslot;
if (transform.childCount == 1)
{
if (newItemOldSlot.storedCount > 0) //Were there items in the old bag?
{
if (newItemOldSlot != GetComponent<ItemSlot>())
{
transform.GetChild(0).gameObject.SetActive(false);
GameObject copy = Instantiate(newItem);
copy.transform.SetParent(slot.transform);
slot.newItem = copy;
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int index = newItemInfo.info.modification1;
slot.makeInvSlots(slotcount(index)); //Make slots for the new bag
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
int oldCount = newItemOldSlot.storedCount;
for (var i = 0; i < oldCount; i++) //Move items from old bag into here
{
bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startParent = newItemOldSlot.bagWindow.GetChild(i).transform;
newItemOldSlot.storedItems[i].GetComponent<ItemDragging>().startSlotType = newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().slotType;
newItemOldSlot.storedItems[i].transform.SetParent(slot.bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
newItemOldSlot.storedCount--;
slot.storedCount++;
//bagWindow.GetChild(i).GetComponent<ItemSlot>().Invoke("setupStuff", 0);
}
//newItemOldSlot.removeInvSlots(); //Remove slots in old bag
slot.setupBagButton();
}
}
else if (newItemOldSlot.storedCount == 0) //Old bag was empty
{
if (newItemOldSlot != GetComponent<ItemSlot>())
{
transform.GetChild(0).gameObject.SetActive(false);
GameObject copy = Instantiate(newItem);
copy.transform.SetParent(slot.transform);
slot.newItem = copy;
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
//newItemOldSlot.Invoke("removeInvSlots", 0); //Remove slots in old bag
slot.makeInvSlots(newItemInfo.info.modification1); //Make slots for the new bag
slot.setupBagButton();
}
}
}
}*/
#region buildingSlot
else if (slotType >= 900 && slotType < 999 && newItemInfo.info.typeID != 1) //Is this slot a building inventory slot. Some buildings may accepts different varieties. 999 Is for looting or quest rewards. Nothing will be permitted to be put into it by the player.
{
//Else if for other plots
if (slotType == 900) //Is this general buliding inventory?
{
if (transform.childCount > 0) //Is there already an item in this slot?
{
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
#region fromBag
if (newItemOldSlot.slotType == 20) //Did it come from a bag slot?
{
if (curItemInfo.info.typeID == 20) //Is the item currently in the slot a bag?
{
if (newItemOldSlot.storedCount > 0 && curItemInfo.info.modification1 >= newItemOldSlot.storedCount) //Are there items in the old bag and can the new bag fit them?
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
int oldCount = newItemOldSlot.storedCount;
for (var i = 0; i > oldCount; i++)
{
newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].transform.SetParent(newItemOldSlot.bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
}
int indexNum = buildingInv.buildingInv[buildingNum].IndexOf(curItemInfo.info);
Debug.LogWarning("Removing item needs work");
if (indexNum >= buildingInv.buildingInv[buildingNum].Count)
{
Debug.LogWarning("Adding item needs work");
}
else if(indexNum < buildingInv.buildingInv[buildingNum].Count)
{
Debug.LogWarning("Inserting item needs work");
}
}
else if (newItemOldSlot.storedCount == 0) //Old bag has no items
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0);
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
newItemOldSlot.Invoke("setupBagButton", 0);
int indexNum = buildingInv.buildingInv[buildingNum].IndexOf(curItemInfo.info); //Get the index of the item being removed.
Debug.LogWarning("Removing item needs work"); //Send the command to remove the item from the synclist.
if (indexNum >= buildingInv.buildingInv[buildingNum].Count) //Check if the index is out of range. If it is, add item to the end of the list.
{
Debug.LogWarning("Adding item needs work");
}
else if (indexNum < buildingInv.buildingInv[buildingNum].Count) //If index is not out of range, then insert.
{
Debug.LogWarning("Inserting item needs work");
}
}
//No else, as the current item does not have room for items in the old bag, so the switch is illegal.
}
//No else, as the current item is not a bag, and so cannot switch. Might search for an open slot and put the new item there if not holding items.
}
#endregion
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.slotType < 999 && newItemOldSlot.transform != transform) //Did it come from another building and not from this slot?
{
}
else if(newItemOldSlot.slotType == 0) //Did it come from general inventory?
{
if (newItemInfo.info.typeID == curItemInfo.info.typeID && newItemInfo.info.stackCount + curItemInfo.info.stackCount <= curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
curItemInfo.info.stackCount += newItemInfo.info.stackCount;
curItemInfo.transform.FindChild("Count").GetComponent<Text>().text = curItemInfo.info.stackCount.ToString();
Destroy(newItem.gameObject);
newItemOldSlot.associatedBag.storedCount--;
}
else if (newItemInfo.info.typeID != curItemInfo.info.typeID || newItemInfo.info.stackCount + curItemInfo.info.stackCount > curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
int indexNum = buildingInv.buildingInv[buildingNum].IndexOf(curItemInfo.info);
Debug.LogWarning("Removing item needs work");
if (indexNum >= buildingInv.buildingInv[buildingNum].Count)
{
Debug.LogWarning("Adding item needs work");
}
else if (indexNum < buildingInv.buildingInv[buildingNum].Count)
{
Debug.LogWarning("Inserting item needs work");
}
}
}
}
else //The current slot is empty
{
if (newItemOldSlot.slotType == 20) //Check if the old slot is a bag
{
if (newItemOldSlot.storedCount == 0) //Check if it is empty
{
newItem.transform.SetParent(transform);
newItemOldSlot.removeInvSlots();
print("Index: " + int.Parse(transform.name) + "Count: " + buildingInv.buildingInv[buildingNum].Count);
if(int.Parse(transform.name) <= buildingInv.buildingInv[buildingNum].Count) //Is this slot outside the item range?
{
Debug.LogWarning("Inserting item needs work");
//commands.CmdInsertItem(buildingInv.gameObject, newItemInfo.info, int.Parse(transform.name) - 1);
}
else if (int.Parse(transform.name) > buildingInv.buildingInv[buildingNum].Count) //Is the slot in the item range?
{
Debug.LogWarning("Adding item needs work");
}
}
//No else, as the bag cannot be empty.
}
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.slotType < 999 && newItemOldSlot.transform != transform) //Check if it came from another building and is not the same slot.
{
//Not implemented yet.
}
else if( newItemOldSlot.slotType == 0) //Must have come from general inventory
{
newItem.transform.SetParent(transform);
newItemOldSlot.associatedBag.storedCount -= 1;
Debug.LogWarning("Adding item needs work");
}
}
}
else if (slotType == 901) //Is this a food storage building?
{
if (transform.childCount > 0) //Is there already an item in this slot?
{
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
//Check if it came from a specialty slot
if (newItemDrag.startSlotType == 20) //Did it come from a bag slot? This assumes it was a bag item type.
{
if (curItemInfo.info.typeID == 20) //Is the item currently in the slot a bag?
{
if (newItemOldSlot.storedCount > 0 && curItemInfo.info.modification1 >= newItemOldSlot.storedCount) //Are there items in the old bag and can the new bag fit them?
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
int oldCount = newItemOldSlot.storedCount;
for (var i = 0; i > oldCount; i++)
{
newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].transform.SetParent(newItemOldSlot.bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
}
}
else if (newItemOldSlot.storedCount == 0) //Old bag has no items
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0);
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
newItemOldSlot.Invoke("setupBagButton", 0);
}
//No else, as the current item does not have room for items in the old bag, so the switch is illegal.
}
//No else, as the current item is not a bag, and so cannot switch. Might search for an open slot and put the new item there if not holding items.
}
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.transform != transform) //Did it come from a building and not this slot?
{
//Not implemented yet.
}
else //Item was not in a specialized slot and did not come from another building.
{
if (newItemInfo.info.typeID == curItemInfo.info.typeID && newItemInfo.info.stackCount + curItemInfo.info.stackCount <= curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
Debug.LogWarning("Removing item needs work");
int index = buildingInv.buildingInv[buildingNum].IndexOf(curItemInfo.info);
curItemInfo.info.stackCount += newItemInfo.info.stackCount;
curItemInfo.transform.FindChild("Count").GetComponent<Text>().text = curItemInfo.info.stackCount.ToString();
Debug.LogWarning("Updating an item needs work");
Destroy(newItem.gameObject);
}
else if (newItemInfo.info.typeID != curItemInfo.info.typeID || newItemInfo.info.stackCount + curItemInfo.info.stackCount > curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform); //Set the parent of the current child to the old slot
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.associatedBag.storedCount--; //Note that an item has been removed from the inventory.
}
}
}
else //The current slot is empty
{
if (newItemOldSlot.slotType == 20) //Check if the old slot is a bag
{
if (newItemOldSlot.storedCount == 0) //Check if it is empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0); //Remove slots in old bag
newItem.transform.FindChild("BagButton").gameObject.SetActive(false); //Make sure that the button is diabled.
//Stuff
}
//No else, as the bag cannot be empty.
}
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.transform != transform) //Check if it came from a building and not from this slot.
{
//Not implemented yet.
}
else //Must have come from general inventory
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this slot.
associatedBag.storedCount--; //Note that an item has been removed from the inventory.
}
}
}
}
#endregion
#region GeneralSlot
else if (slotType == 0 && newItemInfo.info.typeID != 1) //Is this an inventory slot? Bag inventory, where most item types are accepted.
{
#region fromBag
if (newItemInfo.info.typeID == 20 && newItemOldSlot.slotType == 20) //Is the item a bag and did it come from a bag slot?
{
if (transform.childCount > 0) //Is there already an item in this slot?
{
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
if (curItemInfo.info.typeID == 20) //Is the item currently in the slot a bag?
{
if (newItemOldSlot.storedCount > 0 && curItemInfo.info.modification1 >= newItemOldSlot.storedCount) //Are there items in the old bag and can the new bag fit them?
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.itemsToLimbo(); //Move items in old bag to limbo
newItemOldSlot.removeInvSlots(); //Remove slots in old bag
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
int oldCount = newItemOldSlot.storedCount;
for (var i = 0; i > oldCount; i++)
{
newItemOldSlot.bagWindow.GetChild(i).GetComponent<ItemSlot>().newItem = newItemOldSlot.storedItems[i];
newItemOldSlot.storedItems[i].transform.SetParent(newItemOldSlot.bagWindow.GetChild(i));
newItemOldSlot.storedItems[i].GetComponent<CanvasGroup>().alpha = 1;
}
}
else if (newItemOldSlot.storedCount == 0) //Old bag has no items
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0);
yield return shortPause;
int curIndex = curItemInfo.info.modification1;
newItemOldSlot.makeInvSlots(slotcount(curIndex)); //Make old bag set up slots
newItemOldSlot.Invoke("setupBagButton", 0);
}
//No else, as the current item does not have room for items in the old bag, so the switch is illegal.
}
//No else, as the current item is not a bag, and so cannot switch. Might search for an open slot and put the new item there if not holding items.
}
else //Slot is empty
{
if (newItemOldSlot.storedCount == 0) //Check if the old bag is empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
newItemOldSlot.Invoke("removeInvSlots", 0); //Remove slots in old bag
newItem.transform.FindChild("BagButton").gameObject.SetActive(false); //Make sure that the button is diabled.
associatedBag.storedCount++; //Note that an item has been place into the inventory.
}
//No else, as a bag with items cannot be moved to inventory.
}
}
#endregion
else if (newItemOldSlot.slotType == 0 && newItemOldSlot.transform != transform) //Item came from an inventory slot but not this slot
{
if (transform.childCount > 0) //Is there already an item in this slot?
{
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
if (newItemInfo.info.typeID == curItemInfo.info.typeID && newItemInfo.info.stackCount + curItemInfo.info.stackCount <= curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
curItemInfo.info.stackCount += newItemInfo.info.stackCount;
curItemInfo.transform.FindChild("Count").GetComponent<Text>().text = curItemInfo.info.stackCount.ToString();
newItemOldSlot.associatedBag.storedCount--;
Destroy(newItem.gameObject);
}
else if (newItemInfo.info.typeID != curItemInfo.info.typeID || newItemInfo.info.stackCount + curItemInfo.info.stackCount > curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
newItemOldSlot.newItem = transform.GetChild(0).gameObject; //Set the old slot newItem to the current child
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
}
}
else //Slot is empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
associatedBag.storedCount++; //Note that an item has been place into this inventory.
newItemOldSlot.associatedBag.storedCount--; //Note that an item has been removed from another inventory.
}
}
else if (newItemOldSlot.slotType >= 900 && newItemOldSlot.slotType < 999) //Did it come from building inventory?
{
GameObject newItemOldSlotGO = newItemOldSlot.buildingInv.gameObject;
if (transform.childCount > 0) //Is there already an item in this slot?
{
ItemDragging curItemDrag = transform.GetChild(0).GetComponent<ItemDragging>();
itemInfo curItemInfo = transform.GetChild(0).GetComponent<itemInfo>();
if (newItemInfo.info.typeID == curItemInfo.info.typeID && newItemInfo.info.stackCount + curItemInfo.info.stackCount <= curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
Debug.LogWarning("Removing item needs work");
int index = buildingInv.buildingInv[buildingNum].IndexOf(curItemInfo.info);
curItemInfo.info.stackCount += newItemInfo.info.stackCount;
curItemInfo.transform.FindChild("Count").GetComponent<Text>().text = curItemInfo.info.stackCount.ToString();
Debug.LogWarning("Updating an item needs work");
//commands.CmdUpdateItem(buildingInv.gameObject, index, curItemInfo.info);
Destroy(newItem.gameObject);
}
else if (newItemInfo.info.typeID != curItemInfo.info.typeID || newItemInfo.info.stackCount + curItemInfo.info.stackCount > curItemInfo.info.stackLimit && newItemInfo.info.modification1 == curItemInfo.info.modification1)
{
int index = newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].IndexOf(newItemInfo.info);
newItemOldSlot.newItem = transform.GetChild(0).gameObject;
curItemDrag.startParent = transform; //Set the startParent for the current item to this object.
curItemDrag.transform.SetParent(newItemOldSlot.transform);
newItem.transform.SetParent(transform);
Debug.LogWarning("Removing item needs work");
if (index > newItemOldSlot.buildingInv.buildingInv[newItemOldSlot.buildingNum].Count - 1)
{
Debug.LogWarning("Adding item needs work");
}
else
{
Debug.LogWarning("Inserting item needs work");
}
}
}
else //Slot is empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
associatedBag.storedCount++; //Note that an item has been place into this inventory.
Debug.LogWarning("Removing item needs work");
}
}
else if (newItemOldSlot.slotType == 999) //Item came from loot window
{
if (transform.childCount > 0) //Is there already an item in this slot?
{
//Do nothing for now, because items cannot be put into the loot window.
}
else //Slot is empty
{
newItem.transform.SetParent(transform); //Set the parent of the new item to this object
if (newItemOldSlot.lootTest != null)
{
LootTest itemList = newItemOldSlot.lootTest;
itemList.items.RemoveAt(itemList.items.FindIndex(x => x.itemName == newItemInfo.info.itemName && x.modification1 == newItemInfo.info.modification1 && x.typeID == newItemInfo.info.typeID));
}
else if(newItemOldSlot.resourceGather != null)
{
resourceLoot itemList = newItemOldSlot.resourceGather;
itemList.items.RemoveAt(itemList.items.IndexOf(newItemInfo.info));
}
else if(newItemOldSlot.instanceRef != null)
{
instanceReference itemList = newItemOldSlot.instanceRef;
itemList.items.RemoveAt(itemList.items.IndexOf(newItemInfo.info));
}
associatedBag.storedCount++; //Note that an item has been place into this inventory.
}
}
}
#endregion
yield return null;
}
public void setupBagButton()
{
itemInfo newItemInfo = newItem.GetComponent<itemInfo>();
windowManager.bagSlotted[bagSlot - 1] = true;
bagWinName.text = newItemInfo.info.itemName;
GameObject child = newItem.transform.FindChild("BagButton").gameObject;
child.SetActive(true);
child.GetComponent<ButtonID>().invBagNum = bagSlot;
child.transform.SetAsLastSibling();
}
public void OnDrop(PointerEventData eventData)
{
GameObject item = eventData.pointerDrag;
if (item.GetComponent<ItemDragging>() != null)
{
newItem = item;
StartCoroutine("setupStuff");
}
}
public void itemsToLimbo() //Move items in window slots to the canvas so they don't get removed
{
storedItems = new GameObject[storedCount];
int s = 0;
for (var i = 0; i < bagWindow.childCount; i++)
{
if (bagWindow.GetChild(i).childCount > 0)
{
storedItems[s] = bagWindow.GetChild(i).GetChild(0).gameObject;
storedItems[s].GetComponent<CanvasGroup>().alpha = 0;
bagWindow.GetChild(i).GetChild(0).SetParent(GetComponentInParent<Canvas>().transform);
s += 1;
}
}
}
void makeInvSlots(int slotCount)
{
bagWindow.GetComponent<RectTransform>().sizeDelta = new Vector2(0, Mathf.Ceil(slotCount / 5f) * 54);
for (var i = 1; i <= slotCount; i++)
{
GameObject slot = Instantiate(slotItem);
slot.transform.SetParent(bagWindow);
slot.name = "" + (((bagSlot - 1) * 32) + i);
slot.GetComponent<ItemSlot>().associatedBag = gameObject.GetComponent<ItemSlot>();
slot.GetComponent<ItemSlot>().slotType = 0;
//slot.GetComponent<ItemSlot>().slotType = 0;
}
}
public void removeInvSlots()
{
int slots = bagWindow.transform.childCount;
for (var i = 0; i < slots; i++)
{
if (bagWindow.GetChild(i).childCount > 0)
{
bagWindow.GetChild(i).GetChild(0).SetParent(GetComponentInParent<Canvas>().transform);
}
Destroy(bagWindow.GetChild(i).gameObject);
}
}
public int slotcount(int mod1Index)
{
if (mod1Index == 5)
{
return 4;
}
else if (mod1Index == 6)
{
return 8;
}
else if (mod1Index == 7)
{
return 16;
}
else if (mod1Index == 8)
{
return 32;
}
else
{
return 0;
}
}
}
The script runs on each item slot, and handles items being added, removed, or swapped, with different slot types that accept certain item types.
The problems I have with it are, like I mentioned before, it’s not very easy to read and it also doesn’t scale very easily. I’m hoping to get some advice on how I can make this cleaner and scaleable.
EDIT:
For those who don’t want to go through the 952 lines, here’s a summary as best as I can manage
The script runs on the item slots, whether it’s an inventory slot or equipment slot. It listens for items to be dropped onto or removed from it, and checks for a few things.
If an item is dropped:
a) if the slot is already occupied
b) if the new item is of an accepted type
c) if the new item and current item can be combined
d) if the current item can be placed int the slot the new item came from
e) for bags, checks if the bags have enough inventory space for any stored items.
f) configures any necessary windows, particularly inventory windows.
If an item is removed, it just configures any necessary windows.
The script references another script for ints that hold max size and current size