0
\$\begingroup\$

I have been trying to follow Code with Tom's fps tutorial and I encountered this error on line 27. I don't know how to fix it and everything I've tried has not worked.

extends KinematicBody

export var speed = 10
export var acceleration = 5
export var gravity = 0.98
export var jump_power = 3
export var mouse_sensitivity = 0.3

onready var head = get_node("head")
onready var camera =get_node("head/camera")

var velocity = vector3()
var camera_x_rotation = 0

func _ready():
    input.set_mouse_mode(input.mouse_mode_captured)

func _input(event):
    if event is inputeventmousemotion:
        head.rotate_y(deg2rad(-event.relative.x * 0.3))
        
        var x_delta = event.relative.y *mouse_sensitivity
        if camera_x_rotation + x_delta > -90 and camera_x_rotation + x_delta < 90:
            camera.rotate_x(deg2rad(-x_delta))
            camera_x_rotation += x_delta
            
            func _process(delta)
            if input.is_action_just_pressed("ui_cancel"):
                Input.set_mouse_mode_visible
                
                func _physics_process(delta)
                var head_basis = head.get_global_transform().basis
                
                var direction = vector3()
                if input.is_action_pressed("move_forward"):
                    direction -= head_basis.z
                    elif input.is_action_pressed("move_backward")
                    direction += head.basis.z
                    
                    if input.is_action_pressed("move_left")
                    direction -= head_basis.z
                    elif input.is_action_pressed("move_right")
                    direction += head.basis.x
                    
                    direction = direction.normalized()
                    
                    velocity = velocity.linear_interpolation(direction * speed, acceleration *delta)
                    velocity.y -= gravity
                    
                    if input.is_action_just_pressed("jump") and is_on_floor():
                        velocity.y += jump_power
                        
                        velocity = move_and_slide(velocity, vector3.up)

Godot marks error on the line that says func _process(delta).

\$\endgroup\$
1
  • \$\begingroup\$ Please post code as text, not as a screenshot. It's much easier to troubleshoot code in its native form which in turn makes it more likely to get answers / help. Including tags for the language &/or links to the tutorial would also help. \$\endgroup\$
    – Pikalek
    Jul 17, 2022 at 3:45

1 Answer 1

0
\$\begingroup\$

In GDScript, as you would know, nesting is whitespace based. So what you are doing is putting a method inside another. You can just not. Put the method outside (i.e. without indentation).

In fact, given that the method in question is called _process I presume you intend to override the virtual method _process. So what you have to do is precisely that: have no indentation (no white space before the code, so it is at the start of the line), so it is not inside the other method.


Addendum: After transcribing the code, I have noticed it has more errors of the same kind (and some capitalization errors too). However, it happens that Godot 3.x first checks for the indentations, and the parser only reports the first error.

Thus, I want to explain the indentation a little better. For example, in this code:

if input.is_action_pressed("move_forward"):
    direction -= head_basis.z
    elif input.is_action_pressed("move_backward")
    direction += head.basis.z

    if input.is_action_pressed("move_left")
    direction -= head_basis.z
    elif input.is_action_pressed("move_right")
    direction += head.basis.x

You need : after the conditions. This indicates to Godot that it is the start of a block:

if input.is_action_pressed("move_forward"):
    direction -= head_basis.z
    elif input.is_action_pressed("move_backward"):
    direction += head.basis.z

    if input.is_action_pressed("move_left"):
    direction -= head_basis.z
    elif input.is_action_pressed("move_right"):
    direction += head.basis.x

But that is not yet correct. Where does the block end? Currently, Godot would think that everything is inside the first if. And would also complain that the other blocks don't have anything inside. That is because you indicate what is inside with white spaces, like this:

if input.is_action_pressed("move_forward"):
    direction -= head_basis.z
elif input.is_action_pressed("move_backward"):
    direction += head.basis.z

if input.is_action_pressed("move_left"):
    direction -= head_basis.z
elif input.is_action_pressed("move_right"):
    direction += head.basis.x

To reiterate: whitespace is important in GDScript. I hope that makes sense. And yes, the same issue happens with methods and other statements.

You will see the capitalization errors once you fix the erros of this kind. I suppose you will be able to fix those with help of autocomplete.


If you did actually intend to put a method inside another, know that Godot 3.x does not support nested methods. In that case, you might be interested in Godot 4.0, currently in Alpha at the time of writing, which has method expressions. See GDScript progress report: Feature-complete for 4.0.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged .