Bind Inputs

We’ve gotten the Architect up and running, so let’s start coding.

So, we’ve started our world. The player can push buttons to their heart’s content, but nothing will move on screen. We’ll need to handle that input, and then use it to move a character around.

We want our game to support input from both the keyboard and from the gamepad. We’ll do this by binding our controls to a single “input axis” in Murder.Input.

For this demonstration we’ll be copying some code from the Ludum Dare 53 entry, Neo City Express. Check out the game if you like – it’s a great reference for how to build a complete game in the Murder engine. The game engine has changed considerably since then but many of the core aspects are similar.

In this section, we’ll:

  • Add an Initialize() method to the main game class
  • Add code to bind the keyboard and gamepad inputs to an input axis.

Open file

  1. First we’re going to look at our Game class instance. Open the file src/HelloMurder/HelloMurderGame.cs.

    The latest code from the repo may look a little different from the examples below, but the code steps should remain about the same.

Implement Initialize method

  1. Right now, there’s no way to move the player character in our game. To get him moving, we’ll need to add some input binding to our game. To start, we’ll implement the Initialize method. IMurderGame classes will call this method on startup, making it a good place for us to register input handlers.

    Add an empty Initialize method underneath the Name property:

    namespace HelloMurder
    {
        public class HelloMurderGame : IMurderGame
        {
            public string Name => "HelloMurder";
    
            public JsonSerializerOptions Options => HelloMurderSerializerOptionsExtensions.Options;
    
            public void Initialize()
            {
            }
        }
    }
    

Bind keyboard controls

  1. Now that our game’s Game class has its own Initialize method, we can add the code to bind our controls.

    Murder provides the Game.Input.Register method. This method binds button inputs into an “input axis,” InputAxis.Movement. Later, the Player Input System will read the state of those controls using the Game.Input.GetAxis method.

    We’ll allow the player two different ways to move using the keyboard: the W, A, S, and D keys, and the Up, Left, Down, and Right keys. These keys are defined in the FNA framework.

    Add the following lines to our Initialize() method:

            public void Initialize()
            {
                Game.Input.Register(InputAxis.Movement,
                    new InputButtonAxis(Keys.W, Keys.A, Keys.S, Keys.D),
                    new InputButtonAxis(Keys.Up, Keys.Left, Keys.Down, Keys.Right));
            }
    

Add references

  1. Your IDE may have added references to a few files. If not, let’s do that before we move on.

    The methods we added refer to the following namespaces:

    • Microsoft.Xna.Framework.Input for game input methods
    • Murder.Core.Input for the Murder engine’s classes
    • HelloMurder.Core.Input for the game’s classes

    So, let’s make sure the top of our file has references to these namespaces:

    using Microsoft.Xna.Framework.Input;
    using HelloMurder.Core.Input;
    using Murder;
    using Murder.Core.Input;
    using Murder.Serialization;
    using System.Text.Json;
    

Bind gamepad controls

  1. Okay, now that the references are settled, let’s add two more bindings to InputAxis.Movement: the gamepad’s left thumbstick and directional pad.

    Luckily, Murder has already defined the gamepad axes for us, GamepadAxis.LeftThumb and GamepadAxis.Dpad, so we just need to reference them.

    Add these lines to our Initialize() method:

            public void Initialize()
            {
                Game.Input.RegisterAxes(InputAxis.Movement, [
                    GamepadAxis.LeftThumb,
                    GamepadAxis.Dpad,
                ]);
    
                Game.Input.Register(InputAxis.Movement,
                    new InputButtonAxis(Keys.W, Keys.A, Keys.S, Keys.D),
                    new InputButtonAxis(Keys.Up, Keys.Left, Keys.Down, Keys.Right));
            }
    
  2. We’ve added some input axes, so we’re done making edits here for now. Save the file before moving on.

Next steps

Next, we’ll modify Murder’s Player Input system to read the input bindings we just added.