Input not working no matter how many times i try (Simple question please help)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By cyrus8088
:warning: Old Version Published before Godot 3 was released.

I looked around for a question similar to mine, i just couldn’t find it, so i’m making my own post
(Also i’m relatively new to coding so this is a fairly simple script)

extends Area2D

export (int) var SPEED
var velocity = Vector2()
var screensize
var position = 1
var input = 1

func _ready():
    screensize = get_viewport_rect().size
    
func _process(delta):
    _ready(set_process_input(true))
    velocity = Vector2()
    if Input.is_action_pressed("ui_right"):
        velocity.x += 1
    if Input.is_action_pressed("ui_left"):
        velocity.x -= 1
    if Input.is_action_pressed("ui_down"):
        velocity.y += 1
    if Input.is_action_pressed("ui_up"):
        velocity.y -= 1
    if velocity.length() > 0:
        velocity = velocity.normalized() * SPEED
    
    position += velocity * delta
    position.x = clamp(position.x, 0, screensize.x)
    position.y = clamp(position.y, 0, screensize.y)

That is my code for the 2D Character’s movement on a flat surface.
No matter how many things i change, i just can’t get it to move no matter what input i press, please help me.

Are you setting the SPEED variable in the editor to a value?

literalcitrus | 2017-12-18 03:33

Yes, i set it to “400”

It’s still not working
:confused:

cyrus8088 | 2017-12-18 08:53

:bust_in_silhouette: Reply From: Wender Alves Libório

the code is a bit unorganized
there are lot of typo errors, are you following the first steps tutorial?
here is the reference http://docs.godotengine.org/en/latest/learning/step_by_step/your_first_game.html

extends Area2D

export (int) var SPEED
var velocity = Vector2()
var screensize

func _ready():
	screensize = get_viewport_rect().size
			
func _process(delta):
	velocity = Vector2()
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1

	if velocity.length() > 0:
		velocity = velocity.normalized() * SPEED
		
	position += velocity * delta
	position.x = clamp(position.x, 0, screensize.x)
	position.y = clamp(position.y, 0, screensize.y)

Sorry buddy, it’s not working
I used your exact script but it gives me an error on the

position += velocity * delta

line, “Identifier not found position” ideally, i figured i needed to make a “var position” so i made the line of code above

var position

But nothing happened, it’s not moving, what do i do?

cyrus8088 | 2017-12-18 08:53

position is a member of your Area2D and should not be declared as variable. If you got this error your code seems to be at the wrong place.

It woks fine on my side. Here is the sample project I created in Godot 3 with the exact same code: Zippyshare.com - [now defunct] Free File Hosting

rolfpancake | 2017-12-18 14:34

:bust_in_silhouette: Reply From: plavius

Not sure if you already solved since there are some months you posted, but I had the same issue today, and may help others.

Solved by changing the last two lines to: (and don’t forget to indent)

position.x = clamp(position.x, 0, get_viewport_rect().size.x)
position.y = clamp(position.y, 0, get_viewport_rect().size.y)

It seems that getting viewport size at ready funtion, was returning a 0, 0 vector.
This way it updates every frame. Not sure if it’s useful for you, but one good thing is that you can change the size of the screen during the game.

another issue at least form me, was that I needed to declare “screensize” as Vector2 in order to get the size. Anyway it returned 0, 0.

sir you just saved an hour

abecus | 2020-01-25 20:00