Why does event.isActionPressed(action) is called twice in the _Input function?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cyb9x

Hello,

I found this engine and I wanted to try something really simple at first - 2d character movement. I read in the documentation that the recommended way to check for input is by using Input Map. I defined there one action “move_up” with the key W.

In the code I wrote:

public override void _Input(InputEvent @event)
{
     if (@event.IsActionPressed("move_up"))
     {
         GD.Print("up pressed");
     }
}

And in the game after each W press, I get:

up pressed
up pressed

I don’t understand why it is called twice… it’s the same even if I release the key. There is also another problem… Each code snippet from the documentation here seems to call that twice…

I hope someone can answer that :slight_smile:

P.S. I’m using the latest C# version of Godot on Win10 .

:bust_in_silhouette: Reply From: juppi

You should better use the Input class and check the actions in your game loop:

public override void _Process(float delta)
{
    if (Input.IsActionJustPressed("move_up"))
    {
        GD.Print("up pressed");
    }
}

This has to work. It’s also smoother, because you want to check for input every frame. If you use the _Input method instead, it’s possible that your player stutters.

Just want to point out that this is a workaround that can be used when making a game, but not a solution to the problem.

If you’re making an app and you want to take advantage of low-cpu-mode, getting input during process will prevent low-cpu-mode from happening. In that case you want to handle input only ever in the _input or _unhandled_input functions, but you’ll still have this problem to deal with. (Which is my case… :/)

woopdeedoo | 2021-10-22 20:53

I know this topic is old but I wish to add something that I didn’t know either as a beginner.

public override void _Input(InputEvent @event)
{
     if (@event.IsActionPressed("move_up") && @event.isPressed())
     {
         GD.Print("up pressed");
     }
}

This will result in one event fire. Worked for me. Hope it will help someone else in the future.

2 Likes

I would also ad there are also “echos” (key repeats) depending on how long you held the key. There are functions to check this.

As well as there is an action event check called is_action_just_pressed that will ignore echos and signify the transition of the key. There is a similar check for release.

2 Likes