KEY_UP and KEY_LEFT not recognised?

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

Hello guys. It’s my first time writing on this forum. I recently discovered Godot and i’m following a tutorial to learn basic things. I have some several problems. This is my code:

extends Sprite

func _ready():


func _process(delta):

var velocity = Vector2()

if Input.is_key_pressed(KEY_UP):
	velocity.y = -5

if Input.is_key_pressed(KEY_DOWN):
	velocity.y = 5

if Input.is_key_pressed(KEY_LEFT):
	velocity.x = -5

if Input.is_key_pressed(KEY_RIGHT):
	velocity.x = 5

	self.position = self.position + velocity.normalized()*5*delta

And the sprite moves only if i press down+right, it will move diagonally . And then i can move the sprite also in down direction. If i write this code:

func _process(_delta):

var velocita = Vector2()

var deltaFrame = _delta * 60

if Input.is_key_pressed(KEY_W):
	velocita.y = -1

    if Input.is_key_pressed(KEY_S):
	velocita.y = 1
    if Input.is_key_pressed(KEY_A):
	velocita.x = -1

    if Input.is_key_pressed(KEY_D):
	velocita.x = 1

    self.position += velocita * 5 * deltaFrame

The sprite moves correctly . But i’d like to use arrow keys on the keyboard. Also this code managed to solve the errors I will explain down low.

Another problem is this:

Any code I copy or write I always get errors like “misplaced func” or “expected intended block” or “identifier input isn’t declared in this scope”. There are no grammatical errors. I tried to copy the code point by point both as written by Crystal Bit and by LordXenon. It’s frustrating because I can’t go on with the tutorial and anything I do always gives me error. I also tried various combinations of tabs, spaces and enter but nothing changes. I specify that I am using the latest version of Godot 3.2.1

PS: I tried again to copy the code written by Crystal Bit, and I found that if I press enter, between “func _ready” and “func _process”, it returns an “expected intended block” error, if I press tab on “func _process” it returns an “misplaced func” error . I solved it by using the word “PASS” under “func _ready” and pressing tab. While as regards the syntax, if I write “func _process (delta):” it returns me wrong, forcing me to write “(_delta):” or “(_event):” or “(_process)” for example, all of these with the underscore before the word.

Now I don’t know if this procedure is a bit forced or something has actually changed in the new versions of Godot. If anyone can enlighten me, it would do me a great favor.

:bust_in_silhouette: Reply From: jgodfrey

It looks like you have some indent-related issues in your code. In GDScript, (like Python), code blocks are determined by indent levels. In the first block of code you posted, the the self.position = ... code will only run when you press the right key, because it’s indented at a level that makes it belong to that block. To make that code run independently from the individual key presses, (which you want), you need to change the indent on that line so that it lines up with if Input... lines. So, this…

if Input.is_key_pressed(KEY_RIGHT):
    velocity.x = 5

self.position = self.position + velocity.normalized()*5*delta

Also, your second block of posted code has indent-related issues. Bottom line, the way you indent your code will impact how it executes, so be very careful about that.

Also, be careful when you cut/paste code from other sources as it may cause similar indent issues. Also, don’t mix the use of tab and space for indents as that’ll cause similar issues…