- Home /
Best type of array to use in c# for a large group of items with indexes
In my game, I am at the point of creating an inventory system. There are a large number of items that can be found, purchased, crafted, etc.
Instead of using a random generator for items and their attributes, I want specific items to be created for whatever: a chest, loot, store, etc. What is the best way of creating an array with an index that I can use to determine what set of items to be created in a set of circumstances.
Example: This chest contains numbered items 4,5,7,21 a sword, an axe, a fur boot and leather armor.
The store in this city has items 14-32 only. an antidote potion, water, endurance potion, healing herbs, grape juice, healing potion and light crossbow.
Thanks!
Answer by f34rtehninja · Jul 05, 2012 at 06:15 PM
There's a lot of info on which array to use here: Unify Community Arrays. It sounds like what you want is the built in array since you won't be changing the size of it at all, and built in is the fastest if you don't need dynamic sizing.
You can accomplish what you want two ways, either make one big ItemController script that has a list of all the possible items and what they have or do, your 0-32 different items. Then you can in your chest have a mini array with ints that reference your ItemController array to find out which items are in the list. So this array would be the one that has 4,5,7,21 in it. Here's how you declare them and reference them:
// Here's the declaration in ItemController, drag all the item GameObjects to it in the inspector
Public GameObject[] itemList;
// Here's the chest script stuff, declaration then function for use
Public int[] itemsInChest; // This is the items you want in the chest, 4,5,7,21 or whatever
Public GameObject itemController; // This is the ItemController object you made that holds all of the item references.
void OpenChest()
{
// This is where you would open your chest and transfer items to player inventory or something.
foreach ( int item in itemsInChest)
{
// Transfer them to the player
itemController.itemList[item].GetComponent().GiveInstanceToPlayer() // This is the call to the item script that gives the item to the player
}
}
You could also skip the int list, instead just referencing the item directly in each chest, and then have:
Public GameObject[] itemsInChest;
And call it with
foreach (GameObject item in itemsInChest)
{
item.GetComponent<ItemScript>().GiveInstanceToPlayer();
}
Holding references to an item multiple times takes up more memory, so if you hold ints in the chest that point to where you can find the item it will be faster when you start to have a bunch of chests. It's a bit more complicated though. Make sense?
Yes, makes sense. Thanks! There will be icons for all items so they can exist in the pack/bag and in the equipment slots. Only weapons will also have a gameobject associated with them for instantiation in combat. I wouldn't need separate lists would I?
I'm not sure how you're storing your icons in the hierarchy, but somewhere it's got to be attached to a gameobject. So you can have an items list with all Gameobjects, the ones that hold the icon and the ones that hold the weapons. Or you could make a second list. All of the built in array's use the same syntax. Just do int[] or GameObject[] or Vector3[] or whatever you want. You could make a separate list if you wanted, with the same formatting and everything. The link at the top of my answer details all the different lists available in unity.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
unity3d simple inventory 1 Answer
[C#] Grouping variables 1 Answer
How to set a specific gameobject in an array false when it collides with a trigger. 1 Answer