Input and Events in Quorum

This tutorial shows how to use input and events in our games.

Input Polling

In this tutorial, we will learn about using player input and the game engine’s event system. With these tools, our games will be able to react to mouse and keyboard input, as well as to colliding game objects.

The first method we will discuss for getting player input is known as polling.Polling is a technique which involves regularly checking the current state of the mouse or keyboard to see if the player is giving us any input.

To do this in the Quorum Game Engine, we must include the InputMonitor library with the statement use Libraries.Game.InputMonitor. If we want to monitor keyboard input, we will also include the KeyboardEvent library with the statement use Libraries.Interface.Events.KeyboardEvent.

To see input polling in action, we will use a sample program which will play a sound when we press the spacebar on the keyboard or when we click inside the screen. To download a sample project that includes audio files, click here.

To begin with this sample, let’s start by looking at the objects we will use in our program.

InputMonitor monitor
KeyboardEvent keys
Audio keySound
Audio mouseSound
  • The InputMonitor class is what we will use to poll the mouse and keyboard for input. We will see how this is used in the Update action.
  • The KeyboardEvent is typically used for getting player input through event handling, which is an alternative approach compared to polling. However, in this case we will use it to test if a particular key has been pressed because it also contains descriptive values for all supported keys on the keyboard so we don't have to use numeric codes.
  • The Audio class will be used to play our sounds when the user gives us input. For more information on how Audio works, see Playing Audio Files in Quorum.

   action Update(number seconds)
       // If the user is currently clicking the mouse, play a sound.
        if monitor:IsClicked()
            mouseSound:Play()
        end

       // If the user is pressing the spacebar, play a sound.
        if monitor:IsKeyPressed(keys:SPACE)
            keySound:Play()
        end
    end

The two if statements are where we check for input. In if monitor:IsClicked() we ask the InputMonitor to check if any of the mouse buttons are currently being held down. If any of them are, we will enter the code inside the if block.

Then we reach if monitor:IsKeyPressed(keys:SPACE) . The InputMonitor action IsKeyPressed will test if a particular key on the keyboard is being held down. Inside the action, we have to tell Quorum which key we want to test, so we give it the value of SPACE from the KeyboardEvent class. To see the names of all the keys refer to the KeyboardEvent documentation.

These two actions are enough to test for input from both mouse and keyboard, but the InputMonitor has other actions to poll for other kinds of information, such as the current location of the mouse. For full details on what an InputMonitor can do, see its documentation page here.

Try it out below:

We put our InputMonitor in the Update action because we want to check (poll) the status of the inputs at every frame. This is where we test for input, and react accordingly.

Try it Yourself!

Press the blue run button to execute the code in the code editor. Press the red stop button to end the program. Your program will work when the console outputs "Build Successful!"

Next Tutorial

In the next tutorial, we will discuss Keyboard Events in Quorum, which describes How to use keyboard events in our games..