Multiple of the same node. How to send same signal?

Godot Version

4.3

Question

image
How would i connect both coin and Coin2(Which are just copys of each other) to the exit node. Currently it would do stuff like this:

func _on_coin_coin_pickup() -> void:
	 # Replace with function body.


func _on_coin_2_coin_pickup() -> void:
	pass # Replace 

I want it to do this:

func _on_coin_coin_pickup() -> void:
	 # Replace with function body

The goal would be that once you pick up all of the coins you can exit. So adding another coin to the scene, would require you too pick that one up as well.

You don’t need to connect the signal to the function Godot wants to generate. Instead, select the existing _on_coin_coin_pickup using the Pick button.

Something like this:

2 Likes

@FencerDevLog mentiond the correct solution for your question about the signals.

If you also want to trigger the “exit” method only when all of the coins are picked up, then you need to additionally keep track of how many coins are in the scene, vs how many coins have been picked up. When one number matches the other, the exit method triggers.
Sorry I can’t provide you with specific code, but I’m on mobile. If you need any further help, let me know.

I currently just do a

coin = 0
	for i  in coins.get_children():
		coin += 1

If thats the best way of doing it. :sweat_smile: Have no idea of how to keep track of that number, so each time you pick up a coin it does the calculations. I am new to godot(only about 6 days in.)

Yes, great. So now the coins variable shows you the total amount of coins player has to collect.
Now, create a second variable called coins collected and increment it within your _on_coin_pickup() method that is triggered by a signal. Then right after that, check if coins_collected is equal to coins, and if yes - then do your exit function, if not then do nothing.

var coins_total: int = 0
var coins_collected: int = 0

func _ready() -> void:
    for coin: Node in coins.get_children():
        coins_total += 1


func _on_coin_pickup() -> void:
    coins_collected += 1
    if coins_collected == coins_total:
        exit() ## or whatever your function is