- Home /
C# Lists & Inventory Logic
I'm working on my Drag and Drop inventory and nearly have a working prototype but my logic is off somewhere in managing the Inventory list and correspondong bagSlot and after numerous attempts figured I would ask for some help.
Update: After posting original code, I fixed my logic errors and now just chasing down last one. If I take an item from slot X and move it into a slot X + (some higher slot), I haveto click into the new slot twice. If I take an item from slot X and move it into a slot lower#, it works as intended registering the mouseUP and drops the icon.
I'm new to Lists, LINQ and OOP in general so I could be misusing stuff or just flubbed some logic.
TL;DR: what's wrong with my ItemDrop approach?
private void InventoryWindow(int WinID) {
// Increments from 0...invWinRows * invWinCols
int cnt = 0;
bool slotUsed = false;
// Display rows and cols in Window
for (int y=0; y < invWinRows; y++) {
for(int x=0; x < invWinCols; x++) {
// Build Rect/Bag Slot
float xPos = 5+(x*iconDim);
float yPos = 20+(y*iconDim);
invSlotRect = new Rect(xPos,yPos,iconDim,iconDim);
// LINQ: look through Inventory and see if cnt/current invBagSlot is a match
// returns -1 if it is not; UA @KellyThomas - danka!
int ItemINDEX = pinv.pInventory.IndexOf(pinv.pInventory.Where (i => i.invBagSlot == cnt).FirstOrDefault ());
if(ItemINDEX == -1)
slotUsed = false;
else
slotUsed = true;
if(invSlotRect.Contains(Event.current.mousePosition)) {
// Debug.Log("Mouse in box " + cnt);
// ********* Right click to Use item ********* //
if(slotUsed && Event.current.type == EventType.MouseDown && Event.current.button == 1 && bagSlotState == BagSlotState.None) {
Debug.Log (pinv.pInventory[cnt].invName + "was right-clicked");
Debug.Log (pinv.pInventory[cnt].invID + " is the ID");
// cursorActive = CursorState.Farming;
bagSlotState = BagSlotState.UsingItem;
activeItemListIDX = cnt;
activeItem = true;
}
// *********** Left click to pick up item *********//
if(Event.current.type == EventType.MouseUp && Event.current.button == 0 && slotUsed && bagSlotState == BagSlotState.None) {
ItemINDEX = pinv.pInventory.IndexOf(pinv.pInventory.Where (i => i.invBagSlot == cnt).FirstOrDefault ());
// dragclass.pic = pinv.pInventory[ItemINDEX].invIconTexture;
dragclass.SetIconProps(pinv.pInventory[ItemINDEX].invIconTexture, pinv.pInventory[ItemINDEX].invStackCount);
itemInMotion = ItemINDEX;
Debug.Log ("PICKUP ==> Inv Slot: " + cnt + " Item: " + pinv.pInventory[itemInMotion].invName + " INDEX: " + ItemINDEX);
//bagSlotState = BagSlotState.PickingUpItem;
bagSlotState = BagSlotState.DraggingItem;
}
// *********** Left click held down, dragging item ********//
// if((Event.current.type == EventType.mouseDrag || Event.current.type == EventType.MouseDown) && bagSlotState == BagSlotState.PickingUpItem) {
if(Event.current.type == EventType.mouseDrag && bagSlotState == BagSlotState.PickingUpItem) {
bagSlotState = BagSlotState.DraggingItem;
}
// ************* Left click, dropping item into unused inventory slot *************//
if(Event.current.type == EventType.MouseUp && Event.current.button == 0 && !slotUsed && bagSlotState == BagSlotState.DraggingItem) {
Debug.Log("DROP ==> Inv slot: " +cnt+" Item in Motion: " + pinv.pInventory[itemInMotion].invName + " INDEX: " + itemInMotion);
bagSlotState = BagSlotState.DroppingItem;
pinv.pInventory[itemInMotion].invBagSlot = cnt;
//dragclass.pic = null;
dragclass.SetIconProps(null,-1);
itemInMotion = -1;
}
// If Event.current is a MouseUp in an occupied slot and we are dragging, that's a Swap
// cases to consider later: 1/stackables and on-split stuff, 2/ drop into same box
if(Event.current.type == EventType.MouseDown && Event.current.button == 0 && bagSlotState == BagSlotState.DraggingItem && slotUsed == true) {
Debug.Log ("SWAP ==> Inv SOURCE slot: "+pinv.pInventory[itemInMotion].invBagSlot+ " Item in Motion: " + pinv.pInventory[itemInMotion].invName + " INDEX: "+itemInMotion);
// Get ListID of new item in cnt/current slot
int ItemINDEX2 = pinv.pInventory.IndexOf(pinv.pInventory.Where (i => i.invBagSlot == cnt).FirstOrDefault ());
// Place the item we were dragging into this slot - but now we have 2 items sharing this slot!
pinv.pInventory[ItemINDEX2].invBagSlot = -1; // move it out
pinv.pInventory[itemInMotion].invBagSlot = cnt; // place the item we are dragging into it
// Update icon of item in bag
// pinv.pInventory[itemInMotion].invIconTexture = pinv.pInventory[ItemINDEX2].invIconTexture;
// Assign new dragging item
itemInMotion = ItemINDEX2;
// Change icon of dragging object
//dragclass.pic = pinv.pInventory[itemInMotion].invIconTexture;
dragclass.SetIconProps(pinv.pInventory[ItemINDEX].invIconTexture, pinv.pInventory[ItemINDEX].invStackCount);
ItemINDEX = ItemINDEX2;
Debug.Log ("SWAP ==> Inv TARGET slot: "+cnt+" Item to Swap "+pinv.pInventory[itemInMotion].invName);
}
}
// ItemINDEX = pinv.pInventory.IndexOf(pinv.pInventory.Where (i => i.invBagSlot == cnt).FirstOrDefault ());
if(ItemINDEX != -1) {
GUI.Button (new Rect(invSlotRect),pinv.pInventory[ItemINDEX].invIconTexture);
// GUI.Button (new Rect(invSlotRect),qmload.allIcons[IconINDEX].iconTexture);
// If item is stackable, display current stack count
if(pinv.pInventory[ItemINDEX].invStackable)
GUI.Label(new Rect(invSlotRect),pinv.pInventory[ItemINDEX].invStackCount.ToString());
}
else {
// This is an empty slot
GUI.Button(new Rect(invSlotRect),"");
}
cnt++;
if(bagSlotState == BagSlotState.DroppingItem)
bagSlotState = BagSlotState.None;
} // x
} // y, forloop
// "X" to close too
if (GUI.Button (new Rect ((invWinCols*iconDim)-16, 2, 20, 18), "x"))
showInventory = false;
//Empty DragWindow allows dragging if nothing else has precedence/focus
GUI.DragWindow ();
}
Dunno if it matters but this is all going on inside a GUI.Window
Here is a tut to an inventory, that uses stacks ins$$anonymous$$d of lists: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Your answer
Follow this Question
Related Questions
Custom pathfinding and node-values 1 Answer
C#, LINQ and Lists - help with 1 line of code? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Count object list duplicates 1 Answer