Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Craft System

Discussion in 'Scripting' started by PedroMBP, Jan 2, 2019.

  1. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Hi, guys, how are you?
    I'm a bit in trouble, and I assume I was stuck in it a few days, so I decided to ask for help.
    I'm doing a craft system where the player pushes a button to make the new item, in that item he has an array of items that is the price to craft, ie a shovel would need (wood and stone).
    My code works partially, but I need to verify that the player has the 2 items in his inventory, in which case he only checks one item.
    Would anyone have any suggestions on how to improve this system?

    thank you in advance, any and all help. :)

    Prints


    upload_2019-1-2_17-8-5.png

    Cost of the item, there are 2 linked items and the intention is to compare if it has these 2 items in the inventory.

    upload_2019-1-2_17-10-3.png


    Code (CSharp):
    1. Void CheckHaveItem(){
    2.  
    3.         for (int i = 0; i < itemToSlot.cost.Length; i++)
    4.         {
    5.  
    6.             var checkInventory = buildItems.inventory.slot.Where(x => x.itemToSlot != null).Where(x => x.itemToSlot.Id == itemToSlot.cost[i].Id);
    7.  
    8.             foreach (var item in checkInventory)
    9.             {
    10.                 //Enable and disable button craft
    11.      
    12.             }
    13.         }
    14.  
    15. }
     

    Attached Files:

    Last edited: Jan 2, 2019
  2. PedroMBP

    PedroMBP

    Joined:
    Feb 25, 2017
    Posts:
    21
    Someone ? :(
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @PedroMBP

    What actually isn't working? It would be easier for anyone to help, if the question was clear...

    This sentence...

    "I need to verify that the player has the 2 items in his inventory, in which case he only checks one item."

    deosn't make much sense.

    You can loop the items in your crafting "recipe" and then do a contains check for each of these in your inventory, if any of these is missing, return false.

    Only if every ingredient is found return true from your CheckHaveItem.