- Home /
Issues with Inventory script
I'm working on an inventory system and I was following this tutorial series and I've got most of what's in that series (parts 1-8 at least) working right but I'm having issues with two things.. one is the dragging and dropping to move items.
for some reason it only works if you click outside of the Rect containing the item you want to drag and then drag onto the Rect with the mouse button already held down (doesn't work if you have the mouse over the Rect and then click and drag away which is what I'm going for and what's shown in the tutorial series - in part 8).. the code that has that part in it is this
void DrawInventory()
{
Event e = Event.current;
int i = 0;
for (int y = 0; y < slotsY; y++)
{
for (int x = 0; x < slotsX; x++)
{
Rect slotRect = new Rect(invLocX + (x*55), invLocY + (y*55), 50, 50);
GUI.Button(slotRect, "", skin.GetStyle("Inventory Slot"));
slots[i] = playerInventory[i];
if(slots[i].itemName != null)
{
GUI.DrawTexture(slotRect, slots[i].itemIcon);
GUI.Label(slotRect, playerInventory[i].quantity.ToString(), skin.GetStyle("Quantity Label"));
if(slotRect.Contains(e.mousePosition))
{
if (slots[i].questItem) questItem = "This is a quest item.";
else questItem = "";
CreateTooltip(slots[i]);
if(!draggingItem) showTooltip = true;
if (e.button == 0 && e.type == EventType.MouseDrag && !draggingItem)
{
draggingItem = true;
prevIndex = i;
draggedItem = slots[i];
playerInventory[i] = new Item();
}
if (e.type == EventType.MouseUp && draggingItem)
{
playerInventory[prevIndex] = playerInventory[i];
playerInventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
}
else
{
if(slotRect.Contains(e.mousePosition))
{
if (e.type == EventType.MouseUp && draggingItem)
{
playerInventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
}
if (tooltip == "")
showTooltip = false;
i++;
}
}
}
Then the other thing I'm having trouble with is some stuff I added to the AddItem function to try and get it so that items can stack. it works to add single items and stacks of items to the inventory. but if I try to stack onto a slot that has an item in it but isn't at maxStack or if I try to add items in a quantity that's larger than those items can stack to be it gets all messed up... this is what I have for the AddItem function. I think I must be doing something wrong with assigning the quantities... or maybe I need another variable to hold items while they're getting stacked properly or something so that it doesn't overwrite the assignments in other parts of the function? I mean I thought that if I assign thingA the value of thingB in one part of the script, it would only change with thingB if the part of the script that has that assignment in it gets called again. so I probably am also not understanding how unity moves through all these for loops that I have...
I would appreciate any help that anyone may have to offer about either of these issues..
void AddItem(int id, int quant)
{
for (int j = 0; j < database.items.Count; j++)
{
if (database.items[j].itemID == id)
{
database.items[j].quantity = quant; //ALWAYS set the relevant quant when referencing database items
print("1:" + quant+ "a:" + database.items[j].maxStack);
if (database.items[j].maxStack == 1)
{
for (int i = 0; i < playerInventory.Count; i++)
{
if (playerInventory[i].itemName == null)
{
playerInventory[i] = database.items[j];
quant--;
if (quant == 0)
{
quantLeftover = 0;
return;
}
}
else if (i == (playerInventory.Count - 1) && playerInventory[i].itemName != null)
{
inventoryIsFull = true;
quantLeftover = quant;
return;
}
}
}
else if (database.items[j].maxStack > 1)
{
for (int i = 0; i < playerInventory.Count; i++)
{
if (playerInventory[i].itemID == id && playerInventory[i].quantity < database.items[j].maxStack)
{
print("2:" + quant+ "b:" + playerInventory[i].quantity);
for (int k = playerInventory[i].quantity; k < playerInventory[i].maxStack; k++)
{
playerInventory[i].quantity++;
quant--;
print("3:" + quant+ "c:" + playerInventory[i].quantity);
if (quant == 0) return;
}
}
else if (playerInventory[i].itemName == null)
{
print("4:" + quant+ "d:" + playerInventory[i].quantity);
if (quant <= database.items[j].maxStack)
{
playerInventory[i] = database.items[j];
playerInventory[i].quantity = quant;
quant = 0;
print("5:" + quant + "e:" + playerInventory[i].quantity);
quantLeftover = quant;
return;
}
else if (quant > database.items[j].maxStack)
{
print("6:" + quant + "f:" + playerInventory[i].quantity);
database.items[j].quantity = database.items[j].maxStack;
playerInventory[i] = database.items[j];
quant -= database.items[j].maxStack;
print("6:" + quant + "f:" + playerInventory[i].quantity);
}
}
else if (playerInventory[i].itemID == id && playerInventory[i].quantity == playerInventory[i].maxStack && quant > 0)
{
inventoryIsFull = true;
quantLeftover = quant;
return;
}
print("7:"+quant+ "g:" + playerInventory[i].quantity);
if (quant == 0)
{
quantLeftover = 0;
return;
}
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Inventory AddItem help 1 Answer
How do I make my item only open if there is room in my Inventory? 3 Answers
chest items get added to inventory 2 Answers
Clone a component, add it to a list and destroy the GameObject 1 Answer
Searching your inventory for an item,How do i search for an item in my inventory? 1 Answer