Tutorials / Getting started

✔ OfficialBeginner⏱ 15 minTallow 0.2 (notes for 0.1)

Your first script

Scripts give objects behaviour. In this tutorial you will write a component that makes a cube bob up and down, then change it while the editor keeps running.

Step 1: Create the script

In the Project panel, open the src/Scripts folder. Right-click an empty spot (or press +) and choose Script > Basic Component. Name it Bobber and press Enter.

New projects already have a script called Spinner, which is why this one has a different name. Script names use letters, digits and underscores, and start with a letter.

The new file opens in the Scripts window, the built-in code editor. It has completion that knows the engine.

Step 2: Write the code

Replace the contents with this, changing MyFirstGame to your project's name:

using System;
using Tallow;

namespace MyFirstGame;

[Component]                          // every component class needs this
class Bobber : Script
{
    public float height = 0.5f;      // public fields are saved and shown in the Inspector
    public float speed = 2;

    Vector3 mStart;                  // private fields are just for the script
    float mTime;

    public override void Start()
    {
        mStart = Transform.Position;
    }

    public override void Update(TickInput input, float deltaTime)
    {
        mTime += deltaTime;
        Transform.Position = mStart + Vector3(0, Math.Sin(mTime * speed) * height, 0);
    }
}

A few things to notice:

  • Start runs once, before the first update.
  • Update runs every frame. Use the deltaTime it is given for anything that changes over time, so speed doesn't depend on frame rate.
  • Transform is the object's position, rotation and scale.

On 0.1: derive from Component instead of Script, write public override void Update() with no arguments, and use Time.DeltaTime in place of deltaTime.

Step 3: Save and let it compile

Press Ctrl+S. The editor compiles the script in the background and reloads it without restarting. If something is wrong, the Console shows the error. Click it to jump to the line.

Step 4: Put it on an object

  1. Add a cube with GameObject > 3D Object > Cube.
  2. In the Inspector, press Add Component, type bobber and pick it.
  3. The Height and Speed fields appear, because they are public.

Press Play. The cube bobs up and down.

Step 5: Tune it live

While playing, drag Height and Speed in the Inspector and watch the cube change. When you stop, the values go back. Set the ones you like while stopped, so they are saved with the scene.

Now try changing the code while the editor is open. For example, add sideways sway to Update:

Transform.Position = mStart + Vector3(Math.Cos(mTime) * 0.3f, Math.Sin(mTime * speed) * height, 0);

Save, and the change is picked up. Script hot reload explains what reloads in place and what needs a restart.

Step 6: React to a key

Make Space pause the bobbing. Add a field and check the controls in Update:

public bool paused;

public override void Update(TickInput input, float deltaTime)
{
    if (input.Pressed(.Jump))       // Jump is Space by default
        paused = !paused;
    if (paused)
        return;
    mTime += deltaTime;
    Transform.Position = mStart + Vector3(0, Math.Sin(mTime * speed) * height, 0);
}

In 0.2, controls come from input, not from the keyboard directly. That is what lets the same script work in multiplayer later. Writing scripts in 0.1 and 0.2 has the details.

You have written a component. Next, make things fall and collide.