0 votes

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 on0whitecheck() -> void:
if type == 0:
score += 1
print("points" + str(score))
else:
die()

func on0redcheck() -> void:
if type == 1:
score += 1
print("points" + str(score))
else:
die()

func on0yellowcheck() -> void:
if type == 2:
score += 1
print("points" + str(score))
else:
die()

func on0bluecheck() -> void:
if type == 3:
score += 1
print("points" + str(score))
else:
die()

Here is my code in my enemy's script:

func onArea2Dbodyentered(body: Node) -> void:
if type == 0:
emitsignal("whitecheck")
elif type == 1:
emitsignal("redcheck")
elif type == 2:
emitsignal("yellowcheck")
elif type == 3:
emitsignal("bluecheck")
queue_free()

If someone has an idea... please help me ! Thanks a lot :)

Godot version 3.3
in Engine by (47 points)

1 Answer

+1 vote

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.

by (608 points)

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 !

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.

That's really great, thanks !

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.