Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Zukas · Dec 09, 2014 at 02:39 PM · lists

How to get quantity of same item in list?

How could I get quantity of same item in list? Lets say I have a treasureBag list, which contains Treasure class variables, and inside there's 3 sapphires, 1 ruby, and 1 emerald. How could I possibly return specific integer for their amount?

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

2 Replies

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

Answer by Unitraxx · Dec 09, 2014 at 07:42 PM

Ok I read your comments in the other answer and it looks like you have a unique id for each item. If this unique id starts at 0 you can hold an array of counts with as index the id of the item and as value the count of that item.

To initialize this you just start with an integer array with size equal to the number of items there are and all values set to 0. Then you iterate over all your items, use the unique id as index to access the count in the array, and increase that number with one.

Pretty simple and if you don't have a unique id starting from zero, you can apply the same principle with a dictionary that maps integers to integers.

The advantages of this method is that you only have to iterate once over the entire list to get ALL the counts. Also you never need to adjust your function when you add a new item type. (With a switch statement you would.)

Comment
Add comment · Show 2 · 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 Zukas · Dec 09, 2014 at 07:48 PM 0
Share

This sounds like a good idea, I'm going to try this tomorrow or later today if I'll be given enough time and wont stay lazy, thanks :D

avatar image Zukas · Dec 11, 2014 at 01:17 PM 0
Share

Yes! It works this way, it also solved up some other little things for me, how couldn't I have thought of this simple, yet efficient way before, thanks!

avatar image
0

Answer by EvilTak · Dec 09, 2014 at 04:09 PM

If the Treasure class has a variable type with type of enum TreasureType { Sapphire, Ruby, Emerald } then:

 int sapphireCount, rubyCount, emeraldCount;
 
 foreach(Treasure item in treasureBag){
   switch(item.type){
     case TreasureType.Sapphire:
       sapphireCount++;
       break;
     case TreasureType.Ruby:
       rubyCount++;
       break;
     case TreasureType.Emerald:
       emeraldCount++;
       break;
   }
 }
Comment
Add comment · Show 6 · 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 Zukas · Dec 09, 2014 at 04:30 PM 0
Share

No, sadly it doesn't have enum type, it would be way too much enums to make for all the treasure types I plan to have. Thanks for suggestion though.

avatar image EvilTak · Dec 09, 2014 at 04:58 PM 0
Share

Then how do you plan on managing the types of treasure?

avatar image Zukas · Dec 09, 2014 at 05:04 PM 0
Share

I assigned each of them unique ID (Int), I guess I could use 'Switch' thing with them too, but then I'd still have to assign many custom integers for their quantities. I guess I'll try to do something with those IDs when I'll have some free time. I'll reply on how it went, if you have any more suggestions, let me know.

avatar image EvilTak · Dec 09, 2014 at 05:07 PM 0
Share

How many types of treasure do you have?

avatar image EvilTak · Dec 09, 2014 at 05:18 PM 0
Share

I have an idea, but I need the number of treasure types for that. It is a very fast and efficient idea.

Show more comments

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

27 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

Related Questions

lists in lists the best option here? 1 Answer

Modifying Lists via the inspector 1 Answer

Undo/back system using a List/Array 2 Answers

Lists of lists in Boo gives error: "Type 'object' does not support slicing." 0 Answers

Saving a List of variables between scenes 2 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