-
Notifications
You must be signed in to change notification settings - Fork 14
Guide 104: Using SmartObjects
This chapter will show how to use SmartObjects in the Unity editor in three high-level ways.
- SmartVars as shared data
- EventVars for game events
- SmartVars as events with data payloads
Keep exploring Examples 01, 02 and 03!
The simplest way to use SmartData is to share data between different parts of your game without hard-coding or coupling.
For example, storing the player's HP in a FloatVar
gives easy access to anything which would need to know it. This avoids singletons, and makes it easy for coders and designers to get the data.
By using FloatReaders
in all places except on the Player
object itself, we can guarantee that only the Player
can actually change their health. This might be through a modular Health
or Damageable
MonoBehaviour
, or it might be rolled into a monolithic Player
class.
This is similar conceptually to having a static health property in the
Player
code:public static float hp {get; private set;}
This is a screenshot of the SmartGraph which shows the links between SmartRefs and SmartObjects in a scene (Window > Smart Graph editor).
Here, the Player object (the bottom Player node) has a FloatWriter
and the HUD text object (the top PlayerHp node) has a FloatReader
. PlayerHp has read-only (dotted) access to the FloatVar
while Player has read/write access.
The SmartGraph is one of many features that depends on use of SmartRefs rather than direct code references to SmartObjects.
Game-level events such as "Game started", "Player killed" and "Game paused" are needed in almost all games, and EventVars provide a simple, powerful high-level messaging system. EventListeners can be added to any GameObject or script - by designers or coders - which needs to respond to an event, without any hard-coding or coupling.
As with Writers for SmartVars, EventVars
can only be dispatched by EventDispatchers
. So it's easy to keep track of what parts of your game actually set these events off. Other objects can easily and safely respond to them via EventListener
's in-built UnityEvent
or from code.
As before, the Player object has listen/dispatch access to the EventVar
while the Particle System only has listen access.
When the EventVar
is dispatched, the EventListener
here receives the event and trigger its UnityEvent
, which causes the ParticleSystem
to fire.
EventVars are great for notifying objects across your game that something has happened. However, in many cases you need to pass some data as well.
For example, a "Level loaded" event might need to pass the name of the level. This is easily done with a SmartVar (i this case, a StringVar
). SmartVars fire their events whenever their value is changed, or their Dispatch()
method is called. This way, they can be used as typed events.
Note that this isn't mutually exclusive to the "shared data" use-case - you can check a SmartVar's value or just wait for an update event.
When using a SmartVar strictly for a typed event, consider naming conventions such as "OnThingHappened" rather than "NameOfData". Naming conventions such as these - and use of the description field - help avoid miscommunications between coders and designers.