- Home /
Assigning scriptable objects to dictionary through array?
Hello. I am trying to create a shop system where the shop updates its inventory with new items whenever the player buys a few. I think that, in order to do this, the shop system would need access to a class that had a list of the items it could add to the store.
I use scriptable objects to store the data of different items. I would like to store these scriptable objects within a dictionary so I can just reference them by name. In the shop system, I would have an algorithm that determines which item to instantiate, find the scriptable object in the dictionary, make a copy of it, assign it to an item prefab, instantiate that item prefab, and put the prefab in the shop based on what was bought. There is not a way to make dictionaries visible in the editor, so I can just use serialize field and drop all the scriptable objects I want in the dictionary. That leads to my main question.
Main Question; Would I have to store the scriptable objects in an array first then send them to the dictionary? Is there a better way to handle updating a stores items that I haven't thought of? Any suggestions on how to solve the dictionary issue or on a better way of updating the shop will be greatly appreciated.
Example Scriptable Object of Item
[CreateAssetMenu(fileName = "New Base Item", menuName = "Items/Base Item")]
public class BaseItem : ScriptableObject
{
public enum ItemType
{
Equippable,
OneUse,
Quest
}
[SerializeField]
private ItemType type;
//there is already a default variabe for name in Unity, so you must declare a new name in order to change its value
[SerializeField]
new private string name = "Base Item";
[SerializeField]
private Sprite icon = null;
[SerializeField]
private bool isDefaultItem = false;
[SerializeField] [TextArea(15,20)]
private string itemDescription = null;
public string Name { get => name; }
public Sprite Icon { get => icon; }
public bool IsDefaultItem { get => isDefaultItem; }
public string ItemDescription { get => itemDescription; }
public ItemType Type { get => type; set => type = value; }
public virtual void Use()
{
}
}
Class the Shop System would refer to when updating it's store. There are several dictionaries in the class based on the type of scriptable object item.
public class ItemTypesCollections
{
//Should scriptable objects be store in arrays here, then assigned to dictionaries?
private Dictionary<string, OneUseItem> oneUseClothingDic = new Dictionary<string, OneUseItem>();
private Dictionary<string, OneUseItem> oneUseFoodDic = new Dictionary<string, OneUseItem>();
private Dictionary<string, OneUseItem> oneUseMiscDic = new Dictionary<string, OneUseItem>();
private Dictionary<string, OneUseItem> oneUseSexDic = new Dictionary<string, OneUseItem>();
private Dictionary<string, EquippableItem> equippableClothingDic = new Dictionary<string, EquippableItem>();
private Dictionary<string, EquippableItem> equibbalbeWeaponsDic = new Dictionary<string, EquippableItem>();
private Dictionary<string, EquippableItem> equibbableSexDic = new Dictionary<string, EquippableItem>();
}