Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by getyour411 · Jan 31, 2014 at 11:39 PM · c#listinventory systemlinqcontain

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artifactx · Jan 17, 2015 at 05:49 PM 0
Share

Here is a tut to an inventory, that uses stacks ins$$anonymous$$d of lists: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inventory System wont work 2 Answers

Instantiate a prefab from jsonarray? 1 Answer

Get highest Vector3 from list (using Linq?) 1 Answer

Calling for two variables in a list 1 Answer

Sort List by string field 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges