Understanding the Input Map in Godot

 Lets explore Godot’s Input Map. The Input Map, Godot's built in system for managing inputs, allows you to define and manage game controls in a flexible way. Instead of hardcoding input checks, you can use defined actions to add controls, making your game easier to modify and adapt for different input devices.

Lets say you have a game sprite that you want to move left or right using either the arrow keys or maybe a game controller. You don't have to code separate functions to handle input from either the keyboard or a game controller. You can use just use Godot's Input Map.

In the game editor, click the Project tab. Then click Project Settings... Then you can click on the Input Map tab. To see the default inputs click on the Show Built-in Actions toggle.


Now you can see all the default inputs like ui_left, ui_right, ui_up, and ui_down. You can modify what events these inputs listen for. For example, if you wanted to add the keybord letter A to the ui_left input, just click on the + symbol next to ui_left and type A into the text field that appeared. Now the A key will move a sprite left.


You can define your own new Inputs by giving a name in Add New Action text field and click the + Add button.


Once you have your Input Map all set up how you like, you can use the inputs in your script.

Example:

extends Area2D

var speed = 500

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	position = Vector2(100, 100)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if Input.is_action_pressed("ui_right"):
		position.x += speed * delta
		
	if Input.is_action_pressed("ui_left"):
		position.x -= speed * delta

And now the game will respond to either keyboard events or game controller.
Thanks for reading.
 

Comments

Popular Posts