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 17, 2014 at 01:40 AM · listlinqcrafting

Help with crafting system.

Hello unity3d community, I am in need of assistance, whenever I use my craft() function in my game, it says "invalid items", in the console(which it is supposed to say when not enough items are present in InventoryItems to craft an item), and then it does not craft, even if I do have enough items. This is really frustrating. Please help!!!

Code:

     using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 
 public class PlayerInventory : MonoBehaviour {
     
     public int MaxInventory = 26;
     
     public List<Item> InventoryItems = new List<Item>();
     
     void OnGUI(){
         if (GUILayout.Button ("+5 Wood")) {
             AddItem(ItemConstructors.Wood());
             AddItem(ItemConstructors.Wood());    
             AddItem(ItemConstructors.Wood());    
             AddItem(ItemConstructors.Wood());    
             AddItem(ItemConstructors.Wood());
         }
         if (GUILayout.Button ("Craft Wall")) {
             Craft(CraftingRecipes.WoodenWall());
         }
         
     }
     
     
     public void AddItem(Item _item){
         if (InventoryItems.Count < MaxInventory) {
             InventoryItems.Add(_item);
             SaveItems();
         }
     }
     
     public void RemoveItem(Item _item){
         InventoryItems.Remove (_item);
     }
     
     public void UseItem(Item _item){
         if (_item.ItemType == "Food") {
             ConsumeItem(_item);
         }
         SaveItems();
     }
     
     public void ConsumeItem(Item _item){
         if (_item.ItemDur > 1) {
             _item.ItemDur--; 
         }
         else if(_item.ItemDur <= 1){
             RemoveItem(_item);
         }
     }
     
     public void SaveItems(){
         for (int i = 0; i<MaxInventory; i++) {
             PlayerPrefs.SetString("Itemn"+i,InventoryItems[i].ItemName); 
             PlayerPrefs.SetString("Itemt"+i,InventoryItems[i].ItemType);
             PlayerPrefs.SetInt("Itemv"+i,InventoryItems[i].ItemValue);
             PlayerPrefs.SetInt("Itemd"+i,InventoryItems[i].ItemDur);
         }
         LoadItems ();
     }
     public void LoadItems(){
         InventoryItems.Clear();
         for (int i = 0; i<MaxInventory; i++) {
             Item it = new Item();
             it.ItemName = PlayerPrefs.GetString("Itemn"+i);
             it.ItemType = PlayerPrefs.GetString("Itemt"+i);
             it.ItemValue = PlayerPrefs.GetInt("Itemv"+i);
             it.ItemDur = PlayerPrefs.GetInt("Itemd"+i);
             if(it.ItemName.Length <= 2 && it.ItemType.Length <= 2){
             }
             else{
                 InventoryItems.Add(it);
             }
         }
     }
     
     public void Craft(CraftingItem Recipe){
         bool canCraft = ContainsAllItems (InventoryItems,Recipe.InputItem);
         if(canCraft){
             if(InventoryItems.Count < MaxInventory){
                 List<Item> list = new List<Item>();
                 list = InventoryItems.Except(Recipe.InputItem).ToList();
                 InventoryItems = list;
                 AddItem(Recipe.OutputItem);
             }
             else{
                 Debug.Log("Not enough space!");
             }
         }
         else{
             Debug.Log("Invalid Items.");
         }
     }
 
     public static bool ContainsAllItems(List<Item> a, List<Item> b)
     {
         return !b.Except(a).Any();
     }
     
 }

Other scripts;

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class CraftingItem
 {
     public Item OutputItem = new Item();
     public List<Item> InputItem = new List<Item>();
 }
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public static class CraftingRecipes {
 
     public static CraftingItem WoodenWall(){
         CraftingItem ct = new CraftingItem ();
         ct.InputItem.Add (ItemConstructors.Wood());
         ct.InputItem.Add (ItemConstructors.Wood());
         ct.OutputItem = ItemConstructors.WoodenWall ();
         return ct;
     }
 
     public static CraftingItem StoneWall(){
         CraftingItem ct = new CraftingItem ();
         ct.InputItem.Add (ItemConstructors.Stone());
         ct.InputItem.Add (ItemConstructors.Stone());
         ct.OutputItem = ItemConstructors.StoneWall ();
         return ct;
     }
 
     public static CraftingItem FirePit(){
         CraftingItem ct = new CraftingItem ();
         ct.InputItem.Add (ItemConstructors.Wood());
         ct.InputItem.Add (ItemConstructors.Stone());
         ct.OutputItem = ItemConstructors.FirePit ();
         return ct;
     }
 
 }
 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Item
 {
     public string ItemName = "";
     public string ItemType = "";
     public int ItemValue = 0;
     public int ItemDur = 0;
 }

(Just the wood and woodenwall of item constructor)

 public static Item Wood(){
         Item it = new Item ();
         it.ItemName = "Wood";
         it.ItemType = "Crafting";
         it.ItemDur = 1;
         it.ItemValue = 1;
         return it;
 }
 
 public static Item WoodenWall(){
         Item it = new Item ();
         it.ItemName = "Wooden Wall";
         it.ItemType = "Build";
         it.ItemDur = 100;
         it.ItemValue = 1;
         return it;
     }

Comment
Add comment · Show 2
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 18, 2014 at 05:47 AM 0
Share

Please help me, I really need it. Also if not stated clearly above containsallitems is returning false all the time even if should return true.

avatar image Mr.Brownstone · Feb 07, 2015 at 02:44 PM 0
Share

Same problem for me. If you use your Items class or Gameobject, etc.. it doesn't work. I tried it by changing the list type to string or int, It worked.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by fuzzysmurf · Jan 17, 2014 at 10:40 PM

can you show your Itemconstructor.Wood() method? and anything else that may be needed w/ that method?

if you're only accepting "Item" in your list, chances are, from what I can tell, is that you are accessing the Wood Method, but aren't returning an item.

If so, then you have to make ItemConstructors.Wood() like this:

 public Item Wood()
 {
 item wood;
 //whatever code you had
 return wood;
 }

Thats what I believe, but I dont know for sure till I can see your scripts.

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 fuzzysmurf · Jan 17, 2014 at 10:48 PM 0
Share

the second thing is to check your InventoryItems, make sure it contains a list in their, of your items, and check your Recipe.InputItems

avatar image HuskyPanda213 · Jan 18, 2014 at 12:54 AM 0
Share

I updated the question, with new code.

avatar image fuzzysmurf · Jan 18, 2014 at 01:48 AM 0
Share

line 97. whats the purpose of it? add a Debug.Log that prints out a true or false statement. if it looks correct, like it should work, but it prints out false in the console, it means its probably that method. only thing i can think of.

avatar image HuskyPanda213 · Jan 18, 2014 at 02:25 AM 0
Share

I know that it is that method, I just do not know how to make it work. Also, the line in craft to destroy the items used(inventoryitems = inventoryitems.except(Recipe.Inputitems)), does not work. Those were the only things that I needed help with. So I guess the main question is, do you know how to fix the method?

avatar image fuzzysmurf · Jan 18, 2014 at 02:41 AM 0
Share

Im assu$$anonymous$$g you are trying to compare the items in list A to some sort of combination in list B, right? im not sure what is in your list B, but just a thought.

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

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

A node in a childnode? 1 Answer

Linq OrderBy GameObject's name Numerical 1 Answer

Custom pathfinding and node-values 1 Answer

Combining Items in a single list by comparison 1 Answer

Search multiples INT in a custom class List 3 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