How to check if coin has been destroyed by player and then add to score?

Godot Version

4.2.1

Question

Hello, I am currently trying to make an infinite runner in Godot. I want to make it so that when my player touches and destroys (Queue_free) a coin it will add +1 to the score.

However, my player has its’s own script that handles the collisions and the stuff regarding the score is in my world script. I cannot figure out the proper coding to check if a coin has been destroyed by the player. I know how to add to the score, but I can’t figure out what line of coding is needed to check if an object in a group was destroyed by the player.

So what kind of code would I need if I want to check if an object has been destroyed and then add +1 to the score?

I’m new here so if I posted in the wrong area, please let me know where I can go to get the help I need.

are you wanting code to run on the coin when it is destroyed, and run custom code if that coin is destroyed by a player?

I’m not sure, I’m still learning about Godot?

What need is something that I can put in my world script to check if the player has destroyed a coin and then add 1 to the score.

Basically, how can I make an if statement that checks if the coin has been destroyed? I just don’t know what kind of coding I need to use

Oh I see what you mean. You said you have your collision set up, right? If the collision already works, you can just += 1 to your score variable. I can help you with collision if you don’t have that working yet.

I have a similar mechanic in my game I’m developing for Experience. here’s what I did:

In the collectible script, add it to a group (mine is experience) and have this script:

extends Area2D

@export var value = 5


func Collect():
	print(value)
	queue_free()

Then, in the player script, connect a signal to the on_area_entered_2D and connect it to your player script. It should then look something like this:

func _on_area_2d_area_entered(body: Area2D) -> void:
	if body.is_in_group("experience"):
		make_experience_update(body.value)
		body.Collect()

where i have a function that I garnered from This tutorial on YoutTube

hope this helps!

1 Like

I set my score as a global variable - makes it easy to access from anywhere in your code.

My code - end_level() is a check to see if all coins have been collected.

func _on_body_entered(body):
	if body.name == ("player"):
		print("COIN")
		global.score += 10
		$AnimatedSprite2D.visible = false
		$CollisionShape2D.set_deferred("disabled",true)
		end_level()

FYI Brackeys has a good beginner tutorial that does exactly this

2 Likes

Thank you all so much for the help! :slight_smile:

The video really helped a ton. What I ended up doing was making a gameplay node and putting the world script into there. That way I could access it from my player script where the collisions happen and have the score be added there using the add_point() function in the gameplay script.

Thank you all again so much for your help and support! :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.