0
\$\begingroup\$

When I try to use the GDScript code below, Godot gives me the error:

error(23, 2) Error parsing expression, misplaced: func

Why am I getting this error, and how can I fix it? Is there another input node that could give the same results without the error?

func _ready():
 position = get_viewport_rect().size / 2
 direction.x = rand_range(-1, 1)
 direction.y = rand_range(-1, 1)
 direction = direction.normalized()
 width = get_viewport_rect().size.x
 height = get_viewport_rect().size.y

func _process(delta):
    position += direction * speed * delta
 if position.x < 0 or position.x > width:
  direction.x = -direction.x
 if position.y < 0 or position.y > height:
  direction.y = -direction.y


 func _on_UFO_input_event( viewport, event, shape_idx ):
 if event is InputEvenMouseButton and event.button_index == 
 BUTTON_LEFT and event.pressed:
 direction.x = rand_range(-1, 1)
 direction.y = rand_range(-1, 1)  
 direction = direction.normalized()
 position.x = rand_range(1, width -1)
 position.y = rand_range(1, height -1)
 speed += 5
\$\endgroup\$
1
\$\begingroup\$

the function that i have keeps giving me the error(23, 2) is there another input node that could give the same results without the error?

This looks like symptoms of the XY problem, you have a syntax error, but you try to change the algorithm to avoid it. You'd better ask how to fix the syntax errors.


It looks like you did not format/indent your code properly: the parser thinks you want to add a function (_on_UFO_input_event) to another function (_process), which is not allowed (highlighted by the error message Error parsing expression, misplaced: it does not expect the keyword func in the context of a function). GDScript is a lot like Python, where the indentation of the code has a significant importance.

Following the tips from this official page, you should use four spaces for your indentation.

Your code should end up looking like this.

func _ready():
    position = get_viewport_rect().size / 2
    direction.x = rand_range(-1, 1)
    direction.y = rand_range(-1, 1)
    direction = direction.normalized()
    width = get_viewport_rect().size.x
    height = get_viewport_rect().size.y

func _process(delta):
    position += direction * speed * delta
    if position.x < 0 or position.x > width:
        direction.x = -direction.x
    if position.y < 0 or position.y > height:
        direction.y = -direction.y

func _on_UFO_input_event( viewport, event, shape_idx ):
    if event is InputEvenMouseButton and event.button_index == BUTTON_LEFT and event.pressed:
        direction.x = rand_range(-1, 1)
        direction.y = rand_range(-1, 1)  
        direction = direction.normalized()
        position.x = rand_range(1, width -1)
        position.y = rand_range(1, height -1)
        speed += 5

Correctly formatting your code is required in a language such as GDScript. And it is very important with other languages in general: it allows you to read and parse what's going on faster and more easily.

In addition, the (23,2) is not a pair of random numbers, it tells you where the issue is in your code. It tells you "at this location, I have found this error". Most IDEs allow you to double-click on the error and take you right to there in your code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.