Listening to player input
Creating your first script, let’s look at another important feature of any game: giving control to the player. To add this, we need to modify our Sprite.gd code.
You have two main tools to process the player’s input in Godot:
_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.Inputsingleton. 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.Inputsingleton here as we need to know if the player wants to turn or move every frame.direction. In our_process()function, replace therotation += angular_speed * deltaline with the code below. GDScript C#var direction = 0if Input.is_action_pressed("ui_left"): direction = -1if Input.is_action_pressed("ui_right"): direction = 1rotation += angular_speed * direction * delta
var direction = 0;if (Input.IsActionPressed("ui_left")){ direction = -1;}if (Input.IsActionPressed("ui_right")){ direction = 1;}Rotation += AngularSpeed * direction * delta;
directionlocal variable is a multiplier representing the direction in which the player wants to turn. A value of0means the player isn’t pressing the left or the right arrow key. A value of1means the player wants to turn right, and-1means they want to turn left.Input. A condition starts with theifkeyword in GDScript and ends with a colon. The condition is the expression between the keyword and the end of the line.Input.is_action_pressed(). The method takes a text string representing an input action and returnstrueif the action is pressed,falseotherwise. 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.directionas a multiplier when we update the node’srotation:rotation += angular_speed * direction * delta. If you run the scene with this code, the icon should rotate when you press Left and Right.Moving when pressing “up”
var velocitywith the code below. GDScript C#var velocity = Vector2.ZEROif Input.is_action_pressed("ui_up"): velocity = Vector2.UP.rotated(rotation) * speed
var velocity = Vector2.Zero;if (Input.IsActionPressed("ui_up")){ velocity = Vector2.Up.Rotated(Rotation) * Speed;}
velocitywith a value ofVector2.ZERO, another constant of the built-inVectortype representing a 2D vector of length 0. If the player presses the “ui_up” action, we then update the velocity’s value, causing the sprite to move forward.Complete script
Sprite.gdfile for reference. GDScript C#extends Spritevar speed = 400var angular_speed = PIfunc _process(delta): var direction = 0 if Input.is_action_pressed("ui_left"): direction = -1 if Input.is_action_pressed("ui_right"): direction = 1 rotation += angular_speed * direction * delta var velocity = Vector2.ZERO if Input.is_action_pressed("ui_up"): velocity = Vector2.UP.rotated(rotation) * speed position += velocity * delta
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.using Godot;public class Sprite : Godot.Sprite{ private float Speed = 400; private float AngularSpeed = Mathf.Pi; public override void _Process(float delta) { var direction = 0; if (Input.IsActionPressed("ui_left")) { direction = -1; } if (Input.IsActionPressed("ui_right")) { direction = 1; } Rotation += AngularSpeed * direction * delta; var velocity = Vector2.Zero; if (Input.IsActionPressed("ui_up")) { velocity = Vector2.Up.Rotated(Rotation) * Speed; } Position += velocity * delta; }}
Summary
rotationandpositionin 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._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.Inputsingleton allows you to react to the players’ input anywhere in your code. In particular, you’ll get to use it in the_process()loop. Using signals, we’ll build upon the relationship between scripts and nodes by having our nodes trigger code in scripts.
