0 votes

Error parsing expression, misplaced: else

extends Area2D

export var speed = 400
var screen_size

func _ready():
    screen_size = get_viewport_rect().size

func _process(delta):
    var velocity = Vector2()  # The player's movement vector.
    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
        $AnimatedSprite.play()
    position += velocity * delta
    position.x = clamp(position.x, 0, screen_size.x)
    position.y = clamp(position.y, 0, screen_size.y)
    else:
    $AnimatedSprite.stop()
in Engine by (12 points)
edited by

Your screenshot doesn't work because apparently you linked to the file on your computer. People on the internet can't (for obvious reasons) access your computer.

Rather than posting a screenshot, please post the text of the error message and copy and paste the script that is causing it. Note that when you post code here on this forum, there's a "Code Sample" button just above that will ensure the code is formatted correctly.

1 Answer

+1 vote

If you highlight your code and click the curly braces symbol, it'll format your code in a readable way.

I assume the issue is that you have something like this:

if velocity.length() > 0:
    velocity = velocity.normalized() * speed
    $AnimatedSprite.play()
    position += velocity * delta
    position.x = clamp(position.x, 0, screensize.x)
    position.y = clamp(position.y, 0, screen_size.y)
    else: # incorrectly tabbed
    $AnimatedSprite.stop()

Instead of the correct:

if velocity.length() > 0:
    velocity = velocity.normalized() * speed
    $AnimatedSprite.play()
    position += velocity * delta
    position.x = clamp(position.x, 0, screen_size.x)
    position.y = clamp(position.y, 0, screen_size.y)
else:
    $AnimatedSprite.stop()

It's hard to be sure exactly what's wrong because the tabbing wasn't preserved but it'll be something along those lines.

by (1,473 points)

The else needs to be in line horizontally with its corresponding if.

func _process(delta):
    ...
    if velocity.length() > 0:
        velocity = velocity.normalized() * speed
        ...
    else:
        $AnimatedSprite.stop()

(it could be that he has it unindented like func)

Thank you both, Looking now, I do believe it to be a indenting issue. I'm still very new to all of this. It's literally a foreign language to me.

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 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 webmaster@godotengine.org with your username.