Skip to main content

Tutorial: Getting started with input

So far, our green square is quite happy to spin on its own. Let's give the player something to do: press Space to turn the square into a blue outline, then press it again to bring the solid green square back.

Before you begin​

We'll pick up where Creating your first ECS left off. If you don't have that project handy, you can follow along with the rotating square example on GitHub. Run go run . from the project directory. Once you see the square spinning, close the window and open main.go.

Give the control a name​

We could check for the spacebar directly, but the square doesn't really care which key we press. All it needs to know is when to change its style. In Castrum we can give that instruction a name, called an action, and choose which key triggers it.

Add "github.com/Leonard-Atorough/castrum/input" to the imports, then declare our action outside main:

const toggleStyle input.Action = "toggle_style"

Now let's bind Space to that action. In main, replace the line that creates the game with:

g := castrum.New(
castrum.WithTitle("Input tutorial"),
castrum.WithBindings(input.Bindings{
toggleStyle: {input.KeyInput{Key: input.KeySpace}.Binding()},
}),
)

Pressing Space now triggers toggle_style, but there's nothing listening for it yet.

Change the square once per press​

Our rotation system can carry on as before. We'll add a second system that changes how the square looks. For that we'll need the square's entity handle. If you're discarding it when you create the square, change _, err := g.World().NewEntity( to box, err := g.World().NewEntity(. If you already called it box, you're ready for the next step.

In main, just after registering the rotation system, add the following frame system:

if err := g.AddSystem(core.PhaseFrame, "toggle box style", func(ctx *core.Context) error {
actions := ctx.World.MustResource[input.Actions]()
if !actions.JustPressed(toggleStyle) {
return nil
}
return box.Update(ctx.World, func(p *sprite.Primitive) {
p.Style.Outline = !p.Style.Outline
if p.Style.Outline {
p.Style.StrokeWidth = 4
p.Style.Color = &color.RGBA{B: 255, A: 255}
} else {
p.Style.Color = &color.RGBA{G: 255, A: 255}
}
})
}); err != nil {
panic(err)
}

Run go run . again, focus the game window, and hold down Space. The square should turn into a blue outline and keep spinning. Let go and it stays outlined. Give Space another tap and you're back to the solid green square.

That's why we used JustPressed: our system runs every frame, but we only want to change the square on the frame the press begins. With Held, even a short press could flip the style several times before you lift your finger.

Try a different key​

Let's give Enter the job instead. Close the window, change input.KeySpace to input.KeyEnter in the binding, and run the game again. Space no longer changes the square, but Enter does. Notice what we didn't have to change: the system still listens for toggleStyle and knows nothing about either key.

You can also let the player choose between controls without editing the game. In the tank input demo on GitHub, try driving forward with W, then with the up arrow. Both keys trigger the same action.

Next steps​

There's a lot more to learn about input that will be covered in future guides. If you'd like to learn by playing then try out the Castrum input examples on Github.