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?
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â. 
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?
Oh yes youâre totally right! Sorry for missinformation, i just got used to different code editors
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.
The code is as it appeared in my question.
How do I put a print statement?
(Sorry if Iâm asking for too much.)
Change this:
To this:
func _on_hurtbox_area_entered(area: Area2D):
print("Area Detected")
if area.has_method("collect"):
print("Area Has collect Method")
area.collect(inventory)
I changed it, but there doesnât seem to be any difference.
Well you did that to debug it. There should be no difference. You asked how to add print statements.
Now, run the game, and see what prints out in the Output window. Nothing? The first one? Both?
I just tried it, but got no output related to collecting or inventory.
Great. So that tells us that the Hurtbox Area2D is not detecting the other Area2D. So the next thing to check is that the Hurtbox Area2D has a physics mask on the same physics layer that the Item Area2D is on.
I checked, but it seems to be on the same layer.
Ok, just so weâre clear, you are using the hurtbox to pick up items?