Enemy from a spawner emit signal only once

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By dany_pitre

Hi ! I’ve been trying to make an enemy spawner and have a big issue.

The way my system works is that if the player is the same type as my enemy, it can youch it and it will increase his score. If it’s not the same type, it will kill him. Each time the player enters the area of the enemy, the enemy emits a signal that will tell the player to check if it’s the same type and do the action to increase score or kill him.

the problem that I have is that if the player reach the score of 1, my player can touch any enemy and it will not increase the score or kill the player, but only queue_free the enemy… i’ve been looking to fix this bug for a few hours and dont have any idea of the issue since i’m new to godot and coding in general.

Here is my code for the player’s scipt:

func _on_0_white_check() → void:
if type == 0:
score += 1
print(“points” + str(score))
else:
die()

func _on_0_red_check() → void:
if type == 1:
score += 1
print(“points” + str(score))
else:
die()

func _on_0_yellow_check() → void:
if type == 2:
score += 1
print(“points” + str(score))
else:
die()

func _on_0_blue_check() → void:
if type == 3:
score += 1
print(“points” + str(score))
else:
die()

Here is my code in my enemy’s script:

func _on_Area2D_body_entered(body: Node) → void:
if type == 0:
emit_signal(“white_check”)
elif type == 1:
emit_signal(“red_check”)
elif type == 2:
emit_signal(“yellow_check”)
elif type == 3:
emit_signal(“blue_check”)
queue_free()

If someone has an idea… please help me ! Thanks a lot :slight_smile:

:bust_in_silhouette: Reply From: scrubswithnosleeves

You should definitely only need one signal for this instead of 4:

signal check_color(color)

func _on_Area2D_bodyentered(body: Node) -> void:
     emit_signal("check_color", type)

In this code, we define the signal to take 1 parameter/argument color. Then we send that argument in the signal.

Then in your player script, you would do:

func _on_check_color(color):
     if color == type:
          score += 1
     else:
          die()

See, way less code! Try that and see if that fixes your issue.

Where are you connecting your signals? Have you tried putting print() functions in your _on_colorcheck() functions to see if they are being triggered?

Also, you should really format the code in your post like I did here. People will be more likely to help you. At the top of the text box where you are typing your post, you should see a little icon that looks like this {}. Highlight any section of your post that contains code and then click that icon to format it as code.

Thanks a lot for your answer, it helped !

I’m not quite sure to understand how to use it properly.

So i’m creating a signal that is called check_color(color) right?
but where do I declare “color” in my player script?

thanks a lot for the tip of the script !

dany_pitre | 2021-05-03 22:38

Im gunna go live on YouTube in like 20 minutes just to answer questions solve peoples problems, so I’ll let you know here when I go live and then I’ll do this all on stream.

scrubswithnosleeves | 2021-05-03 22:44

That’s really great, thanks !

dany_pitre | 2021-05-03 22:45