Godot Version
Godot 4.4.1
Question
Hey everyone. I’m currently trying to make a game, specifically on attacking enemies.
In an earlier posts I asked how to fix three errors:
Line 71:Expected statement, found “Indent” instead.
Line 72:Expected statement, found “Indent” instead.
Line 78:Expected end of file.
This was the code before fixing it:
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()
#Here, are errors 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
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
I fixed the Indent error, but then it said:
Attempt to call function ‘get_overlapping_areas’ in base ‘null instance’ on a null instance.
Apparently, “hurtbox” was null, becasue it didn’t exit in the scene tree (of the Player). So it couldn’t find it. I was following a tutorial and it seems the code was supposed to detect if an enemy overlapped the Player.
So I gave the Player a “hurtbox”, and the error seemingly stopped. But it seems it caused another problem. I had programmed the player to pick up items, but now it couldn’t pick anything up.
Here is the code for the collectible:
extends Area2D
@export var itemRes: InventoryItem
func collect(inventory: Inventory):
inventory.insert(itemRes)
queue_free()
Hope this isn’t too long.