Optimization: Scriptable Objects
As grows the size of any system, so grows the complexity, and so the accompanying inconvenience of additions or adjustments made to it. You will find that the deeper you delve, the higher the likelihood that one small tweak can corrupt your entire structure. Complexity cannot be avoided, but it can and should be handcuffed to its sober sponsor, Modularity.
Modularity is “the degree to which a system is made up of relatively independent but interacting elements, with each module typically carrying an isolated set of functionality. It is a mechanism for complexity redistribution.”¹
Simply put, a good complex system should be made up of small, adjustable pieces that can interact with while remaining independent from one another. One such tool in the quest for modularization is the Scriptable Object.
To sell it quickly, the Scriptable Object is an intelligent data container in Unity. It’s a lovely standard by which to organize any information database. It can be thought of as an interactive spreadsheet with which to organize, adjust, and categorize different aspects of a database. At its highest form it’s an incredibly lightweight and diverse design tool with which to simplify and communicate between different parts of your Unity project. It’s a major opportunity for restructuring your projects in a macro sense. But onto its simple usefulness…
The Scriptable Object’s fastest friend is the database. Are you building a video game that has a list of items, spells, enemy waves, or tables of information to store? With the Scriptable Object, once you define what’s in each of them, you can create and adjust them in the Inspector without so much as looking at a line of code. Now any game designer can jump into your user friendly interface and make adjustments if they need to.
It takes very few steps to get started.
- Create script and change inheritance from Monobehaviour to ScriptableObject
- Add CreateAssetMenu attribute to the class, define your own Menu entry and use “/” if need to denote a hierarchy for separate entries
- Define any variables and you’re done.
Start basic, and once you continue to learn with Unity, the potential of these will creep up on you. Define the probability table for loot from types of treasure chests throughout your game. Define the stat modifiers for each Level your character gains. Detail the behavior of each enemy wave in a Tower Defense, or the different variables that will accompany varying levels of difficulty your game is played at.
Like a new pair of glasses, they may strain your mind at first, but give them time and you’ll scoff at ever having done without them. And be careful not to get sucked in too deep into the rabbit hole like I did this weekend.
Sources and reference material that gets into more advanced concepts:
Unity 2016 — Overthrowing MonoBehaviour Tyranny in a Glorious Scriptable Object Revolution
Unite Austin 2017 — Game Architecture with Scriptable Objects
AWESOME Tutorial and Code-Along with User Interfaces and Scriptable Objects
Hearthstone Cards as Scriptable Objects by Brackeys
[1] https://asmedigitalcollection.asme.org/mechanicaldesign/article-abstract/140/5/051101/384163
[2] https://healplzgame.com/unity-scriptableobject-spell-system-part-1/