Smart Variables

Solo

Ongoing

Started July 2018

Systems Programming

Unity


Smart Variables is a system for Unity based on a Presentation about Game Architecture by Ryan Hipple.

It lets the user connect scripts to each other through a system of Scriptable Objects in a modular and convenient way.

The system is being used in and developed alongside Jello. It has proven itself to be very useful, and at this point I feel that it's ready for anyone else to use.

Using Smart Variables in your project

In Unity, click Window->Package Manager

Add package from git URL: https://github.com/gavriktonio/SmartVariables.git

The package contains examples that you can download through the package manager

System Overview


This system lets the user to separate data from logic.

A variable is stored in Assets

A component defines an int variable reference in code

In the inspector, we link the proper variable to be used

All systems, that need to use the HealthPoints int, only depend on the variable, not on each other. That's why we can swap the systems using the variable at any time, or get rid of them completely.

This modularity can be used in many ways. Below are some examples that can be achieved for Input.

Persistence, Easy Debugging


Because the variables are stored in assets, they can be persistent. With a simple switch, the variable will stay the same inbetween play sessions and scene loads.

For easy debugging, a debug log toggle can be turned on, which will print all the changes, happening to the variable.

For persistence in builds, a variable saver can be assigned to variables, that will serialize their values in binary files and handle saving/loading

Event System


The biggest change that I did, compared to the Original Presentation , is that I merged Smart Variables and the event system together.

Any script can subscribe to changes that happen to a particular Variable Reference.

That works very well for UI, for example, because HealthUI can just subscribe to hp, and trigger it's logic only when health gets changed.

Internally, the event system has a queue that ensures that all callbacks are run before a new change to the variable happens.

Local Collections


Imagine if you want to have multiple players, and therefore, multiple health point values per each one. It would be quite cumbersome to duplicate variables, stored in assets.

Just for that usecase, there are Local collections, where the variables are stored in a component belonging to an object

Unlike when storing the object in assets, these variables are going to be duplicated together with the object.

Smart Variables and Smart References


The variables, that are described above, I call Smart References. On top of it, there is a system of Smart Variables. Unlike smart references, they can be set to behave as regular public variables.

How does it work internally


The base SmartReference class inherits from Unity ScriptableObject. ScriptableObjects can be stored in Assets, and are persistent between scenes and play sessions.

Defining a new variable type is as easy as eating a cookie. It can contain any class or type.

Accessing the variable value happens through a .Value property, that ensures that only the runtime value gets returned and that all variable change callbacks are triggered (explicit conversion operators are also provided that let the user omit .Value).

Custom Editor code is in place to make sure working with the system is smooth, and callbacks work when changing a variable from the editor.

For more info on the system, and working with Scriptable Objects in general, I highly suggest the presentation I got the idea from.