im new to programming as a whole, and i need to know how to fix this

Godot Version

Godot 4.6.1

Question

so, my code is:

extends CharacterBody2D


const SPEED = 300.0
const JUMP_VELOCITY = -400.0


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta

	# Handle jump.
	if Input.is_action_just_pressed("ui_accept") and is_on_floor():
		velocity.y = JUMP_VELOCITY

	# Get the input direction and handle the movement/deceleration.
	# As good practice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("ui_left", "ui_right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

if direction.x < 0:
	animated_sprite.flip_h = true
elif direction.x > 0:
	animated_sprite.flip_h = false

if velocity.length() > 0.0:
	animated_sprite.play("walk")
else: 
	animated_sprite.play("idle")

  move_and_slide()

and, as soon as i wrote the direction code to flip the sprite, it gave me 3 different errors:

Line 25:Unexpected “if” in class body.
Line 26:Unexpected “Indent” in class body.
Line 26:Expected end of file.

wtf is going on???

any way to fix it?

In GDScript indentation is important. Select your direction code and press tab, so the compiler knows it belongs to the _physics_process function.

ratrogue is right! GDScript is just like Python, as it doesn’t have {}` like JavaScript, Java or C#, indentation is needed in everything.

Just to add a bit more of what I know: every code you write, except for variables, enums, signals, and so on, must be in a function. That’s the meaning of that error lines: the class body itself can’t have functional code, it must be inside functions.

That if statement block you added at the end had the logic of the physics_process function, so it also had to be inside of it.

But for example, if you wanted to write an independent if block, it would still have to be inside a function, then you would call it somewhere.

It was something that really stressed me at the beginning, because I wanted to do just like in Python hahaha

If someone notice I said something wrong in my comment, please, feel free to correct me! I have been for around 4 years in Godot, but I’m still not an expert. Maybe there are ways to right code directly in the class body that I don’t know of :slight_smile:

thanks now it should work!

edit: i tried adding func _physics_process(delta: float) → void:

and it gave an error saying

Function “_physics_process” has the same name as a previously declared function.

and one saying

Line 26:Expected indented block after “if” block.

idk i should just watch back sum tutorials

The error is as it states, you have two func _physics_process’s in your program, this is allowed. You cannot declare a function twice in the same script.

Also as it states the if ... : expects more indentation after it. It’s hard to say exactly what you should do if you don’t share your updated code.


Check out this scripting basics tutorial

i see, im gonna try trough the docs link you sent me, thanks