I had inquiries about this topic since a very long time. How can you make an array of objects that is not unlimited? for example, an array with 20 entries, each corresponding to a specific item slot in the inventory. Can anyone help me out?
Please do not give me links unless they are specifically for this subject.
Any answer with LINQ in it is automatically amazing.
However, I wanted to suggest an alternative for your stated need which has some advantages in a closed system where the number and type of slots is fixed.
Here’s the code (replace GameObject with the actual type of your stored item, if you prefer):
Speed (low constant time – essentially a pointer lookup – vs O(n) with string comparisons)
Secure lookups: as long as you stick to accessing the array with the enum, you are assured of never accessing an incorrect slot
…and possibly readability (for those who are unfamiliar with LINQ and lambdas – although everyone should learn these).
One disadvantage over the LINQ system is that you will have to convert the enum to a string for display to the user (e.g. Head.ToString()), although this can be done upfront to avoid repeated performance penalties.
I’m not going to say that this solution is strictly better than Dracorat’s, but it’s another approach to consider that has its advantages.
I think it's a great solution, so heres a TU for awesomeness.
There is no issue with me over converting the enumeration to a string - I would quite like that. As i said above, I am looking for a swift and hasty inventory system that works, and that is all I need. I think this method is far more reliable and sustainable than the other one posted by Dracorat, but hey! you both deserve the karma, but I just thought this answer is far better than the other.
Inventory inventory = new Inventory[20]; // Where “Inventory” is the name of the type you want to use for the array.
If you want closer cohesion to slot types, create a custom class:
public class InventorySlot {
public int SlotName { get; set; }
public InventoryItem ItemInSlot { get; set; } // Where InventoryItem is a class that is an inventory item
}
Then for the array…
public List<InventorySlot> inventorySlots = new List<InventorySlot>();
InventorySlot inventorySlot = new InventorySlot();
inventorySlot.SlotName = "Helm";
invontorySlots.Add(inventorySlot);
You would do that if you wanted to use LINQ to easily select a slot
var headSlot = inventorySlots.FirstOrDefault(i => i.SlotName == "Helm");
Lists are theoretically unlimited (2^32 items) but you don’t have to use them as such.
I would recommend this method (using a List with a class like Dracorat suggested).
I quite agree with the idea of having playerprefs in this - a very easy way to save all the inventory items, gold, etc... But right now I am focusing on a simple inventory that has basic functions - might get to the playerprefs later if i have time - but anyway, thanks a lot for the help man!
I recently started a more advanced inventory video series covering this. Hope it helps you out if you haven’t figured it out already.
Are you Jesse Etzler? Wow, I am a huge fan of your tutorials and I have followed every single one from the start to about #75 so far! Your tutorials are simple and yet amazingly effective. But no, I have not seen the latest inventory tutorials. I will make a mental note to check them later today. Hope you have a nice day!
Why yes I am :) Glad to help out a fellow fan! But ya my brand new tutorials are covering every step to create an advanced inventory / equipment menu, ect. :P Hope it helps ya out out. You have an amazing day as well!
Here is a tutorial, that shows you how to create an inventory, that has the most common operations: https://www.youtube.com/watch?v=KLaGkc87dDQ
– Artifactx