Why is my signal not always emitting?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: 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 as if 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? :slight_smile:
  • It’ s not necessary to use get_node to get the current node. Instead of using get_node(".").add_to_group("InfectedHumans") you can simply write add_to_group("InfectedHumans"). If you need to explicitly declare a node somewhere, e.g. when connecting a signal from code, you can just use self.

njamster | 2020-03-23 14:43

Honestly, I don’t really get what you’re trying to do with that. Care to explain? :slight_smile:

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

:bust_in_silhouette: Reply From: njamster

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

Alright. However, an immune human should be safe from the virus no matter what, isn’t it? In your implementation you’re throwing a dice each time they get in contact with the virus. Given enough time/dice rolls, everyone will become infected regardless of the rate!

How [to provide an example project]??

Create a new project which contains your problem (or use your full project, if you don’t mind), zip the whole project directory, upload the ziip-file “somewhere” (really, there are a myriad pages to do this, pick what fits you best) and then provide a link to the uploaded files here so others can download and run your project.

In your implementation you’re throwing a dice each time they get in contact with the virus.

That’s intended :slight_smile:


and then provide a link to the uploaded files here so others can download and run your project.

https://drive.google.com/uc?export=download&id=id=1Imndp-mB6-8ecbhXxEjozndas_9hTFSv

rubendw03 | 2020-03-23 15:43

This is the correct link

https://drive.google.com/uc?export=download&id=1Imndp-mB6-8ecbhXxEjozndas_9hTFSv

rubendw03 | 2020-03-23 15:45

What you provided seems to be some compiled version of your project, i.e. I can’t open it in Godot and look at the script. You should see your project’s location in the Project Manager (the window that’s shown when Godot is starting). Click on the little folder-icon next to it and your file explorer should open that directory. Simply zip everything you see in there and upload that! Do not use Project > Export!

njamster | 2020-03-23 16:53

While what you uploaded still wasn’t ready for import (it didn’t have a project.godot-file and I had to fix dependencies because file-pathes changed), it was enough for me to spot the error: The tree of Game.tscn contains a hidden node called Human. This is the node that is connected to the signal and only that one! When you later spawn more human instances, you need to connect them individually as well using:

human.connect("human_infected", self, "_on_Human_human_infected")

(This code should go into _on_HumanTimer_timeoutin GameScript.gd)

njamster | 2020-03-23 17:41

Thanks that worked!!!

rubendw03 | 2020-03-23 18:51