Introduction to Game Input and Events in Quorum
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.
Input PollingThe 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
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.
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.
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
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
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.
Try it out below:
Try the above code block.
In the next tutorial, we will discuss Keyboard Events in Quorum, which describes How to use keyboard events in our games..