Keyboard inputs work fine in editor but not when running games?

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

I am new at the Godot Engine and am having an awful learning curve. I am however making slow process.

I am having a particularly persistent and annoying problem that I have spent nearly a week trying to resolve, and lots of fruitless searching and today, accidentally destroyed my project trying to find the problem!

When running the games, both the project I was trying to build and numerous game -making tutorials, I can’t get anything to move using keyboard inputs. These work perfectly in the editor but not when running the games.

I have meticulously followed instructions on Godot’s tutorials, as well as YouTube videos, etc. I keep coming up with the same results every time. I have even copied and pasted the code from these tutorials into the script editor, including Godot’s 3.1 documentation.

This worked at first, and then, trying to create a pong clone, the paddles would go along the x axis rather than the y - and of late, nothing works. I have no idea what I might have done to cause this and is discouraging. If anyone has had this issue or knows what might be causing it, I would appreciate some feedback.


:bust_in_silhouette: Reply From: Zylann

These work perfectly in the editor but not when running the games.

What do you mean? The game doesn’t run in the editor. It only launches either from the editor, or once you export it. You mean keyboard input doesn’t work in exported games?

Did you try a very simple project with just one sprite and a script like:

extends Sprite

func _process(delta):
    if Input.is_key_pressed(KEY_D):
        # Move right when D is held
        position += delta * Vector2(100, 0)

This is one of the most basic ways to get input.

is_action_pressed() should come next when you want to have input remapping (player settings to change keys for example), in which case you would create such mapping in your ProjectSettings by giving names to each, and then use it like this:

func _process(delta):
    if Input.is_action_pressed("move_right"):
        position += delta * Vector2(100, 0)

Then, even later, you might encounter a mix of GUI and game input which could conflict if Input is used, so you could use _input and _unhandled_input (those are explained in the doc), but before you get there, using Input is probably the simplest way to get started moving things.

What I meant here was launching the game from the editor, which hasn’t worked insofar as movement. Yesterday it was working in the editor itself but not when launching it.

I won’t have to do the simple examples above. I borrowed the line of code for setting motion & speed from your examples above and replaced what I had for two lines in mine, making one a negative integer and for the first time in a week, got the paddle to move! At first the paddle went left and right (and I had this problem at first for a couple days before all this, not realizing the positioning of the numbers between the parentheses mattered, but since then, I learned and know better now. Swapped them to get up/down movement. I changed the figure from 100 to 500. I had to do one more flip-flop. Down was going up and up, down. Working perfectly now in both editor and launched game! Now I have the code for the other paddle as well (with the required modifications.)

I also learnt a lesson about copying and pasting code. Part of my problem wasn’t always code statements as a week ago, I did have movement - they were just going the wrong way and had no idea how to fix that. Copying and pasting was my way of saving time, but that proved false as this strips the tabs and replaces them with character spaces! On top of this, you don’t always get errors and this was throwing me into a perpetual loop until I finally did get an error about expected indentation. More hours of research finally led to figuring out that problem.

I then had the tutorial game “Dodge the Creeps” motion working! Today I rebuilt my PONG clone (a simulation of the 1976 “Atari PONG for Your Home TV” console game,) that I accidentally obliterated by deleting its Root.tscn file. It’s even better now than it was now that I was able to get the shader and Light2D working! I used some of the code from the Dodge game and was dismayed it didn’t work with my PONG game - not until you came along…

I want to thank you for releasing me from game development hell (for a little while anyway!) I had not expected looking at code you provided and plugging it in would make the difference, but it sure did! Why the other code didn’t work, I don’t know, but this was certainly a learning experience! It is incredibly difficult to find answers online but now I’m getting a handle on some of this stuff at this point!

Thanks again!

Euergetes | 2019-03-23 04:52

great answer!
I did also learn a few things along the way, keep helping please !
it saves us a lot of trouble !


Jorge | 2020-07-01 03:45