Hello.
My name is Fred. I am working on a small 2d game about collecting stuff. I got some basic coding skills but I am not that good at it and find myself getting stuck from time to time.
The problem:
Player is gonna collect chanterelles. This by pushing a button, in my dualshock4 controller square button, or the key X. I got a simple HUD setup with a Singleton which displays a chanterelle and a number, start from 0. When collecting a chanterelle, it should add 1.

So if I hold the controller square button down, it adds more than 1 to chanterelles.

It adds more than 1 chanterelles when using controller button. Not all the time but still. Any suggestions how to solve this?

/Fred

EDIT: solved, I got some help on the Godot discord.
Just added canBeCollected = false after the Input check and before the playerCollected.chanterelles += 1

code:

extends Area2D

var canBeCollected: = false
onready var animatedSprite: = $AnimatedSprite
onready var playerCollected: = $"/root/PlayerCollectedVariables"

func _ready() -> void:
	animatedSprite.play("default")
	

func _input(event: InputEvent) -> void:
	if canBeCollected && Input.is_action_just_pressed("interact"):
		animatedSprite.play("collected")
		z_index = 100
		playerCollected.chanterelles += 1
		print("chanterelle collected!")
		print(playerCollected.chanterelles)

func _on_Chanterelle_big_1_area_entered(area: Area2D) -> void:
	print("player detected!")
	canBeCollected = true

func _on_Chanterelle_big_1_area_exited(area: Area2D) -> void:
	print("player exited!")
	canBeCollected = false

func _on_AnimatedSprite_animation_finished() -> void:
	if animatedSprite.animation == "collected":
		queue_free()
		print("queue_freed!") 

I assume that script is attached to each chanterelle?

Add a bool variable collected. Initialize it to false. Set it to true when the chanterelle is collected. Check it before collecting the chanterelle.

if not collected and canBeCollected and Input.is_action_just_pressed("interact"):
    ...
    collected = true
    ...

There might also be a structural problem with the code due to indentation. Since the code you posted doesn't show the indentation, I can't determine that.

To post code here correctly, put ~~~ before and after the code.

    DaveTheCoder Ok, so I edited the code snippet, thanks for feedback. The problem is solved, just had to set canBeCollected = false before playerCollected.chanterelles += 1, like so:

    	if canBeCollected && Input.is_action_just_pressed("interact"):
    		canBeCollected = false
    		playerCollected.chanterelles += 1
    		animatedSprite.play("collected")
    		z_index = 100
    FTines changed the title to Just add 1 while collecting *SOLVED* .