DataAsset-Driven Design
DataAssets are your own data, edited in the Inspector. This page goes one step further: a DataAsset can also be a channel that connects parts of your game. An event asset connects a button to the code that reacts to it, a variable asset is a value that several components read, and a response asset says what happens when an event arrives. A designer can connect these in Studio without writing glue code, and the publishing and the listening objects never reference each other.
This is the designer-wired style. It lives in the DataAsset-Driven Design brick (org.mass4.turian.ddd), and the
ButtonComponent that raises click events comes from the UI brick (Game UI).
Install them as you install any brick, see Bricks.
Asset families
Every family is a DataAsset you create from the Assets panel's context menu under New → DDD.
| Asset | Holds | Used by |
|---|---|---|
DataAssetEvent |
An event with no payload: Raise(), Subscribe(...). |
Buttons, listeners, responses, input. |
DataAssetEvent<T> |
An event with a payload, for float, int, bool, Vector2, Vector3 and asset enums. |
Code that sends a value with the event. |
DataAssetVariable<T> |
A shared value. Value is the session value, DefaultValue is the authored start value, and OnChanged is an event raised after each change. |
Components that read or write a value, and the UI. |
DataAssetConst<T> |
A constant value you author once. | Settings that never change at run time. |
DataAssetRuntimeSet<T> |
A set of nodes, assets or asset enum values, with Add, Remove and Items. |
Code that needs every item of a kind, such as all enemies. |
DataAssetResponse |
One action, such as raising events or adding to an integer variable. | Listeners and transitions. |
AssetEnumValue |
A named value, such as a state or a faction. It is an enum you extend in Studio. | State machines and typed variables. |
DataAssetStateTransition |
From, To, a Trigger event and a list of responses. |
State machines. |
InputReader |
An input action, exposed as OnPressed and OnReleased events and as Held, AxisValue or VectorValue variables. |
InputReaderComponent. |
The common payload types are already compiled, so creating one of these assets never needs a script. Other event or variable types need your own class.
Wiring in Studio
The example project's demo scene shows the whole chain. The button raises Events/Click. A listener on the scene's
score node runs the Responses/AddScore response, which adds to Variables/Score. The HUD reads the same
variable. A state machine consumes the same event through Transitions/Start.
The steps to build the same chain:
- Create an event asset, such as
Click, and assign it to the button's OnClick field. - Create an integer variable, such as
Score. - Create an int response, which has a Variable, an Amount and an Add flag, and point it at
Score. - Add a
DataAssetListenerComponentto a node, set its Event toClick, and add the response to its Responses list. - Open View → DataAsset Events while the game is playing. It shows live values, lets you raise an event by hand, lists the listeners of each event and reports broken wires after you rename something.
Changes you make to these assets are saved as asset content. Stop Play to reset the runtime values, the listener subscriptions and the events that were queued but not yet delivered.
Referencing assets from components
A component refers to a DataAsset with a plain field, the same way it refers to a node or another asset in Components. The demo driver in the example declares its references like this:
namespace Usercode;
public sealed class DemoDriver : Component
{
public DataAssetEvent? Click { get; set; }
public DataAssetVariableInt? Score { get; set; }
public DataAssetVariableAssetEnum? Faction { get; set; }
}
From code, you raise events and read or write variables. The following component subscribes to an event while it is enabled and adds to a variable:
using Turian.Engine.Core;
namespace Usercode;
public sealed class ScoreBoard : Component
{
IDisposable? clickSubscription;
public DataAssetEvent? Click { get; set; }
public DataAssetVariableInt? Score { get; set; }
public override void OnEnable() => clickSubscription = Click?.Subscribe(AddPoint);
public override void OnDisable() => clickSubscription?.Dispose();
void AddPoint()
{
if (Score is not null)
{
Score.Value += 10;
}
}
}
Dispose the subscription when the component is disabled, as shown, so a disabled component stops receiving events. A
typed event sends its payload with Raise(payload), and OnChanged on a variable sends the new value after each
change.
When a field should be either a fixed value or a shared variable, declare it with the wrapper
Turian.Engine.DataAssetDriven.DataAssetReference<T>. It has UseConstant to choose between ConstantValue and
Variable, and Value reads whichever is in use. Writing through the wrapper requires variable mode and an assigned
variable. The wrapper works as a direct field of a component or a DataAsset, but not inside a list.
The engine also has a different type with the same name, Turian.Engine.Core.DataAssetReference<TData>, which is the
handle the asset loader uses. Import one namespace or use an alias when a file needs both.
Responses and state machines
A listener runs its responses when its event is raised. The brick provides DataAssetRaiseResponse, which raises a list of
events, and DataAssetIntResponse, which sets or adds to an integer variable by Amount. To do something else, subclass
DataAssetResponse and implement Execute().
A state machine is a component with an InitialState, a list of Transitions, and a CurrentState that it exposes as
it runs. Each transition has a From state, a To state, a Trigger event and its own responses. States are
AssetEnumValue assets, so a designer can add a new state without a code change. The example's Start transition
moves the machine from Idle to Running when Click is raised, and its CountTransition response counts how
often it happened.
Project settings are DataAssets
Every project setting is a DataAsset too. The project's entry point is Assets/Project.dataasset, which lists:
- Settings, one entry per settings kind the project uses, such as player, graphics, physics or tags and layers. A kind listed twice uses its first entry.
- BuildProfiles, the build profiles of the project, and ActiveBuildProfile, the one that builds run with. A profile holds overrides of the project settings, usually as variants of the project's own assets.
A settings kind is a class that derives from ProjectSettingsAsset. The engine's own settings are examples. The tags
and layers settings, for instance, declare [CreateAssetMenu(fileName: "TagsAndLayersSettings", path: "Settings/Tags and Layers")],
and that menu path is the label of the entry under Project → Project Settings.
Your own settings follow the same pattern:
namespace Usercode;
[CreateAssetMenu(fileName: "GameSettings", path: "Settings/Game")]
public sealed class GameSettings : ProjectSettingsAsset
{
public float MasterVolume = 1f;
public int MaxPlayers = 4;
}
Add a reference to the asset in the Settings list of Project.dataasset, and read it from code with
IAppSettings.Get<GameSettings>(). Get returns the stored settings, or the declared defaults when the project does
not have them.
Services read their configuration this way. The audio service is built from IAppSettings.Get<AudioSettings>(), and
the physics scene is built from Get<PhysicsSettings>(), falling back to the defaults when the project has no such
asset. A service you write can do the same, so its configuration is an asset that a designer edits.
Limits and gotchas
- Values exist only while playing. Outside a session,
Valuereturns the authoredDefaultValue, and writing a variable or raising an event requires a running session. Stop Play to reset everything. - Dispatch runs on the game-loop thread.
Raise,Subscribeand variable writes must run on the game loop. From a worker thread, useTryQueue, which delivers the payload at the next fixed-tick boundary. It returnsfalsewhen the bounded queue is full. - Listener exceptions stop the raise. A listener that throws stops delivery of that raise to the listeners after it.
- Per-tick and per-contact code stays direct. Physics callbacks, particle updates and anything that runs every frame should call methods directly. Events are for game logic that a designer connects.
- Variables are not saved. DataAsset-driven design has no save format of its own. Store the values you need through the save brick.
- Not included yet: per-prefab private variables, list and dictionary assets, and an automatic injector for references.
- Authored assets cannot reference nodes. The rule from Assets applies here too: a designer connects assets to each other, and a component connects a node to an asset.
Next steps
- Assets covers the basics of data assets, ids and loading.
- Components explains the fields and lifecycle that the examples use.
- Game Saves stores the values that variables hold between sessions.
- Game UI shows how buttons raise events and read enabled and visible variables.