0

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)

dvr33
  • 145
  • 1
  • 3
  • 11
  • Is the purpose of this to persist changes over the course of the game? Or to just act as a table to look up data about items? – Erik Overflow Feb 04 '20 at 19:26
  • The inventory content can change during the course of the game. However, the item themself that are added / removed are fixed (except the items that are a variation of existing items with random stats). – dvr33 Feb 04 '20 at 20:07
  • What about when the player closes/reopens the game? Should the data persist? – Erik Overflow Feb 04 '20 at 20:56
  • Yes, the inventory is persistent between game session. I plan to save the list of string id in a streaming asset monobehaviour. However, I don't know how to handle generated item because I guess that I'll need a Monobehaviour which holds the string id and the stats modification of the item. – dvr33 Feb 05 '20 at 02:10
  • You wouldn't need a monobehaviour to store modified stats. You could store it in a standard class that isn't a monobehaviour. That being said, changes to ScriptableObjects do not last between gameplay sessions. You cannot use a scriptableobject as a database. – Erik Overflow Feb 05 '20 at 02:41
  • Write/Read a proper file to the `Application.persistentDataPath` – derHugo Feb 05 '20 at 06:38
  • The SO is appropriate and convenient to forward your DB data as it allows drag and drop. SO are only persistent in Editor so for your runtime operation, you need to store either in PlayerPrefs or some txt file of your own. Not that PP are stored in registry which makes it harder (not impossible) to hack. – Everts Feb 05 '20 at 06:54
  • @Everts in the registry only on the PC, on a phone e.g. they are simply stored as raw text file -> even easier to hack ;) If you want something more secure you will always have to implement some sort of encryption on your own – derHugo Feb 05 '20 at 07:06
  • My bad, I was stuck on the PC description. So yep, encrypt or store on server (with encryption). – Everts Feb 05 '20 at 08:52

1 Answers1

0

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.

Ron
  • 1,806
  • 3
  • 18
  • 31
  • You understood correctly. I serialize via App.persistentDataPath and Streaming Assets. But, how can I autogenerate GUID ? It seems effectively better than strings. – dvr33 Feb 06 '20 at 16:23
  • Not 100% sure Unity supports GUIDs, we use [Odin](https://odininspector.com/) which supports it. But you could very easily do one yourself and create a [property drawer](https://docs.unity3d.com/ScriptReference/PropertyDrawer.html) for it – Ron Feb 11 '20 at 17:09