Parser error: Unexpected "if" in class body

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("left", "right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)

if direction > 0:
animated_sprite,flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# Apply Movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()

move_and_slide()

Wrap your code in triple back-ticks ```[code]``` and tell us which line the error is in.

Line 6:Unknown character “→”.Line 6:Expected “:” after function declaration.Line 6:Expected statement, found “void” instead.Line 8:Unexpected “if” in class body.Line 9:Unexpected “Identifier” in class body


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("left", "right")
if direction:
	velocity.x = direction * SPEED
else:
	velocity.x = move_toward(velocity.x, 0, SPEED)
if direction > 0:
animated_sprite,flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# Apply Movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()```

You need to indent the code inside your functions with a tab.

1 Like

When I indent it says Line 4:Unexpected “Indent” in class body. with some more errors

Paste the code here again, but use preformatted text functionality please wrapping your code with ```
And please list all errors, we can’t help you if we don’t know what you’re dealing with.

Your code has a bunch of indentation issues:

  1. DO NOT indent your _physics_process() function line. You didn’t, so I’m just reiterating.
  2. DO indent the contents of your _physics_process() function.
  3. DO NOT indent your conditional statements beyond 1 tab (in this case). This includes the else.
  4. DO indent the contents of your conditionals 1 tab beyond the conditional indentation.

The errors Line 4:Unknown character “→”.Line 4:Expected “:” after function declaration.Line 4:Expected statement, found “void” instead.Line 6:Unexpected “if” in class body.Line 7:Unexpected “Identifier” in class body.Line 10:Unexpected “Indent” in class body.Line 11:Unexpected “Identifier” in class body.Line 15:Expected end of file.

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 pactice, you should replace UI actions with custom gameplay actions.
var direction := Input.get_axis("left", "right")
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
if direction > 0:
animated_sprite,flip_h = false
elif direction < 0:
animated_sprite.flip_h = true
# Apply Movement
if direction:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()```

You haven’t indented almost any of your code.
Please study this article about how GDscript code is supposed to look like, then correct yours with proper indentation.

const SPEED = 300.0
const JUMP_VELOCITY = -400.0

# 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 pactice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("left", "right")
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)
		if direction > 0:			
			animated_sprite,flip_h = false
		elif direction < 0:
			animated_sprite.flip_h = true
		# Apply Movement
		if direction:
			velocity.x = direction * SPEED
		else:
			velocity.x = move_toward(velocity.x, 0, SPEED)

move_and_slide()```

I idented however the errors still appear the code is from a youtube video which is supposed to animate the player is idle running and jumping

Line 4:Unknown character “→”.Line 4:Expected “:” after function declaration.Line 4:Expected statement, found “void” instead.Line 6:Unexpected “Indent” in class body.Line 7:Unexpected “Identifier” in class body.Line 10:Unexpected “if” in class body.Line 11:Unexpected “Identifier” in class body.Line 16:Unexpected “Indent” in class body.Line 17:Unexpected “Indent” in class body.Line 18:Expected end of file.

You’re still not indenting it correctly :slight_smile: and now you don’t even have any function declaration. See below how it should look like, at least I think from the scraps of information I have, and assuming the code itself is correct.

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 pactice, you should replace UI actions with custom gameplay actions.
	var direction := Input.get_axis("left", "right")
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
	if direction > 0:			
		animated_sprite,flip_h = false
	elif direction < 0:
		animated_sprite.flip_h = true
	# Apply Movement
	if direction:
		velocity.x = direction * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

Thank you the indenting you did is correct however is still pops up with some error messages

Line 7:Mixed use of tabs and spaces for indentation.Line 11:Mixed use of tabs and spaces for indentation.Line 30:Unindent doesn’t match the previous indentation level.Line 30:Mixed use of tabs and spaces for indentation.Line 32:Expected end of file.

As the error says - you can’t mix spaces and tabs for indentation, so change all spaces in the indentations for tabs. I’m on mobile so I can’t see which are spaces and which tabs, but in the Godot Editor you should see the difference.

I just got it down to one error now it just says Line 11:Mixed use of tabs and spaces for indentation.

Okay I just did what you said and it worked thank you

1 Like