- Home /
How to make different types of Objects in database?
How should I make a database of different types of buildings, where each one of them does different things(has different variables and functions). I am thinking of Inheritance, but not too sure how I should implement it. The main problem is with MonoBehaviour. Some of these buildings will need the Update() function, for example, to generate money every second. How do I store monobehaviour scripts to a scriptableobject database then?
You can inherit from $$anonymous$$onoBehaviour without breaking Update()
or other message functions. Just put Update()
in your parent class, and have it call some other function, e.g. doUpdate()
, then override doUpdate()
in your child classes
Answer by Xarbrough · Feb 25, 2018 at 11:28 PM
Why do you need a database? Or why do you think you need to store MonoBehaviour scripts in a ScriptableObject? ScriptableObjects are not databases, btw, although I know you are probably just using this term loosely. ScriptableObjects are very similar to MonoBehaviours, but they can be serialized (e..g save to the project folder as an asset) without having to be attached to a GameObject in a scene. On the C++ side of the engine, both classes are in fact treated the same way.
If you need Update, you will need a MonoBehaviour in the scene, so a building prefab is a good idea. However, you can decide which data to store with the prefab and which data to store in additional ScriptableObjets.
If you use inheritance, Update is still no problem because you can make the base class Update a virtual method and override in child classes (and call base.Update() if you also want the base class behaviour).
Generally, I would favour composition over inheritance. I've implemented a strategy game with buildings, where the building class was only a very thin wrapper that contained a tile position. Then we had components like "ResourceProducer", which was basically a timer which would increase the count of ScriptableObject data containers like "ItemInventory".
Thank you for the answer, I got the idea, but I have a ScriptableObject with a list inside it, which is basically a database of different type of buildings that I have. I figured it out now tho.