Topic was automatically imported from the old Question2Answer platform.
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
P.S. I’m using the latest C# version of Godot on Win10 .
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… :/)
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.