Game Architecture with Scriptable Objects

insights

Note: This is an edited re-post. See the original post on Ryan's blog.

Unite 2017 wrapped up in Austin last week and I finally got to give a talk that was all about ScriptableObjects (and ride a jackalope). I have been exploiting these things since I first discovered the under-documented feature back in 2011.

Session Details

Game Architecture with Scriptable Objects focuses on making modular, data-driven, editable game systems while avoiding (and trash talking) crutch patterns like singletons.Using ScriptableObject based classes, you can easily edit and store references to data from prefabs in a scene. However, ScriptableObjects are not constrained to just static data. You can change data from one prefab and read it from another. This allows for the exchange of state between prefabs without the need for a rigid connection between them.A similar pattern can be used to construct an event system. Scene objects can add themselves to a list on a ScriptableObject based asset to indicate that they are listening for a certain game event. Later, a different scene object can "raise" the event, looping through the list and notifying all listening objects. Again, this pattern removes rigid connections between different systems in the game.Game Architecture with Scriptable Objects covers these topics and more, with a ton of simple code samples, practical demos, and bad programming jokes.The video is below.

The slides are on SlideShare The sample project is on GitHub

Follow Up

One common question I got about the FloatVariable is how to avoid having the runtime data save when the game stops. The truth here is that the sample I showed in the session has been dramatically simplified from what I normally use so it does not handle this use case. You can use OnEnable to copy your default value into a non-serialized current value. Then you only access through the CurrentValue property. An added benefit here is that a float property can be set by a UnityEvent while a float field cannot.

Game arch floatvariable

Another note with these is that you may want to add DontUnloadUnusedAsset to the HideFlags to make sure you do not lose state if nothing is referencing it.The full implementation of this variable system that we use at Schell Games has a bunch more features, custom icons, and a generic base class making it easier make new variable types. Same thing goes for our event system. While I can not post the source for these things, I hope that the concept helps people start hacking apart ScriptableObjects more!

Related Reading:Matt Schell's Unity blog post on ScriptableObjectsSchell Games and UnityEngineering a Super Spy for 'I Expect You To Die'Building a 2.5D Model in Unity