- Home /
Inventory system using an array...
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.
Also consider the fact that all my work is in c#
Thanks in advance!
Here is a tutorial, that shows you how to create an inventory, that has the most common operations: https://www.youtube.com/watch?v=$$anonymous$$LaGkc87dDQ
Answer by kmeboe · Apr 13, 2013 at 12:53 AM
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):
enum SlotNames
{
Head,
LeftArm,
RightArm,
Chest,
LeftLeg,
RightLeg,
Nethers,
SlotCount
}
GameObject[] slots = new GameObject[(int)SlotNames.SlotCount];
You can then reference each slot like so:
GameObject thingOnHead = slots[(int)SlotNames.Head];
The advantages of this system are:
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.
DubstepDragon, I don't know about "far better"...but thank you for the karma!
Answer by Dracorat · Apr 12, 2013 at 07:21 PM
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 currently have an inventory set up and it's working so far but is extremely difficult to work with in the overall scheme of my game.
Ideally I want to eventually have randomized gear so 1000's of possible gear pieces. $$anonymous$$any will have custom artwork for 2d, and different model placed on character mesh etc.
Right now I am using a list with the following variables stored as ints (I will need to add quite a few more for the final system unless I scrap it):
temporaryItemSlot = what [] position of list will item be. itemTypeint = what slot of armor is used (could be replaced with a string name like example above) itemIdentifierInt = what are the specific stats included for that item type to be added to the playerPrefs (strength, dexterity, intelligence, vitality, etc.) would probably refer to the type of .fbx model on character and any 2d art for inventory as well.
So I am setting these 3 variables in my list, and then using them along with PlayerPrefs to store all the information of what gear the player has picked up, whether it is equipped, what slot in the inventory it is, and what stats it adds to the players current base stats.
I have the first 5 pieces of gear set up perfectly for my game, although admittedly the process of adding new gear is a pain because I have to manually set a bunch of stuff in like 5-6 different scripts so all areas recognize it.
In short I am lacking automation of the list so that once you pick up the item all the work is done for you, and the script functions will cycle through all the list components automatically and do all the work.
Right now for GUI components for example, I have to really put in a lot of specific details for each item and the display in inventory etc.
I am sure some of my problems could be eventually solved by really just toughing it out and going through step by step everything in my functions that must be automated when you pick up or equip an item.
But I am really struggling to find a nice easy way to deal with all this especially when combined with PlayerPrefs? Can you even use PlayerPrefs directly in a list? or you have to assign them manually from the list every time?
I realize it might be hard to help me without more specific information.
But does anyone have any advice on how to make all this flow very smoothly for a complex game with 1000's of possible items due to randomization?
I think I understand how it would be done to work like I want. But it seems so ridiculously complicated compared to all the functions I've designed so far, I'm really delaying even trying to attempt it in hopes I learn something that would make it easier?
It just seems like way to many PlayerPrefs is required for this? Or is that not an issue just messy to deal with?
PlayerPrefs Required per item: +1 inventory slot. +1 item type. +1 identifierInt for art purposes +5 or more for what stats are on the gear and any text descriptions. + any other random info like if it only drops once in the game, or if it enables a new spell etc.
If you had a system of 20 or more item slots you could be dealing with over 200 playerPrefs just to remember your gear?
From what I've seen so far it seems like you can't use PlayerPrefs directly in Lists.
Am I missing something here or is this one of the shortco$$anonymous$$gs with Unity's design? It seems super complicated to store information permanently with PlayerPrefs for a complex system?
I guess I'll just have to try it out, maybe it's not as bad as I think it will be. Looks like a serious pain though.
$$anonymous$$aybe I spoke too soon I am so happy to find this!
Answer by jessee03 · Apr 14, 2013 at 10:51 PM
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!
Your answer
Follow this Question
Related Questions
A problem with arrays 1 Answer
Increasing And Decreasing An Array Through A Variable 2 Answers
Unity freezes on play 2 Answers
Saving 30 bools in an array possible? 2 Answers
Picking a Random Order for Levers 1 Answer