- Home /
Swapping items between separate lists. Inventory troubles in C#
I have a pair of scripts that are more or less identical to one another, however each object to which they are attached is separate and controls its own GUI panel. One for Inventory, one for Equipment.
I've been trying for days to get these lists to interact the way I wish and thought I had finally solved it, but that's not how it turns out.
I can get the list items in either panel to be freely draggable inside their own panel, they'll swap position with whatever they're dropped on. I can even get this to happen from one panel to another, but never both simultaneously.
If I can drag from Inventory to equipment with no flaws, then I can't drag an item in the inventory to the same slot as any other item in the inventory without getting some wonky problems.
At one point, I had it working to the point that both windows could be manipulated freely and things could be moved from the Inventory to Equipment flawlessly, but not visa versa.
Any variable abbreviated with "eqp." is referencing the Equipment panel, you can assume identical use of "inv." in the opposing objects script.
I believe lines 46-61 are where my problem is stemming from, most of the rest SHOULD be irellevant as far as I know. the "InEquipment" bool looks at the list index and becomes true if looking as an index from within the Equipment script, an "InInventory" bool exists opposite to it. They check themselves and react exactly as I would expect them to when in the appropriate window, but no matter what I do, I can NEVER, EVER get Unity to check the bool, it always simply defaults to the code passed where the check is. I've tried multiple different placements, I even rewrote a large portion of code several times with no luck before having to revert to this old github save.
I know it's probably pretty ugly to behold and my approach to using the separate scripts to handle the windows might even be blaphemous, but I've been trying to get this working for over two weeks now and I'm mentally exhausted, any sort of assistance would be appreciated.
void DrawInventory()
{
Event e = Event.current;
int i = 0;
swapItem = new Item();
swapIndex = -1;
if (eqp.swapItem.itemID >= 0)
InEquipment = true;
for (int y = 0; y < slotsY; y++)
{
for(int x = 0; x < slotsX; x++)
{
Rect slotRect = new Rect (x * 34, y * 34, 34, 34);
GUI.Box (slotRect, "", InvBackground.GetStyle ("SlotBackground"));
slots[i] = inventory[i];
if (slots[i].itemName != null)
{
GUI.DrawTexture (slotRect, inventory[i].itemIcon);
if (slotRect.Contains (e.mousePosition))
{ swapItem = inventory[i];
swapIndex = i;
tooltip = CreateToolTip (slots[i]);
showTooltip = true;
if(e.button == 0 && e.type == EventType.mouseDrag && !draggingItem)
{
draggingItem = true;
invPrevIndex = i;
draggedItem = slots[i];
inventory[i] = new Item();
}
if(e.isMouse && e.type == EventType.mouseDown && e.button == 1)
{
if (slots[i].itemType == Item.ItemType.eWarfare)
{
print ("Used: " + slots[i].itemName);
inventory[i].stackAmount -=1;
//UseConsumeable(slots[i].itemID);
if (slots[i].stackAmount <= 0)
RemoveItem (slots[i].itemID);
}
}
if (e.type == EventType.mouseUp && draggingItem)
{
if (InEquipment)
{
print ("Made it to code in INV");
inventory[swapIndex] = draggedItem;
eqp.equipment[eqp.eqpPrevIndex] = swapItem;
draggingItem=false;
draggedItem = null;
return;
}
print ("Missed target code INV");
inventory[invPrevIndex] = inventory[i];
inventory[i] = draggedItem;
draggingItem = false;
draggedItem = null;
}
}
}else {
if(slotRect.Contains(e.mousePosition))
{
if(e.type == EventType.mouseUp && draggingItem)
{
inventory[i] = draggedItem;
draggingItem=false;
draggedItem = null;
}
}
}
if (tooltip == "")
{
showTooltip = false;
}
i++;
}
if (eqp.swapItem.itemID == -1)
InEquipment = false;
}
}
Interesting script. Now the question is: Does the order of the list matter? If it does, it might be a good idea to adjust your design a bit. In any case, it's generally easier to keep track of your scripts if you separate GUI and the scripts that do the magic. $$anonymous$$g. if you had a script that handles the equipment and inventory, including methods to equip and unequip items and then use a separate script handling GUI and user input to access those methods, wouldn't it be easier to keep track of everything?
And likely far more efficient in every way, but i'm still pretty new to this whole C# thing, This is the last thing I really need before I can just get away from the inventory for a while, so I'm hoping to get this out of the way, regardless of how efficient it might be. I've already got stackable items and other small tweaks and changes to the overall function. Some of which were quite the headache for me to figure out. Truthfully, I know it's probably a terrible way to do it, but I don't have the heart to just start over from scratch right now.
As to the order of the list, no, it doesn't matter, it's populated via an Item and ItemDatabase script, The lists I'm trying to swap between are just there to hold the information to display and access the items.
Right, okay. The thing is, this code is not exactly easy to understand, especially since we don't know what your other methods you reference do. Which "wonky problems" exactly are you getting when you swap items? Console messages or a closer description of that are really essential so we have a better shot at figuring out what's going wrong.
As for the lists, doesn't the index of the list equal the slot index?
Sorry about that, I don't get any actual errors, everything works. The "wonky problems" are that I can swap things freely inside their respective panels and switch items between the panels, but only one way. I.$$anonymous$$ if I can move an item from Inventory to Equipment, I can't do the opposite, it will place the "swapItem" in the panels prevIndex slot, ins$$anonymous$$d of moving it over to its sibling panel.
EDIT: While pouring back over my code, i've come to recall that 'swapIndex' exists in each script so that I can pull the value out of a for loop and use it elsewhere, it seems like the most straight forward way to do this.
$$anonymous$$y problem is that I can't seem to get it to acknowledge the IF on line 47. If it did, it would all work as far as I can tell, but no matter how I've structured the code in my multiple attempts to make things function, it -will not- check that IF, even if its conditions are met in the inspector when I move items.