Hi,
Newcomer to Godot here as the question will shortly imply. I'm following the documentation and just started the "my first game" section and ran into an issue. I moved the whole input logic inside a separate function _setup_input(velocity) which changes the x and y positions based on key presses and called it inside _process function just so it would look a little cleaner than the example and the character is not moving. Once I move the logic back to the _process function it works and I can't understand why.
func _ready():
screen_size = get_viewport_rect().size
func _process(delta):
var velocity = Vector2() # the player's movement velocity
setup_input(velocity)
if velocity.length() > 0:
velocity.normalized() * SPEED
$AnimatedSprite.play() # $ returns the node at the relative path from this node, shorthand for get_node()
else:
$AnimatedSprite.stop()
position += velocity * delta
position.x = clamp(position.x, 0, screen_size.x)
position.y = clamp(position.y, 0, screen_size.y)
func setup_input(velocity):
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_up"):
velocity.y += 1
if Input.is_action_pressed("ui_down"):
velocity.y -= 1