if Input.is_action_pressed("q"):
number -= 1
I need help making the number var to change by 1 whenever the Q key is pressed. This code doesnt work for some reason. It doesnt change the number at all. also I have all of this within a _process function. Also I only want the number to increase once per key press.
Do you have an action named “q”? Is there any warnings or errors in the log?
Yes I went to the input map and made an action named “q” with the input being q
Can you show more of the code? How are you checking this variable’s value?
extends Node2D
var number = 1
func _ready() → void:
number += 1
func _process(_delta: float) → void:
print(number)
if Input.is_action_pressed("q"):
number -= 1
if Input.is_action_pressed("e"):
number += 1
if number < 1:
number = 1
Test the action, by putting a print_debug(“q is pressed”) in is_action_pressed code block.
You might need to setup mapping in Godot to make that work. Looks like you are trying to read a keyboard button, but using action instead.
Godot->Project->Project Settings->Input Map
This will probably only work for the “e” action, otherwise it’s going to go below 1 and reset
I am trying to use the input map. the names are equivalent to the key that its mapped to.
yes, but test to see if the code block is getting run by putting print or print_debug in there.
where would I put the print at? could you type an example?
I just fixed it myself. forgot to give the inputs an input key. they were just names. sorry.
but how do I make it so it only registers one input and not keeps +1 or -1 every frame?
Override the _input
function or use Input.is_action_just_pressed
2 Likes