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.
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…’ .
public class Inventory : MonoBehaviour
{
public Transform slotsLayout;
InventorySlot[] slots;
public Item[] items;
public int[] itemQuantities;
private void Start()
{
slots = slotsLayout.GetComponentsInChildren<InventorySlot>();
items = new Item[20];
itemQuantities = new int[20];
}
public void AddItem(Item item)
{
for (int i = 0; i < 20; i++)
{
if (items[i] == null)
{
items[i] = item;
itemQuantities[i] = 1;
slots[i].FillSlot(item);
Debug.Log(items);
break;
}
else if (items[i] == item && itemQuantities[i] < item.stackSize)
{
itemQuantities[i]++;
slots[i].AddQuantity(itemQuantities[i]);
break;
}
}
}
}
The last script that edits the inventory ui is fine I think. Any tips or criticism is welcome.
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.
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!
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.