- Home /
Storing Scriptable Item Objects in a static array?,Static Array In a Static class for referencing an item database.
Okay so im trying to better understand the best way to access an item database anywhere in the game at any given time. I watched a video far back that used the method of creating a static class and inside it using a Static array of items thats size and values are assigned through Resources.LoadAll(). Im having a hard time believing that this won't cause any issues down the road. Visually I don't see any performance problems nor do I see any performance issues in the profiler that im aware of. Ill leave my code down below to show what im talking about. Each item is a scriptable object that contains the name, itemID, and max stack amount. What im trying to do is create an array that loads all the available items in the game and stores them in one spot as reference. I need this for adding items to the inventory, dropping items on the ground and trading/buying at a shop. I know theres gotta be a better way to do this, its hard to believe this is the best way. I've heard of using a singleton pattern but if thats not gonna make a difference then Id rather use a different method. Im sorry im not the best at explaining things of the top of my head. Here is the code:
public static class ItemDatabase
{
public static Item[] items = Resources.LoadAll<Item>("ScriptableObjects/Items/");
public static Item GetItem(int _itemID)
{
for (int i = 0; i < items.GetLength(0); i++)
{
if (items[i].ItemId == _itemID)
{
return items[i];
}
}
return null;
}
}
Any help would be great! Thanks
Your answer