Listening to player input

    You have two main tools to process the player’s input in Godot:

    1. The built-in input callbacks, mainly _unhandled_input(). Like _process(), it’s a built-in virtual function that Godot calls every time the player presses a key. It’s the tool you want to use to react to events that don’t happen every frame, like pressing Space to jump. To learn more about input callbacks, see Using InputEvent.

    2. The Input singleton. A singleton is a globally accessible object. Godot provides access to several in scripts. It’s the right tool to check for input every frame.

    We’re going to use the Input singleton here as we need to know if the player wants to turn or move every frame.

    For turning, we should use a new variable: direction. In our _process() function, replace the rotation += angular_speed * delta line with the code below.

    GDScript   C#

    1. var direction = 0;
    2. if (Input.IsActionPressed("ui_left"))
    3. {
    4. direction = -1;
    5. }
    6. if (Input.IsActionPressed("ui_right"))
    7. {
    8. direction = 1;
    9. }
    10. Rotation += AngularSpeed * direction * delta;

    Our direction local variable is a multiplier representing the direction in which the player wants to turn. A value of 0 means the player isn’t pressing the left or the right arrow key. A value of 1 means the player wants to turn right, and -1 means they want to turn left.

    To check if a key was pressed this frame, we call . The method takes a text string representing an input action and returns true if the action is pressed, false otherwise.

    The two actions we use above, “ui_left” and “ui_right”, are predefined in every Godot project. They respectively trigger when the player presses the left and right arrows on the keyboard or left and right on a gamepad’s D-pad.

    Note

    You can see and edit input actions in your project by going to Project -> Project Settings and clicking on the Input Map tab.

    Finally, we use the direction as a multiplier when we update the node’s rotation: rotation += angular_speed * direction * delta.

    If you run the scene with this code, the icon should rotate when you press Left and Right.

    To only move when pressing a key, we need to modify the code that calculates the velocity. Replace the line starting with var velocity with the code below.

    GDScript   C#

    1. if (Input.IsActionPressed("ui_up"))
    2. {
    3. velocity = Vector2.Up.Rotated(Rotation) * Speed;
    4. }

    If the player presses the “ui_up” action, we then update the velocity’s value, causing the sprite to move forward.

    Here is the complete Sprite.gd file for reference.

    GDScript   C#

    1. using Godot;
    2. public class Sprite : Godot.Sprite
    3. {
    4. private float Speed = 400;
    5. private float AngularSpeed = Mathf.Pi;
    6. public override void _Process(float delta)
    7. {
    8. if (Input.IsActionPressed("ui_left"))
    9. {
    10. }
    11. if (Input.IsActionPressed("ui_right"))
    12. {
    13. direction = 1;
    14. }
    15. Rotation += AngularSpeed * direction * delta;
    16. var velocity = Vector2.Zero;
    17. if (Input.IsActionPressed("ui_up"))
    18. {
    19. velocity = Vector2.Up.Rotated(Rotation) * Speed;
    20. }
    21. Position += velocity * delta;
    22. }

    If you run the scene, you should now be able to rotate with the left and right arrow keys and move forward by pressing Up.

    ../../_images/scripting_first_script_moving_with_input.gif

    In summary, every script in Godot represents a class and extends one of the engine’s built-in classes. The node types your classes inherit from give you access to properties like rotation and position in our sprite’s case. You also inherit many functions, which we didn’t get to use in this example.

    In GDScript, the variables you put at the top of the file are your class’s properties, also called member variables. Besides variables, you can define functions, which, for the most part, will be your classes’ methods.

    Godot provides several virtual functions you can define to connect your class with the engine. These include _process(), to apply changes to the node every frame, and _unhandled_input(), to receive input events like key and button presses from the users. There are quite a few more.

    In the next lesson Using signals, we’ll build upon the relationship between scripts and nodes by having our nodes trigger code in scripts.