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
1
Question by badatnames16 · Feb 17, 2015 at 05:21 AM · c#crafting

Help with crafting system. Removing used items

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Recipe : MonoBehaviour {
 public string itemName;
 public Dictionary<string, int> items;

 public Recipe(string name, Dictionary<string, int> materials)
 {
     itemName = name;
     items = materials;
     Inventory.recipes.Add(this);

 }
 }


 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class RecipeCreator : MonoBehaviour {
 public RecipeCreator() {

     Recipe plankHorizontal = new Recipe("Plank (Horizontal)", new Dictionary<string, int>() {
         {"Wood", 10}
     });

     Recipe plankVertical = new Recipe("Plank (Vertical)", new Dictionary<string, int>() {
         {"Wood", 10}
     });

     Recipe woodBlock = new Recipe("Wood Block", new Dictionary<string, int>() {
         {"Wood", 1}
     });

     Recipe WoodPlanks = new Recipe ("WoodPlanks", new Dictionary<string, int> (){
         {"Wood", 2}
     });
 }
 }

These are my two scripts for my crafting system. The problem is it doesnt remove items from my inventory when i craft. How could i make it to when i craft something it takes away the right amount of items.

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 Nymisu · Feb 17, 2015 at 09:13 AM 0
Share

Well, there's no relevant code to inventory, or inventory management here whatsoever. All you've shown us is the recipe class (which on a sidenote doesn't need to inherit from $$anonymous$$onoBehaviour) and creating instances of the recipe class.

We can't answer your question if you do not tell us how your inventory works.

1 Reply

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

Answer by badatnames16 · Feb 17, 2015 at 03:37 PM

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Inventory : MonoBehaviour {

 public int slotsX = 5;
 public int slotsY = 5;
 public GUISkin skin;
 public List<Item> inventory = new List<Item>();
 public bool showInventory = false;
 public bool showCrafting = false;
 public static List<Recipe> recipes = new List<Recipe>();
 
 private Item swappedItem;
 private bool draggingItem = false;
 private Item draggedItem;
 private int prevIndex;
 private int spikeCount;
 private Rect invRect = new Rect(200, 140, 350, 420);
 private Rect imageRect = new Rect(0, 0, 50, 50);
 private Rect craftRect = new Rect(0, 40, 350, 350);
 private Vector2 scrollPosition = Vector2.zero;
 
 // Use this for initialization
 void Start () 
 {
     invRect = new Rect(Screen.width / 2 - (invRect.width / 2), 0, invRect.width, invRect.height);
     craftRect = new Rect(Screen.width / 2 - craftRect.width - invRect.width / 2, 0, craftRect.width, craftRect.height);
     
     for(int i = 0; i < (slotsX * slotsY); i++)
     {
         inventory.Add(new Item());
     }

     new RecipeCreator();
 }
 
 // Update is called once per frame
 void Update () {
     if(Input.GetKeyDown(KeyCode.I))
     {
         showInventory = !showInventory;
         showCrafting = !showCrafting;
         
         if(draggingItem)
         {
             inventory[prevIndex] = draggedItem;
             draggingItem = false;
             draggedItem = null;
         }
     }
     
     //Cheat Keys
     if(Input.GetKeyDown("1"))
     {
         AddItem("Wood");
     }
     
     if(Input.GetKeyDown("2"))
     {
         AddItem("Stone");
     }
 }
 void OnGUI()
 {
     GUI.skin = skin;
     
     if(showInventory)
     {
         GetCraftable();
         invRect = GUI.Window(0, invRect, DrawInventory,  "");
         
         if(Event.current.type == EventType.mouseUp && draggingItem)
         {
             if(invRect.Contains(Event.current.mousePosition))
             {
                 inventory[prevIndex] = draggedItem;
                 draggingItem = false;
                 draggedItem = null;
             } else {
                 RemoveItem(prevIndex);
                 draggingItem = false;
                 draggedItem = null;
             }
         }
         
         if(draggingItem)
         {
             CreateDraggedItem();
         }

         if(showCrafting)
         {
             craftRect = GUI.Window(5, craftRect, DrawCraftView, "");
         }
     }
 }
 
 void AddSpikes (float winX)
 {
     spikeCount = (int)Mathf.Floor (winX - 152) / 22;
     GUILayout.BeginHorizontal ();
     GUILayout.Label ("", "SpikeLeft");//-------------------------------- custom
     for (int i = 0; i < spikeCount; i++) {
         GUILayout.Label ("", "SpikeMid");//-------------------------------- custom
     }
     GUILayout.Label ("", "SpikeRight");//-------------------------------- custom
     GUILayout.EndHorizontal ();
 }
 
 void DrawInventory(int windowId)
 {
     AddSpikes(windowId);
     
     GUILayout.Label ("Inventory");
     
     Event e = Event.current;
     
     int i = 0;
     
     for(int x = 0; x < slotsX; x++)
     {
         GUILayout.BeginHorizontal();
         for(int y = 0; y < slotsY; y++)
         {
             GUILayout.BeginVertical();
             GUILayout.Box (inventory[i].itemIcon);
             
             if(inventory[i].multiItems)
             {
                 if(inventory[i].itemCounter > 1)
                 {
                     GUI.Box(GUILayoutUtility.GetLastRect(), inventory[i].itemCounter.ToString(), "ItemCounter");
                 }
             }
             
             if(inventory[i].itemName != null)
             {
                 if(IsMouseOver())
                 {
                     if(e.button == 0 && e.type == EventType.mouseDown && !draggingItem)
                     {
                         draggingItem = true;
                         prevIndex = i;
                         draggedItem = inventory[i];
                         inventory[i] = new Item();
                     }
                     
                     if(e.type == EventType.mouseUp && draggingItem)
                     {
                         inventory[prevIndex] = inventory[i];
                         inventory[i] = draggedItem;
                         draggingItem = false;
                         draggedItem = null;
                     }
                 }
             }
             else {
                 if(IsMouseOver())
                 {
                     if(e.type == EventType.mouseUp && draggingItem)
                     {
                         inventory[i] = draggedItem;
                         draggingItem = false;
                         draggedItem = null;
                     }
                 }
             }
             GUILayout.EndVertical();
             i++;
         }
         GUILayout.EndHorizontal();
     }
     GUI.DragWindow (new Rect(0, 0, 10000, 75));
 }

 void DrawCraftView(int windowID)
 {
     AddSpikes(craftRect.width);

     Event e = Event.current;

     GUILayout.Space(8);

     GUILayout.Label("Crafting");
     GUILayout.Label("", "Divider");

     scrollPosition = GUILayout.BeginScrollView(scrollPosition);

     for(int i = 0; i < GetCraftable().Count; i++)
     {
         if(GUILayout.Button(GetCraftable()[i]))
         {
             Craft(GetCraftable()[i]);
         }
     }

     GUILayout.EndScrollView();

     GUILayout.Label("", "Divider");

     GUILayout.Space(8);

     GUI.DragWindow(new Rect(0, 0, 10000, 75));
 }
 
 bool IsMouseOver()
 {
     return GUILayoutUtility.GetLastRect ().Contains (Event.current.mousePosition);
 }
 
 void CreateDraggedItem()
 {
     Rect tempRect = new Rect (Event.current.mousePosition.x + 5, Event.current.mousePosition.y, 64, 64);
     imageRect = GUI.Window (2, tempRect, DrawDraggedItem, "", "DragImage");
 }
 
 void DrawDraggedItem(int windowID)
 {
     GUILayout.Box (draggedItem.itemIcon, "DragImage");
 }
 
 public void AddItem(int id)
 {
     if(Database.isItemMultiple(id))
     {
         if(InventoryContains(id))
         {
             AddItemToExisting(id);
             return;
         }
     }
     
     for(int i = 0 ; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == null)
         {
             for(int j = 0; j < Database.items.Count; j++)
             {
                 if(Database.items[j].itemID == id)
                 {
                     Database.CopyItem(inventory[i], Database.items[j]);
                 }
             }
             break;
         }
     }
 }
 
 public void AddItem(string name)
 {
     if(Database.isItemMultiple(name))
     {
         if(InventoryContains(name))
         {
             AddItemToExisting(name);
             return;
         }
     }
     
     for(int i = 0 ; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == null)
         {
             for(int j = 0; j < Database.items.Count; j++)
             {
                 if(Database.items[j].itemName == name)
                 {
                     Database.CopyItem(inventory[i], Database.items[j]);
                 }
             }
             break;
         }
     }
 }
 
 public void AddItem(Item item)
 {
     if(Database.isItemMultiple(item.itemID))
     {
         if(InventoryContains(item.itemID))
         {
             AddItemToExisting(item.itemID);
             return;
         }
     }
     
     for(int i = 0 ; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == null)
         {
             inventory[i] = item;
             break;
         }
     }
 }
 
 void RemoveItem(string name, int amount)
 {
     int removed = 0;
     
     for(int i = 0; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == name)
         {
             if(inventory[i].multiItems)
             {
                 if(inventory[i].itemCounter > (amount - removed))
                 {
                     inventory[i].itemCounter -= (amount = removed);
                     break;
                 } 
                 else if(inventory[i].itemCounter == (amount - removed)){
                     inventory[i] = new Item();
                     break;
                 } else {
                     removed += inventory[i].itemCounter;
                     inventory[i] = new Item();
                     if(removed >= amount)
                     {
                         break;
                     }
                 }
             } else {
                 if(removed < amount)
                 {
                     removed ++;
                     inventory[i] = new Item();
                 } else {
                     break;
                 }
             }
         }
     }
 }
 
 void RemoveItem(int index)
 {
     inventory[index] = new Item ();
 }
 
 bool InventoryContains(int id)
 {
     for(int i = 0; i < inventory.Count; i++)
     {
         if(inventory[i].itemID == id)
         {
             return true;
         }
     }
     return false;
 }
 
 bool InventoryContains(string name)
 {
     for(int i = 0; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == name)
         {
             return true;
         }
     }
     return false;
 }
 
 bool InventoryContains(string name, int amount)
 {
     for (int i = 0; i < inventory.Count; i++) {
         if(inventory[i].multiItems)
         {
             if(inventory[i].itemCounter >= amount)
             {
                 return true;
             }
         } else {
             return false;
         }
     }
     return false;
 }
 
 void AddItemToExisting(int id)
 {
     for(int i = 0; i < inventory.Count; i++)
     {
         if(inventory[i].itemID == id)
         {
             inventory[i].itemCounter ++;
         }
     }
 }
 
 void AddItemToExisting(string name)
 {
     for(int i = 0; i < inventory.Count; i++)
     {
         if(inventory[i].itemName == name)
         {
             inventory[i].itemCounter ++;
         }
     }
 }

 public List<string> GetCraftable()
 {
     List<string> craftable = new List<string>();

     for(int i = 0; i < recipes.Count; i++)
     {
         foreach(var mat in recipes[i].items.Keys)
         {
             if(InventoryContains(mat, recipes[i].items[mat]))
             {
                 craftable.Add(recipes[i].itemName);
             }
         }
     }
     return craftable;
 }

 public void Craft(string item)
 {
     List<string> craftable = GetCraftable();

     for(int i = 0; i < recipes.Count; i++)
     {
         if(recipes[i].itemName == item)
         {
             foreach(var mat in recipes[i].items.Keys)
             {
                 RemoveItem(mat, recipes[i].items[mat]);
             }
         }
     }

     AddItem(item);
 }

}

Heres my inventory script

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 badatnames16 · Feb 18, 2015 at 04:26 AM 0
Share

Never$$anonymous$$d i figured it out. it was a problem in my item script.

avatar image Mr-Elite · Apr 19, 2015 at 10:24 PM 0
Share

What are the code to List();

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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

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

Help with crafting system. 1 Answer

Collision Crafting system 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