Beginner question: User input and variable labels

Godot Version

4.4.1

Question

Hello Guys,

i am a complete beginner (i did some programming in 2010 or so but that is like… forever nowadays) in programming and trying to get a grasp around godot.

Right now i am stuck at the most basic thing: Get my label to show the value of an element in an array, changed by user input. I have following code and structure:

It seems my user input is not detected as i get no print(event.as_text()) in the Output window.
It won’t give me an error message, and the initial scene shows my prefab text of “test1”, instead of 100, 90, 80 etc.

1 Like

Hi,

“action” in the context of is_action_just_pressed refers to an action input defined in the project input settings (Project > Project Settings > Input Map). Have you defined an action called “action” and with a valid key?

For instance:
image

1 Like

Hi,
yeah sorry, i forgot to include that, i already mapped two keys for that

But neither Space nor the LMB does any work

Seems good to me.
Can you move your print instruction above the if statement, like so:

func _input(event):
    print(event.as_text())
1 Like

You need to use is_action_just_pressed in _process() function (not in _input() function)
You can use is_action_pressed in _input() function.

Try this instead. It should trigger only once when you press ‘action’ button.

func _process(delta: float) -> void:
	if Input.is_action_just_pressed('action'):
		action()

EDIT: the following information is wrong but I’ll keep it for the sake of the history of this topic :slight_smile:

If you try is_action_pressed() in _input() function, it will be triggered for each input event while ‘action’ button is pressed. So unless you are very quick to press and unpress ‘action’ button, you will see multiple calls of action() function

1 Like

@lastbender That’s wrong, using is_action_pressed inside _input will work fine and not consider the input being held.

Here’s a test with that code:

func _input(evt: InputEvent):
	if evt.is_action_pressed("interaction"):
		print("Interacted!")

By keeping my interaction key pressed for a few seconds, I only get one log, which is the intended behaviour for that code.

However, when checking inputs inside _process, it’s true that is_action_pressed will return true while the key is being held.


@beronlhitosa When trying your code, I noticed that there’s actually no is_action_just_pressed function inside input events. Could you try with a simple is_action_pressed instead? Like so:

func _input(event):
    if evt.is_action_pressed("action"):
        action()
        print(event.as_text())
3 Likes

So, it might be that the solution of lastbender helps but i will try your way to as _input() seems more logical to me to use (as of right now)

1 Like

_input is event based so for an action that’s triggered punctually like yours, it is indeed the way to go.

Checking inputs in _process is something you would like to do for stuff like movement, where you need to read some direction every frame, not just wait for a key to be pressed at some point.

2 Likes

@lastbender
@sixrobin

Thank you both for yout time, i can close this thread. I tried both ways _process and _input and i found the difference in the input speed, as i can “speed spam” the button in _input with a faster resolve as in _process
Why that is? No idea and i’m to far at the start of this journey but i might find out. Anyway, thanks a lot you two!

@sixrobin is right about _input. I was wrong about that.

_input is called immediately when there is an input (like pressing the keyboard button)
_process is called for every frame. So you may end up pressing the button but the frame may be called a bit later which explains why you experience that _input is faster. And that is normal behaviour.

2 Likes