This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

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 .

Godot version 3.3.2 stable
in Engine by (12 points)
edited by

1 Answer

+1 vote

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.

by (1,081 points)

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... :/)

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.