0
\$\begingroup\$

Lets say we want to store game data inside of scriptableObjects and we have plenty of them... So we need to find a way to store them efficiently in a modular way.

What pattern could we use to code a scriptableObject database for providing a substainable base for our game data- scriptableObjects?

And are there any patterns or structures for extending our game data - scriptableObjects in order to construct them modular and flexible aswell ?

\$\endgroup\$
  • 1
    \$\begingroup\$ Can you give us more concrete details of what kinds of ScriptableObject types you need to manage this way, and what kinds of operations your database needs to support? With no constraints, then either vanilla references or built-in collection types might be the simplest answers, but since you're asking here I'd wager you have more precise needs that these don't service out of the box. So tell us about those needs and we can offer tailored solutions. \$\endgroup\$ – DMGregory Dec 4 '19 at 0:37
1
\$\begingroup\$

You haven't provided enough detail to give a specific answer to your question, but here are some broad strokes:

ScriptableObject assets in your Unity source project are saved as individual files in the project Assets directory.

If you've got dozens of these asset files, you can probably just manually organize them into folders. E.g. Assets\Data\Enemies\Normal and Assets\Data\Enemies\Bosses, and refer to them from your components with serialized field references. E.g.

public class EnemyData : ScriptableObject {
    [SerializeField] private int hp = 10;
    public int HP => hp;
}

public class Enemy : MonoBehaviour {
    [SerializeField] private EnemyData data;
    private int hp;

    void Start() {
        hp = data.HP;
    }
}

If you've got hundreds or thousands of files, it can get cumbersome to manage them all in the project pane and to reference them from your scripts. Then, there are several ways to ease the management of these assets:

  • You can create a custom editor window for browsing through and editing your assets. It's impossible to go into any further detail without knowing your specific use case.
  • You can store the assets in a Resources folder and load them with the Resources system. Unity recommends not using the Resources system, but it can be useful for referencing large quantities of small files like ScriptableObject assets. For example, if your game has 1000 level data assets, you could store them in Assets/Resources/Levels and access them like this:

(ignore this line)

public LevelData GetLevelData(int levelNumber) {
    LevelData data = Resources.Load<LevelData>("Levels/" + levelNumber.ToString());
    return data;
}

There's also a new Addressable Asset System which serves a similar purpose but I haven't had a chance to use yet.

  • You can also store large amounts of data in one asset by storing serialized types or arrays of serialized types. For example:

(ignore this line)

[System.Serializable]
public class SerializableLevelData {
    [SerializeField] private float duration;
    public float Duration => duration;
}

public class LevelCollection : ScriptableObject {
    [SerializeField] private List<SerializableLevelData> levels;
    public SerializableLevelData GetLevel(int index) {
        return levels[index];
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.