Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | rubendw03 |
So I have made a little game in wich the player sends a signal when he gets infected. The problem is that most of the time he doesn’t send the signal although he changes color correctly every time.
This is the script for the player to change color and emit the signal.
onready var infected_sprite = preload("res://Sprites/CT_InfHuman.png")
var rng = RandomNumberGenerator.new()
export var immunity_rate = 0
signal human_infected
func _on_Human_body_entered(body):
rng.randomize()
var immunity = rng.randi_range(1, immunity_rate)
if immunity <= (immunity_rate - 1):
if body.get_name() == "Virus" or body.is_in_group("InfectedHumans"):
var mysprite = $HumanSprite
mysprite.set_texture(infected_sprite)
get_node(".").add_to_group("InfectedHumans")
print("infection")
emit_signal("human_infected")
And this is the main game script wich should receive the signal.
func _on_Human_human_infected():
print("infection")
infected_humans += 1
If it does change color, I guess it also prints?
If it also prints properly, then your node does emit the signal, as your code does.
It’s then more likely that your main game script is not correctly connected instead.
If your code makes it change color but doesn’t print, you may have a look at the console to check if any error occurred that would prevent the rest of the code to run?
Zylann | 2020-03-23 14:01
No errors occurred (apart from “set_bounce” and “set_friction” “have been deprecated and will be removed in the future”
rubendw03 | 2020-03-23 14:36
most of the time he doesn’t send the signal although he changes color correctly every time
Judging from the code you provided that shouldn’t happen! I tried to reproduce it locally, but couldn’t. Maybe you can provide an example project?
A few unrelated remarks:
rng.randomize()
only has to be called once (usually in_ready()
)if immunity <= (immunity_rate - 1)
is the same condition asif immunity != immunity_rate
. Is that really what you wanted to do? Honestly, I don’t really get what you’re trying to do with that. Care to explain?- It’ s not necessary to use
get_node
to get the current node. Instead of usingget_node(".").add_to_group("InfectedHumans")
you can simply writeadd_to_group("InfectedHumans")
. If you need to explicitly declare a node somewhere, e.g. when connecting a signal from code, you can just useself
.
njamster | 2020-03-23 14:43
Honestly, I don’t really get what you’re trying to do with that. Care to explain?
immunity_rate is a variable wich defines when a human gets infected or not, so if i set it to 3 it means that 1 out of 3 people will be immune. That is why i subtract it by 1 so there is always that 1 out of x chance of being immune.
Maybe you can provide an example project?
How??
Thanks for the remarks!
rubendw03 | 2020-03-23 14:54