Search Unity

Resolved Trying to make an inventory system but something isn't working.

Discussion in 'Scripting' started by YoloBoy864, Jan 17, 2022.

  1. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    I already watched Brackeys inventory system tutorial but I didn't wanted to copy everything and instead wanted to make one myself. Sorry if the code is messy and for my bad english.

    This is the script that is on the items in my game, this was fine I think. I'm using scriptableObjects btw.

    Code (CSharp):
    1. public Item item;
    2.  
    3.     Inventory inv;
    4.  
    5.     private void Start()
    6.     {
    7.         inv = FindObjectOfType<Inventory>();
    8.     }
    9.     void OnMouseOver()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.E))
    12.         {
    13.             Destroy(gameObject);
    14.             inv.AddItem(item);
    15.         }
    16.     }

    This is the main inventory script, I created 3 arrays, slots holds the 20 inventoryslots that I have, items stores the items that the players has and itemQuantities stores the amount of the same items a player has (the max stack size of an item is 250). The for loop goes trough every slot checking if there is space for an item or if it can add an item to an already existing stack if it's not full.

    I think the problem has something to do with when I try to add an item to items (under the first if) because everytime I pickup an item, the debug.log just returns 'item[]' instead of 'item1, item2...' .

    Code (CSharp):
    1. public class Inventory : MonoBehaviour
    2. {
    3.     public Transform slotsLayout;
    4.     InventorySlot[] slots;
    5.  
    6.     public Item[] items;
    7.     public int[] itemQuantities;
    8.  
    9.     private void Start()
    10.     {
    11.         slots = slotsLayout.GetComponentsInChildren<InventorySlot>();
    12.  
    13.         items = new Item[20];
    14.         itemQuantities = new int[20];
    15.     }
    16.  
    17.     public void AddItem(Item item)
    18.     {
    19.         for (int i = 0; i < 20; i++)
    20.         {
    21.             if (items[i] == null)
    22.             {
    23.                 items[i] = item;
    24.                 itemQuantities[i] = 1;
    25.                 slots[i].FillSlot(item);
    26.                 Debug.Log(items);
    27.                 break;
    28.             }
    29.             else if (items[i] == item && itemQuantities[i] < item.stackSize)
    30.             {
    31.                 itemQuantities[i]++;
    32.                 slots[i].AddQuantity(itemQuantities[i]);
    33.                 break;
    34.             }
    35.         }
    36.     }
    37. }
    38.  
    The last script that edits the inventory ui is fine I think. Any tips or criticism is welcome.
     
  2. Deleted User

    Deleted User

    Guest

    Standard C# coding conventions suggest you prefix private variables/reference with an underscore (_) and explicitly state it's private (unless it's serialized then there is no underscore). They should also go above all public stuff.

    https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/coding-style/coding-conventions

    Code (csharp):
    1.  
    2. public class Example : MonoBehaviour
    3. {
    4.     private int _privateNumber;
    5.  
    6.     [SerializeField] private int thisHasNoUnderscore;
    7.  
    8.     public int publicNumber;
    9. }
    10.  

    gameObject is a reference to self. What you are telling Unity to do here is:

    Destroy the gameobject this script is attached to. The gameobject (and script) will be destroyed which means the code to add an item is never called.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    I applaud this attitude... good for you! You are living up to your YoloBoy username!!

    Just keep in mind what you're doing and work on one little step at a time.

    Here is my standard caution with regards to getting tangled up in inventory systems:

    Inventories (and shop systems) are fairly tricky hairy beasts. They contain elements of:

    - a database of items that you can possess
    - a database of the items you actually possess
    - persistence of this information to storage between game runs
    - presentation of the inventory to the user (may have to scale and grow)
    - interaction with items in the inventory
    - interaction with the world to get items in and out
    - dependence on asset definition (images, etc.) for presentation

    As you hammer along through your code, keep the above parts in mind and when things get entangled, pull them apart into separate functions and/or separate classes, in order to limit the areas of concern and size of things.

    Just the design choices of an inventory system can have a lot of complicating confounding issues, such as:

    - can you have multiple items? Is there a limit?
    - are those items shown individually or do they stack?
    - are coins / gems stacked but other stuff isn't stacked?
    - do items have detailed data shown (durability, rarity, damage, etc.)?
    - can users combine items to make new items?
    - can users substantially modify items with other things like spells, gems, etc.?
    - does a worn-out item (shovel) become something else (like a stick) when the item wears out?
    - etc.

    Your best bet is probably to write down exactly what you want feature-wise. It may be useful to get very familiar with an existing game so you have an actual example of each feature in action.

    Once you have decided a baseline design, fully work through two or three different inventory tutorials on Youtube, perhaps even for the game example you have chosen above.

    Or... do like I like to do: just jump in and make it up as you go. It is SOFT-ware after all... evolve it as you go! :)

    Breaking down a large problem such as inventory:

    https://forum.unity.com/threads/weapon-inventory-and-how-to-script-weapons.1046236/#post-6769558

    "Combining a bunch of stuff into one line always feels satisfying, but it's always a PITA to debug." - Star Manta on the Unity3D forums
     
    Cato11 likes this.
  4. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    It didn't fix everything but this might have solved a future problem, I now have the destroy after everything. Thanks for the help!
     
  5. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    Thanks for the reply! Tomorrow, I will go trough my code again with these tips in mind! Maybe even rewrite it and try to make it a little bit cleaner.
     
    Kurt-Dekker likes this.
  6. Deleted User

    Deleted User

    Guest

    You could try this instead:

    Code (csharp):
    1.  
    2. for (Item item in items)
    3. {
    4.     Debug.Log(item);
    5. }
    6.  
    So it would be something like:

     
  7. YoloBoy864

    YoloBoy864

    Joined:
    Aug 12, 2021
    Posts:
    39
    I feel so stupid, after testing every line of code with the use of Debug.Log and even trying to switch to lists, I found out the problem was that I disabled the 'icon' gameobject instead of just the image component of this icon. :/
     
    Kurt-Dekker likes this.