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 HuskyPanda213 · Jan 15, 2014 at 04:25 PM · listscheckcrafting

Lists, and Crafting?

How do I like count how many different types of items, how many of them and return that value, use it quickly in my crafting script. So the recipe calls for 2 wood, 4 stone, it calls the CheckForItemsAndReturn() with (wood, 2), and (stone, 4), then adds the two lists, and then uses generic list.sort() and compares the two, if they are the same, then they would continue the craft method.; Thanks, Huskypanda

Code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class PlayerInventory : MonoBehaviour {
 
     public int MaxInventory = 26;
 
     public List<Item> InventoryItems = new List<Item>();
 
     public void AddItem(Item _item){
 
     }
 
     public void Craft(CraftingItem Recipe){
         Recipe.InputItem.Sort ();
         List<Item> _items = new List<Item> ();
 
         //Find out the number and use CheckForItemsAndReturn();!
 
         if (Recipe.InputItem == _items) {
             foreach(Item _it in _items){
                 InventoryItems.Remove(_it);
             }
             InventoryItems.Add(Recipe.OutputItem);
         }
         else{
             Debug.Log("Invalid Items.");
         }
     }
 
     //For Multiple
     public List<Item> CheckForItemsAndReturn(Item checkfor, int count){
         List<Item> il = new List<Item>();
 
         foreach(Item it in InventoryItems){
             if(it == checkfor){
                 if(il.Count <= count){
                     il.Add(it);
                 }
             }
         }
 
         return il;
     }
 
 }
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 HuskyPanda213 · Jan 15, 2014 at 06:01 PM 0
Share

I reworded the question to make more sense.

1 Reply

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

Answer by kei_kvaartz · Jan 15, 2014 at 06:33 PM

I would make CheckForItems a bool function that returns true if the items are found, and have the items (if found) spit out as an output parameter.

bool CheckForItems (List wantedItems, out List foundItems)

Comment
Add comment · Show 3 · 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 HuskyPanda213 · Jan 15, 2014 at 08:40 PM 0
Share

An example would help, also I can change to bool but then finding the items becomes more complecatd.

avatar image HuskyPanda213 · Jan 16, 2014 at 12:14 AM 0
Share

How would I compare all the items without getting the same entity of item twice. I know how to make it a bool but then it will compare all the items, and maybe give the same one twice. Also is there a way to make like for each item of wood in items list.

avatar image kei_kvaartz · Feb 09, 2014 at 05:00 PM 0
Share

I would make a temporary list that is a copy of the inventory list.

 List<item> temp = new List<item>();
 foreach (Item i in inventory) {temp.Add(i);}

Then as you put items in the output list, remove them from the temp list. (Do not remove them from inventory.) This will prevent duplication.

Be careful with "foreach" in this kind of situation. You cannot modify a collection while "foreach"-ing through it. In this type of situation, I would reverse iterate through it, like this:

 for (int j=temp.Count-1; j>=0; j--) {
   if (Condition$$anonymous$$et(temp[j])) {
     outList.Add(temp[j]);
     temp.Remove(temp[j]);
   }
 }

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

19 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

Related Questions

[Answered](C#)Comparing two Lists for a Crafting System 2 Answers

Lists within lists to make a crafting system 1 Answer

I'm trying to make a button that instantiates an item when you craft it 1 Answer

Crafting items. Best way to do it? 0 Answers

Help integrating a category system 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