• Role: Systems programming
  • When: July 2018, ongoing
  • Team: Solo

SmartVars

A Unity tools library for tracking variables and state, in use since 2018.

  • Unity
  • C#

01

Overview

Smart Variables is a system for Unity based on a talk about game architecture by Ryan Hipple. It lets you connect scripts to each other through Scriptable Objects, in a modular and convenient way.

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

One script writes to a variable asset and two other scripts read it
01Scripts share a variable asset instead of referencing each other

02

Adding it to a project

Smart Variables is a Unity package, so it goes into a project through the Package Manager with its git URL.

Unity Package Manager menu with Add package from git URL selected
02Add package from git URL

03

Variables live in assets

A variable is stored as an asset in the project. A component defines a reference to it in code, and in the inspector it is pointed at the asset, so any number of components can read and write the same value without knowing about each other.

A UI Health Points component referencing the PlayerLivesAmount variable, with value 3
03A component pointed at a variable asset

04

Persistence and debugging

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

For easy debugging, a debug log toggle prints every change that happens to the variable.

For persistence in builds, a variable saver can be assigned to variables. It serializes their values to binary files and handles saving and loading.

The settings of a Health Points int variable
04A variable's settings

05

Local collections

Imagine you want multiple players, and therefore a health value for each one. Duplicating variables stored in assets would be quite cumbersome.

For that use case there are local collections, where the variables are stored in a component on an object. Unlike variables stored in assets, these are duplicated together with the object.

A Smart Local Collection component holding two variables
05A local collection on an object

06

Smart variables

The variables described above I call smart references. On top of them is a system of smart variables, which, unlike smart references, can also be set to behave as regular public variables.

06A smart variable in the inspector
See the other projects [email protected]