Problem about colleting items

Godot Version

Godot 4.4.1

Question

Hello, I've been trying to make a game. However, I've been having issues with collecting items. The player is supposed to be able to collect designated items, but with a recent fix I did in the code, the player can no longer pick up items. What could be happening? How can I fix it?

Here is the code for the player:

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

And here’s the code for the item:

extends Area2D

@export var itemRes: InventoryItem

func collect(inventory: Inventory):
inventory.insert(itemRes)
queue_free()


Is the signal still connected? Hooking them up in the editor can be flakey.
Stick a print() in _on_hurtbox_area_entered(), is it triggered?

Try to disconnect the signal and connect it again, maybe something is wrong there. And idk if this is my display bug but your code inside collect() is not indented

Stick a print(), how exactly?

How do I indent it?

Just use Tab or press space 4 times.
Here is what i mean

func foo():
    print("foo") #This line is indented

‘Tab’ is a shortened version of ‘Tabulation’, which was a mechanical system on typewriters (remember typewriters..?) to help with the alignment of text or numbers, to create Tables. The system allowed for aligning text to the left, or numbers to the right, or lined up with the decimal sign. This made the creation of tables for adding up much easier to read.
On our modern software keyboards, for programming, we have little use for these niceties, but it can be useful to know where the terms come from, just the same. Similarly, the key ‘Alt’ derives from the need, again on a typewriter system, to have an ‘Alternative’ set of letters, ‘Ctrl’ stems from ‘Control’, which allowed for special ‘‘Control’ codes to be used. ‘Shift’ was a mechanical means of changing the whole set of keys to type in upper case. Typically the whole paper-bearing roller chariot would be ‘shifted’ up, so that another character would strike the ribbon and print the upper-case letter. This could be tiring to do, for a long series of characters, so a ‘Caps Lock’ key would hold the chariot in this ‘shifted’ position, allowing the release of the ‘Shift’ key. ‘Caps’ is a term for upper case (Capital letters…), so ‘Caps Lock’ meant locking the keys into typing only Capital (upper-case…) letters.
Just sayin’. :slight_smile:

1 Like

Do not press space 4 times. Use the Tab key. Godot uses tabs by default, and while it can parse spaces, it insists on you using one or the other or it throws an unhelpful error on the next line. So you would have to remove all the tabs in your code and uses spaces instead. That’s a nightmare. Use all Tabs.

What was the recent fix?

1 Like

Oh yes you’re totally right! Sorry for missinformation, i just got used to different code editors

1 Like

I delt with an earlier “indent” error, which said “Expected statement, found “Indent” instead”.

Related to a “hurtbox”, it was supposed to work in attacking enemies.

The “hurtbox” wasn’t in the Player scene tree originally, so it was null. I later wrote it in, but now the player can’t collect anything.

Sorry if this is too complicated.

It’s just unclear what the code looks like now.

If it’s not collecting, put a print statement in to see if the body_entered signal is firing.