Expected Statement, found "Indent" instead

Godot Version

Godot 4.4.1

Question

Hello, I’m trying to make a game and am currently dealing with attacks. However, I’ve come across an error.

These are the errors:

Line 71:Expected statement, found “Indent” instead.
Line 72:Expected statement, found “Indent” instead.
Line 78:Expected end of file.

extends CharacterBody2D

class_name Player

signal healthChanged



var screen_size

var lastAnimDirection: String = "down"
var ishurt: bool = false
var isAttacking: bool = false


@onready var animations = $AnimationPlayer

@export var speed: int = 50
@export var maxhealth = 3
@export var knockbackPower: int = 500
@export var inventory: Inventory

@onready var currentHealth: int = maxhealth
@onready var effects = $Effects
@onready var hurttimer = $hurttimer
@onready var hurtColor = $Sprite2D/ColorRect
@onready var hurtbox = $hurtbox
@onready var weapon = $weapon

@onready var sprite = $Sprite2D
@onready var heartsContainer = $CanvasLayer/heartscontainer


func _ready():
	effects.play("RESET")


func handleInput():
	var moveDirection = Input.get_vector("ui_left","ui_right", "ui_up", "ui_down")
	velocity = moveDirection*speed

	if Input.is_action_just_pressed("attack"):
		animations.play("attack" + lastAnimDirection)
		isAttacking = true
		await animations.animation_finished
		isAttacking = false
		

func updateAnimation():
	if isAttacking: return
	
	if velocity.length() == 0:
		if animations.is_playing():
			animations.stop()
	else:
		var direction = "down"
		if velocity.x < 0: direction = "left"
		elif velocity.x > 0: direction = "right"
		elif velocity.y < 0: direction = "up"
	
		animations.play("walk" + direction)
		lastAnimDirection = direction


func _physics_process(delta):
	handleInput()
	move_and_slide()
	updateAnimation()


		if !ishurt:
			for area in hurtbox.get_overlapping_areas():
				if area.name == "hitbox":
					HurtByEnemy(area)
	
	

func HurtByEnemy(area):
	currentHealth -= 1 
	if currentHealth < 0:
		currentHealth = maxhealth
			
	healthChanged.emit(currentHealth)
	ishurt = true
		
	knockback(area.get_parent().velocity)
	effects.play("Blink")
	hurttimer.start()
	await hurttimer.timeout
	effects.play("RESET")
	ishurt = false


func _on_hurtbox_area_entered(area: Area2D):
	if area.has_method("collect"):
		area.collect(inventory)

func knockback(enemyVelocity):
	var knockbackDirection = (enemyVelocity - velocity).normalized() * knockbackPower
	velocity = knockbackDirection
	print_debug(velocity)
	print_debug(position)
	move_and_slide()
	print_debug(position)
	print_debug(" ")
	


func _on_hurtbox_area_exited(area: Area2D): pass

The errors are in this specific parts:

func _physics_process(delta):
	handleInput()
	move_and_slide()
	updateAnimation()

#Here, are 71 and 73
		if !ishurt:
		  for area in hurtbox.get_overlapping_areas():
				if area.name == "hitbox":
					HurtByEnemy(area)
	

#And here is 78
func HurtByEnemy(area):
	currentHealth -= 1 
	if currentHealth < 0:
		currentHealth = maxhealth
			
	healthChanged.emit(currentHealth)
	ishurt = true
		
	knockback(area.get_parent().velocity)
	effects.play("Blink")
	hurttimer.start()
	await hurttimer.timeout
	effects.play("RESET")
	ishurt = false

What could be done?

indentation is the tab before the code. Godot uses indentation as part of control flow, your if statement should flow with the other statements above. After updateAnimation the if !ishurt: should run, but by indenting you’ve declared it should be run as part of another scope that doesn’t exist. Indentation should only increase after certain keywords, if, func, for, and while are a few that come to mind.

I managed to fix the “Indent” error. But now it says:

Attempt to call function ‘get_overlapping_areas’ in base ‘null instance’ on a null instance.

Your hurtbox is null, it either doesn’t exist as a child in the scene tree (you are looking for $hurtbox which may not exist), or you’ve deleted it mid-game.

Hhhhmm, I have a “hurtbox” in the scene tree of the “enemy”, but not on the Player… what could be done to fix it?

Yes the $ notation only checks for children of the scripted node, if your player doesn’t have a node named “hurtbox” then it won’t find it, if you want it to find the enemies hurtbox then you will have to do something else.

It seems like this code is supposed to use the player’s “hurtbox” to detect if an enemy (or any “hitbox”) is overlapping the player, then calling the hurt by enemy function. You probably do want to give your player a “hurtbox”. Are you following a tutorial?

1 Like