-
Notifications
You must be signed in to change notification settings - Fork 14
Guide 102: Introduction to SmartObjects
SmartObjects, as noted previously, are the ScriptableObject
assets at the core of SmartData. This guide will cover the creation and editing of SmartObjects.
The simplest two types of SmartObject are the SmartVar and EventVar. Other types will be introduced in section 5 of the Guide.
SmartVars hold one piece of data. For example, a FloatVar
contains a float
, and a BoolVar
contains a bool
. The value is serialized and appears in the inspector for the SmartVar asset. Everything with a reference to the same SmartVar asset has shared access to the same value.
When the value is changed - whether from code, the inspector, a UnityEvent etc - the SmartVar dispatches an event with the new value. This can be used in code and in the editor (using UnityEvents) to receive updates without polling.
It's possible to create SmartVars with underlying List, Dictionary or array types. However in most cases SmartData provides better alternatives for these use cases. These will be introduced in section 5 of the Guide.
To create a SmartObject, right click in the Project window (or click Assets > Create on the top menu) and go to Create > SmartData. In this case we'll create a Float Variable.
A Float Variable in code is
SmartData.SmartFloat.Data.FloatVar
. However, in most cases you should not use this directly. See section 4 of the guide for more info.
This creates a new asset in the folder the Project window is currently in.
Selecting the new asset will show the following inspector.
- Description has no effect on how the SmartVar works - it's just there so everyone on the team understands what each SmartVar is intended for.
- Reset On Scene Change automatically resets the value of the SmartVar to the default (i.e. whatever the value is before the game is played) when a scene loads. Without this checked, the value will persist over scene loads.
- Value allows reading and changing the value itself.
- Decorators are a bit like components for SmartObjects. They modify behaviour and are modular. They will be introduced in section 5 of the Guide.
- GameObject References shows which GameObjects in the scene have a reference to this SmartObject.
The value will automatically be reset when you exit Play mode.
EventVars are events wrapped in a ScriptableObject
asset. They work exactly like SmartVars, but don't hold a value. When you dispatch an EventVar, anything else referencing that asset and listening to the event will receive a callback.
EventVars are so called to distinguish them from EventMultis, which hold multiple EventVars. They will be introduced in section 5 of the Guide.
Like SmartVars these are created from right clicking in the Project panel and navigating to Create > SmartData. However, while each type of SmartVar has a submenu, EventVars are created directly from here.
As before this will create an asset in the current Project panel folder.
Since they store no data, EventVars don't have a value field or a Reset On Scene Change setting.
However they do still have description, decorators and a panel to show links from other GameObjects.
At this point it's a good idea to jump into the Example 01 scene (you'll need to check out the feat-examples branch to do this). Hopefully it should be reasonably self-explanatory.
In all example scenes a NOTES object is present with some description of the scene and how it works.