-
Notifications
You must be signed in to change notification settings - Fork 16
At the end of this chapter, your will be able to move pc
sprite by arrow keys.
Godot provides several ways to handle inputs on different levels. The topic is covered in Tutorials/Inputs. Our approach in this demo involves two steps. First register inputs with a specific name in Project Settings/Input Map
. Then respond to input events in scripts by implementing _unhandled_input()
.
Open Input Map
(official tutorial). Bind arrow keys and Vi keys (hjkl) to one of four actions: move_left
, move_right
, move_up
and move_down
.
Add PCMove
(Node2D
node) to MainScene
node. Attach PCMove.gd
to the newly created node. Add InputName.gd
to library/
folder to store action names as string constants. Inside PCMove.gd
, write code to respond to keyboard inputs by printing messages in the console window.
# InputName.gd
const MOVE_LEFT: String = "move_left"
# PCMove.gd
var _new_InputName := preload("res://library/InputName.gd").new()
func _unhandled_input(event: InputEvent) -> void:
if event.is_action_pressed(_new_InputName.MOVE_LEFT):
print("move left")