Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Kamil1064 · Mar 09, 2016 at 01:45 PM · classinventorycheckitem

List check argument of item

Hi, i'm need simple inventory to store few stuff, it's working, however i would like to use simpler method to check quantity of exaclty that item. Here is code:

 public class Items
     {
         public string name;
         public int itemID;
         public int quantity;
     }
 public List<Items> inventory = new List<Items>();
 //set up few items
 //add quantity to existing item
 public void AddItem(int idForItem)
     {
         if(inventory.Exists(x => x.itemID == idForItem))
         {
             foreach(Items i in inventory)
             {
                 if(i.itemID == idForItem)
                     i.quantity ++;
             }
         }
     }

But checking whole items is no needed, I know which item i wanna check and I know it's exist already in inventory so would like to use something simple like this:

 inventory.itemID(0).quantity ++;

but it's not working like that.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Masterio · Mar 09, 2016 at 02:14 PM

You should use the Dictionary.

Keep item id as key and item object as value:

 Dictionary<int, Item> inventory = new Dictionary<int, Item>();

then you can simply get the item by the id rally fast:

 if(inventory.ContainsKey(item_id))
    inventory[item_id].quantity ++;

Comment
Add comment · Show 4 · Share
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 Kamil1064 · Mar 09, 2016 at 05:41 PM 0
Share

Thanks, it works :) However I get hard error while using your "if" method and I could'n solve it with google. However it's working if I use this.

 if(inventory.Contains$$anonymous$$ey(int item_id))

But I may use it inside inventory script. I would like ask you to help me if i want to check do I have item from other script, for example:

 public Inventory my_inventory; // dropped from inspector
 void $$anonymous$$yStuff()
 {
 if(my_inventory)...
 {
 // ok
 }






avatar image Masterio Kamil1064 · Mar 09, 2016 at 07:27 PM 1
Share

You can add to the Inventory class a method like: [it search the items dictionary]

 public bool IsItemExists(int itemID)
 {
    return items.Contains$$anonymous$$ey(itemID);
    // you should check the quantity if you are not removing from the list items with quantity = 0.
 }

You can add method like this if you need an item handler.

 public Item GetItemById(int itemID)
     {
        if(items.Contains$$anonymous$$ey(itemID))
           return items.[itemID];

        return null;
     }

And now you can check if item exists in inventory instance with:

 if ( my_inventory.IsItemExists(item_id))
 {
    // item exists
 }
 else
 {
    // item not found
 }
   

or:

 Item item = my_inventory.GetItemById(item_id);
 
 if (item != null )
     {
        // item exists, do something with item f.e.
      item.quantity++;
     }
     else
     {
        // item not found
     }

PS. Code is not compiled.

avatar image Kamil1064 Masterio · Mar 09, 2016 at 07:46 PM 0
Share

Thanks, I've checked first method and it's working fine. Interesting solution :)

Show more comments
avatar image
0

Answer by RoofTurkey · Mar 09, 2016 at 02:15 PM

That is because you're accessing an integer (itemID) as if it is a list. However your list is called 'inventory'.

Therefore the correct syntax would be

 inventory(0).quantity++;

However, then it will not be checking what the itemID is and therefore won't solve your problem.

Before going any further from here I would like to ask why the position in the list can't be the items ID? So for example, the first item would have the ID of '0'.

Comment
Add comment · Share
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
0

Answer by Kamil1064 · Mar 17, 2016 at 03:54 PM

Sorry but i want ask you for one more thing :) how to set my int in other script based on an item's quantity? Something like;

 private int number;
  //somewhere in code
  number = my_Inventory(item_id).quantity;
 number = my_inventory(0).quantity;









Comment
Add comment · Share
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

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

37 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 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

int variable from dictionary? 0 Answers

inventory and item system where every item need its own use function 0 Answers

How to add "Properties" to items? 1 Answer

Add Item to inventory - Shop UI intro and setup 0 Answers

Picking up various types of items? 0 Answers


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