Skip to content

Guide 103: Introduction to SmartRefs

SIGTRAP edited this page Oct 31, 2018 · 4 revisions

Basic use of SmartData involves SmartObjects and SmartRefs. SmartObjects hold data (and/or events), as discussed in the previous chapter, while SmartRefs link them to code and GameObjects.

Examples 01, 02 and 03 demonstrate much of what this chapter covers. It's recommended to explore them while reading.

Reading and Writing

SmartRefs can be Readers or Writers. Readers only have read access to data, while Writers can read and write. This makes it easier to limit which parts of your game actually change data, and therefore easier to trace bugs.

There are different variants of SmartRefs but most share the same core features. Each SmartType has its own set of SmartRef variants - for instance, FloatReader, BoolWriter and so on.

EventVars are slightly different - they have EventListener (can only listen to events) and EventDispatcher (can listen to and dispatch events).

SmartRefs are plain, serializable C# classes. Declaring a public (or private [SerializeField]) SmartRef field in a MonoBehaviour class gives the inspectors shown below.

In the Editor

Here's an IntReader, FloatWriter and EventListener as they appear in the editor.

  • The RW label shows whether this is a Reader (RW) or a Writer (RW).
    • For events this is Listener (LD) or Dispatcher (LD).
  • The field on the right shows the linked SmartObject.
    • Drag-and-drop SmartObject assets to set, like any other asset or prefab.
  • Check Auto Listen for the attached UnityEvent to fire when SmartObject's value changes or it is dispatched.
    • Without Auto Listen, Writers will always fire their UnityEvent when they update the value, but not when something else does.
    • Auto Listen can also be set from code.
  • The V dropdown is used to change the kind of SmartObject the SmartRef links to. By default this is a SmartVar (V) but other modes will be discussed in Guide sections 2 and 5.

It's possible to directly reference SmartObjects from code rather than going via SmartRefs. However, this removes the majority of the power of SmartData and is not recommended.