This is the third post of the series on Scriptable Objects. The first two posts intended covering the basics of scriptable objects. In this and the next post, we’ ll investigate two different use cases of scriptable objects.
What you understand from the title, Scriptable Variables? These are the variable assets that are scriptable objects in original, thus, serialized by unity.
Being serialized means they’ re persistent so any changes in the value of a scriptable object in the unity editor will be stored in its meta file.
Why would we need something like this?
One of the biggest problems in programming is setting up the relationships between classes. Every class must know its place and musn’ t deal with a variable in another class.
This is expressed as “a class should only tell what to do to another class” in the book “Clean Code”.
Another thing is in software design, classes should depend on abstractions instead of concrete classes. The reason behind this is, having an extendable and clean code.
After emphasizing some of the principles behind software architectures, let’s come to the point:
Making a game in unity which has a solid foundation behind it.
Well, for those who studied software engineering in college, these are familiar concepts. That’ s why we think in that way but what about for those who has a design background? Or generally speaking for those who has a different background other than engineering? Do they have to spend some years on software engineering?
Of course NO. Also the idea behind Unity is “Democratizing the game development”. That’s why there must be some other ways, easier than applying software architecture principles, to develop games with relatively solid foundations, right?
This is where Scriptable Variables come into our lives.
How we use Scriptable Variables?
Assuming we’ve an implementation, i’ ll tell you the usage below:
- Create an asset file which has the desired type. It might be an asset which stores int, float, boolean or some other types.
- Assign a value
- Register this asset to the MonoBehaviours or other scriptable objects so they can reach the value
Generally speaking that’ s all 😀
The key point here, whenever a script modify the value of the registered scriptable variable, all other scripts will have the updated value momentarily.
So once the player modifies the value of PlayerHP, represented in the image above, all other objects that have reference to the PlayerHP will have the updated value momentarily.
I think that’ s enough talking on theory, let’s look into how we can implement Scriptable Variables.
The first thing is a base class:
Making this class a generic one will make things easier because all the variable classes will have the same implementation other than their type.
BaseScriptableVariable here is a base class which inherits from ScriptableObject. Having a non generic base class might be useful for future extensions in the codebase but it’ s not a must have.
After implementing our base class we create the concrete variable classes like below:
That’s all for the implementation. Let’s look into how we use this scriptable variables now.
Once we have the implementations represented in the previous section, we’ ll have a menu to create variables as below:
As you can see in the video above, we create an asset file which stores an integer value. We might then register this asset file to any script which defines an exposed IntVariable as follows:
Now the created IntVariable asset can be registered to this mono behaviour as below:
You can register this asset to any amount of scripts/objects. All of them will be able to get/set its value and they won’ t have to have any reference to another class or something to get the value.
Pros And Cons
We are using these scriptable variables for sometime. I’ ll try to share my thoughts on them below:
- Reduces coupling between classes
- Reduces debugging time because you’re able to see the actual value directly
- Allows rapid testing/iterations because you can modify the value easily
- Hard to track the modifications. Any scripts, that has reference to the asset, might change the value, thus makes debugging harder in some situations.
- Open for mistakes as someone can forget resetting the value after making some tests
I tried to tell a use-case of the scriptable objects as scriptable variables in this post. Hope you enjoyed reading,
PS: You might see the implementations on https://github.com/emre2345/generic-unity-event