← Tutorials

Your first brick

A brick is a folder of code and assets that other projects can install, so a component you write once can be reused everywhere. This tutorial takes about fifteen minutes. You need Turian and the .NET SDK installed, and you should have finished the getting-started tutorial, which introduces components and the Inspector.

You will create a brick named com.acme.spinner with one component, install it into a new project and use it twice: once from the Inspector and once from a script. The brick lives in its own folder next to the project, the way a brick you are still developing does.

1. Create the project and the brick

From the folder where you want both to live, create a project and a brick:

turian-cli new HelloBricks
turian-cli brick new com.acme.spinner

The second command creates a com.acme.spinner folder beside HelloBricks. Brick ids are lowercase reverse-DNS names; com.acme is a placeholder, so use your own domain in a real brick. The folder holds:

  • package.json — the brick's name, version, license and the Turian versions it works with.
  • Runtime/SpinnerComponent.cs — a starter component, which you will replace in the next step.
  • Runtime/Acme.Spinner.dataasset — the assembly definition that compiles the Runtime/ code into one assembly.
  • README.md and .gitignore.

Every script and asset has a .meta file next to it. Those ids are how other projects refer to your code, so keep them when you edit the brick.

2. Write the component

Replace the contents of com.acme.spinner/Runtime/SpinnerComponent.cs:

using System.Numerics;
using Turian.Engine.Core;

namespace Acme.Spinner;

/// <summary>Turns its node around the Y axis, at Speed degrees per second.</summary>
public class SpinnerComponent : Component
{
    /// <summary>Degrees per second.</summary>
    public float Speed = 90f;

    /// <inheritdoc/>
    public override void OnUpdate(float deltaTime)
    {
        var node = Node!;
        var rotation = node.Rotation;
        node.Rotation = new Vector3(rotation.X, rotation.Y + Speed * deltaTime, rotation.Z);
    }
}

The namespace is the one brick new chose, Acme.Spinner. Brick code does not get the project's Globals.cs usings, so each file lists the namespaces it needs.

Check that the brick is sound:

turian-cli brick verify com.acme.spinner

The command prints Brick is sound. It checks the manifest and that every asset has a unique .meta.

3. Install the brick into the project

A project declares its bricks in HelloBricks/Bricks/manifest.json. A file: source is resolved from that Bricks folder, so the brick is two levels up:

turian-cli brick add com.acme.spinner file:../../com.acme.spinner --project HelloBricks
turian-cli brick list --project HelloBricks

add writes the declaration, resolves it and prints the brick's version and source. list shows what the project resolves to; bricks you declared directly are marked with *. The manifest now contains a line like this:

{
  "dependencies": {
    "com.acme.spinner": "file:../../com.acme.spinner"
  }
}

A packages-lock.json appears next to it. It records exactly what was resolved, so commit it with the project.

4. Use it from the Inspector

Open the project:

turian-studio --project HelloBricks

Open the starter scene, Assets/scene-01.prefab, select Box in the Scene Tree, and click Add Component in the Inspector. Choose SpinnerComponent. Components from installed bricks appear in that menu the same way the project's own components do. The Inspector shows the Speed field, which you can change for this box.

Save with Ctrl+S, press F5, and the box spins. Press F5 again to stop.

5. Use it from code

A script can find the component on its node and change it. Create HelloBricks/Assets/FastSpin.cs:

using Acme.Spinner;

namespace Usercode;

/// <summary>Makes the node's spinner turn four times faster when play starts.</summary>
public class FastSpin : Component
{
    public override void OnStart()
    {
        if (Node?.GetComponent<SpinnerComponent>() is { } spinner)
        {
            spinner.Speed = 360f;
        }
    }
}

Save it, then select Box and add FastSpin the same way you added SpinnerComponent. Press F5: the box now turns faster, because OnStart runs before the first frame and changes the speed the brick uses. When you stop, the scene returns to its saved state, since play runs on a copy.

The script lives in the project's own assembly, and it sees the brick's types because the brick is installed. There is no assembly reference to set up.

6. Pack the brick for someone else

A folder is convenient while you develop, but other people usually receive a packed .brick file. Pack it from the folder that holds both projects:

turian-cli brick pack com.acme.spinner --out dist

This writes dist/com.acme.spinner-0.1.0--source.brick and a .sha256 checksum with the same name next to it. To try the packed version, point the declaration in HelloBricks/Bricks/manifest.json at it, for example "com.acme.spinner": "file:../../dist/com.acme.spinner-0.1.0--source.brick", and run turian-cli brick restore --project HelloBricks. The project behaves the same, because the brick has the same id and the same assets.

Give each release a new version in package.json. Versions that were published to a registry are never replaced, so bump the version before you publish changes.

Next steps

  • Bricks — installing from the registry or git, forking a brick you do not own, and bricks in continuous integration.
  • Components — the lifecycle, attributes and the Inspector.
  • Test and CI — test your components headlessly and run the tests on every push.

Edit this page