Search code examples
unity-game-engineinventoryscriptable-object

Is ScriptableObject a good database design?


I'm trying to make a simple game database, containing simple items.

My database is simply a ScriptableObject, containing a list of other ScriptableObject (Items) that holds informations about items and hold a string key.

var item = DB.GetItem("weapon.magic_sword");

1 - Is there a better solution than using key string to identify items ?

2 - If I want to have generated item. For instance adding randomly stats to existing "templates" item, am I forced to create a MonoBehaviour script for Item and instanciate this from the template scriptable object ? I use streaming assets to save the inventory of the player, so I need to be able to use those generated item in streaming assets.

PS: I prefer avoid a real database (such as sql)


Solution

  • I'm going to break down your question:

    1. Not sure what you mean by "database" - but if you mean it in the actual sense, no, ScriptableObject can't be used to implement an actual DB
    2. ScriptableObject is great for storing meta data on items (base armor, damage, cost, etc)
    3. If you want to store dynamic data of the user progression (locally), the simplest and most robust solution IMO is serializing your inventory class and storing it in the PlayerPrefs. We use JSON.NET and store it as a string; if you want to be more optimized/secure, you can use some binary serialization (Protobuff, built in binary serialization, etc)
    4. I would suggest working with generated GUIDs versus working with named strings, it adds a risk of human error. Instead simply add an autogenerated GUID to each object and get/set it by it's unique ID.

    I hope I understood your question correctly and answered it.