This article is the result of Google Translate's English translation, and DrGraph added some corrections on this basis. English original page:
Listening to player input — Godot Engine (stable) documentation in English
Listening for player input¶
Building on the previous lesson of creating your first script , let's look at another important characteristic of any game: handing control over to the player. To add it, we need to modify our sprite_2d.gd
code.
You have two main tools for handling player input in Godot:
-
The built-in input callback, basically
_unhandled_input()
. Like_process()
, it's a built-in virtual function that Godot calls every time the player presses a key. This is the tool you want to use to react to events that don't happen every frame, such as pressing Space to jump. To learn more about input callbacks, see Using InputEvent . -
Input【单例singleton】
. A singleton is a globally accessible object. Godot provides access to several scripts. This is the right tool to check every frame of input.
We'll Input
use a singleton here because we need to know whether the player wants to turn or move every frame.
For rotation, we should use a new variable: direction
. In our _process()
function, replace the previous content with the following code [rotation += angular_speed * 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
Our direction
local variable is a factor representing the direction the player wants to turn. Value 0
indicates that the player did not press the left or right arrow key. A value 1
indicating that the player wants to turn right, -1
meaning they want to turn left.
In order to generate these values, we introduce conditional judgment and usage . A condition starts Input
with a keyword in GDScript and ends with a colon. if
The specific condition is the expression between the keyword and the end of the line.
To check if a key was pressed for this frame, we call Input.is_action_pressed()
. This method takes a text string representing the input action and true
returns if the action was pressed, false
otherwise.
The two actions we used above, "ui_left" and "ui_right", are predefined in every Godot project. They fire when the player presses the left and right arrows on the keyboard or the left and right arrows on the gamepad's D-pad, respectively.
NOTE: You can view and edit input actions in your project by going to Project -> Project Settings and clicking the Input Mapping tab.
Finally, we direction
update the node with the factor rotation
: rotation += angular_speed * direction * delta
If you run the scene with this code, the icon should spin when you press Left and Right.
Move when pressing "UP" ¶
To move only when a key is pressed, we need to modify the code that calculates the velocity. var velocity
Replace the beginning line with the code below .
var velocity = Vector2.ZERO if Input.is_action_pressed("ui_up"): velocity = Vector2.UP.rotated(rotation) * speed
velocity
We initialize with value , another built-in type constant Vector2.ZERO
representing a two-dimensional vector of length 0 .Vector
If the player presses the "ui_up" action, we update the velocity value to make the sprite move forward.
Full script¶
Here is the full sprite_2d.gd
file for reference.
extends Sprite2D var speed = 400 var angular_speed = PI func _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 use the left and right arrow keys to rotate and press Up to move forward.
Summary¶ _
In summary, each script in Godot represents a class and extends one of the engine's built-in classes. The node type that your class inherits from gives you access to base class properties, like in the sprite example rotation
, position
properties. You also inherit a lot of functionality that we don't use in this example.
In GDScript, variables placed at the top of the file are properties of the class, also known as member variables. In addition to variables, you can also define functions, which in most cases will be methods of your class.
Godot provides several virtual functions that you can define to connect your classes to the engine. These include _process()
applying changes to nodes every frame, and _unhandled_input()
receiving input events from the user such as key presses and button presses. There are a lot more.
Input
Singletons allow you to react to player input from anywhere in your code. In particular, you'll be _process()
using it in a loop.
In the next lesson Using Signals , we'll build relationships between scripts and nodes by having our nodes trigger code in the script.