1

I'm relatively new when it comes to game development, but I've spent a dozen or so hours researching about how to use Scriptable Objects (SO).

I don't know how many items my game is going to have, so I would like to automate the item-making-process as much as possible to avoid human error. I've read it's good for performance to store information in one place using Scriptable Objects:

ItemData.cs

public class ItemData : ScriptableObject
{
    public int itemID;
    new public string name;
    public Sprite sprite = null;
}

Next, I have a gameObject that loads all itemData into a dictionary on Awake:

ItemDatabase.cs

private Dictionary<int, ItemData> itemDB = new Dictionary<int, ItemData>();

void Awake()
{
    LoadItems();
}

private void LoadItems()
{
    //Load all itemData
    ItemData[] itemData = Resources.LoadAll<ItemData>(itemDirectoryPath);

    for(int i = 0; i < itemData.Length; i++)
    {
        //Set sprite of itemData, to avoid doing it manually
        itemData[i] = SetSprite(itemData[i]);

        //Add itemData to dataBase
        itemDB[itemData[i].itemID] = itemData[i];
    }
}

Lastly, I have a function for creating a gameObject of a specified itemID at runtime:

//Create new gameobject at location
public void SpawnItem(int itemID, float x, float y)
{
    //Get itemData
    ItemData itemData = getItemData(itemID);
    //Create new gameObject
    GameObject gameObject = new GameObject();
    //Set name of gameObject, add Item Script, and set position
    gameObject.name = itemData.name;
    gameObject.AddComponent<Item>().itemData = itemData;
    gameObject.transform.position = new Vector3(x, y, 0);
}

Questions:

  1. Is it considered "hacky" to create an item at runtime as shown in SpawnItem above? Do I have to manually create each item as a prefab?
  2. If using "Resources" is considered bad practice, is there a better solution besides manually dragging each item onto the ItemDatabase Script?
Jaitnium
  • 621
  • 1
  • 13
  • 27
  • Are you using the SpawnItem to create prefabs in editor or are you using it to spawn items during gameplay? Same question for ItemDatabase. Is it using Resources.Load in editor or in gameplay? – Thomas Finch Sep 27 '20 at 04:36
  • 2
    1. It's good idea to have those spawns prepared as prefabs. The only bad thing now imo is that you spend runtime cpu on things that are already known at edit time (assuming spawn means runtime spawn). 2. Resources are not preloaded in memory, they are read from disk (so if the data is big it can add delay). Referencing directly in the inspector means the referenced asset will be preloaded in memory when the referencing scene/prefab is loaded. Inspector/resources is a tradeoff between speed/memory. 3. ScriptableObject is good for code/data that you want to be assignable/draggable in inspector. – Nikaas Sep 27 '20 at 09:22
  • @ThomasFinch I'm using SpawnItems to spawn items during gameplay. I'm using Resources.Load in ItemDatabase during gameplay, but it only happens once at the start of the program. – Jaitnium Sep 27 '20 at 14:58
  • @Nikaas Thank you SO much for the explanation! The reason I was trying to generate items programmatically was to avoid human error, but also because I wanted users to be able to mod in their own items (which wouldn't be known at edit time). I suppose I'm too early on in development to be thinking about that. – Jaitnium Sep 27 '20 at 15:04

0 Answers0