Data-driven design
A data asset is a C# class whose instances are saved as files in Assets/ and edited in the Inspector. Game values such as health, speed and prices then live in data, and a designer can change them without a code edit or a recompile. This tutorial takes about fifteen minutes. You need to have finished the getting-started tutorial, which covers creating a project, writing a script and Play Mode.
1. Create a project
turian-cli new DataDemo
turian-studio --project DataDemo
The starter scene Assets/scene-01.prefab has a Box, which you will use in step 4.
2. Define the data
Create Assets/EnemyData.cs:
namespace Usercode;
/// <summary>An enemy definition that designers tune in the Inspector.</summary>
[CreateAssetMenu(path: "Game/Enemy", fileName: "Grunt")]
public class EnemyData : DataAsset
{
/// <summary>The name shown in the log.</summary>
public string DisplayName = "Grunt";
/// <summary>Hit points when the enemy spawns.</summary>
[Range(1, 500)]
public int MaxHealth = 30;
/// <summary>Metres per second.</summary>
[Range(0, 10)]
public float Speed = 2f;
}
DataAsset is the base class for these classes. [CreateAssetMenu] adds the class to the Assets panel's Create menu, and [Range] clamps the values that can be typed or scrubbed in the Inspector. The fields are ordinary public fields with default values, which become the values of a new asset.
3. Create an asset in the Inspector
Once Studio has compiled the script, right-click in the Assets panel and choose New → Game → Enemy. Studio creates Grunt.dataasset and a Grunt.dataasset.meta file beside it. The .meta file holds the asset's id, so keep both files under version control.
Select Grunt.dataasset in the Assets panel. Its three fields appear in the Inspector. Type a value above 500 into Max Health and it is clamped to 500, the upper bound of the [Range].
4. Reference the asset from a component
Create Assets/EnemySpawner.cs:
namespace Usercode;
/// <summary>Logs the numbers of the enemy it was given when the scene starts.</summary>
public class EnemySpawner : Component
{
/// <summary>The enemy to spawn. Drop an EnemyData asset onto this slot in the Inspector.</summary>
public EnemyData? Definition;
public override void OnStart()
{
if (Definition is null)
{
Log.Logger.LogWarning("No enemy definition assigned to {Node}", Node?.Name);
return;
}
Log.Logger.LogInformation("{Name} spawns with {Health} health, speed {Speed} m/s",
Definition.DisplayName, Definition.MaxHealth, Definition.Speed);
}
}
Select Box in the Scene Tree and click Add Component in the Inspector. Choose EnemySpawner. Then drag Grunt.dataasset from the Assets panel onto the Definition slot. Save the scene with Ctrl+S.
The component stores a reference to the asset, not a copy of its values. The scene file records the asset's id, and the component reads the asset's current values when it runs.
5. Play
Press F5. The Output panel shows Grunt spawns with 30 health, speed 2 m/s. Press F5 again to stop.
6. Tune without code
Select Grunt.dataasset in the Assets panel. Set Max Health to 45 and Speed to 4. Studio saves an edit to an authored asset as you make it, so there is no Apply step.
Press F5. The output now reads Grunt spawns with 45 health, speed 4 m/s. You changed no code and nothing was recompiled.
7. Share one asset or give each user a copy
Every data asset is a single shared object. Add a second node with an EnemySpawner and drop the same Grunt.dataasset onto it. Both spawners read the same values, so a change to the asset changes both.
When one user needs its own numbers, copy the asset at runtime with DataAsset.Instantiate. The copy gets a new id, and changing it never reaches the original:
var mine = DataAsset.Instantiate(Definition!);
mine.MaxHealth = 10;
The copy lives only in the running game. It is not written back to Grunt.dataasset.
Things to know
- Values changed while the game is playing are not written to the asset file. Stop Play to reset them to the saved values.
- A data asset cannot hold a reference to a node or a component, because an asset outlives any scene. It can hold a reference to another data asset, by declaring a field of that type. The compiler warns (
TUR0001) when a data asset tries to hold a scene reference.
Next steps
- Services with dependency injection — share a service between components and choose its lifetime.
- Assets — the asset kinds, the
.metafiles and how assets ship. - Components — declaring components and referencing other objects.