Signals don't get emitted in Autoload script attached to a Node

Godot Version

3.5.3

Question

I’m trying to use an AutoLoad scipt attached to a Node called BroadCaster as Event Bus but when I try calling its functions that are supposed to emit signals they never get received.

Code inside the script looks like this:

signal doing_damage_to_enemy(target,base_DMG,level,res_pen)

func Doing_Damage_To_Enemy(Target,Base_DMG,level,RES_Pen):
    print("Doing damage to enemy")
	emit_signal("doing_damage_to_enemy",Target,Base_DMG,level,RES_Pen)

This print does get called so that means the function gets called as well.
I call this function inside a Node2D that’s meant as playable character like so:

func Basic_Attack(target):
    Broadcaster.Doing_Damage_To_Enemy(target,Base_DMG,level,RES_pen)

(The other parameters are calculated elsewhere in Player Node and are not connected with this)

This signal is connected to a Node called Enemy_team through editor like so:

func _on_BroadCaster_doing_damage_to_enemy(target, base_DMG, level, res_pen):
	print("This should be execuded")
	target.Taking_Damage(base_DMG,level,res_pen)

But this print is never triggered nor is the enemy health ever changed.
Anyone has an idea what I’m doing wrong and how to fix it?

Where and when do you actually connect the signal?

Also, when posting code, you can use the </> button to get syntax highlighting and indentation for your code.

1 Like

How is this done? Does Enemy_team have an instance of EventBus?
It shouldn’t. That would quash the single-ness of the singleton.
Connect to the signal in code in the Enemy_team node:

func _ready() -> void:
	EventBus.doing_damage_to_enemy.connect(_on_BroadCaster_doing_damage_to_enemy)
# Note that you will have to bind the variables you want to pass to this function

Couple of suggestions:

  1. Use the </> tags when posting code in this forum. Unformatted code is no use to anyone.
  2. Tighten up your variable name scheme. Don’t mix and match upper and lower case letters from one variable to the next. You have Target and then res_pen
  3. Use types in variable and parameter definitions. This will eliminate a large number of hard to find errors. ex var base_dmg:int = 10
  4. Tighten up your function and signal naming scheme. You have a signal named exactly the same as a function. That function name doesn’t really tell anyone what it is doing. Consider func apply_damage_to_enemy().
    func Base_Attack() This is not the greatest name since again it doesn’t tell what is happening in the function.
    You are likely getting name conflicts because of the loose way you are approaching it.
    Use the style guide as a starting point.
2 Likes

Actually I made a mistake while typing Event_Bus doesn’t exist that was one of my previous attempts the real name is BroadCaster. I connect its signal to Enemy_Team through editor using signals tab and the function on_BroadCaster_doing_damage_to_enemy() was automatically created. The inconsistent naming is fault on my part since I didn’t think anyone but me would be seeing it but I doubt it interferes with what I’m trying to accomplish. Also I’ll try adding <> once I figure it out, I didn’t know of this feature.

There is a toolbar on the window where you type your messages.
One of the buttons is </>. Select your code and click that button and it will keep its formatting.

I don’t think its possible to connect to an autoloaded script via the editor.
You are probably connecting to a node which has the autoloaded script attached but that node is not necessarily in the tree whereas the autoload node is.

1 Like

Yes you are correct although I think the node is in the same tree . So what should I do move the Node to different place in the tree?
I tried that it doesn’t appear like anything changed.
Also I tried to clean up the question a bit hopefully it helps.

I connected Broadcaster with Enemy_team through code. This is the script inside the Enemy_team

func _ready():
 BroadCaster.connect("doing_damage_to_enemy",self,"_on_BroadCaster_doing_damage_to_enemy")

func _on_BroadCaster_doing_damage_to_enemy(target, base_DMG, level, res_pen):
	target.Taking_Damage(base_DMG,level,res_pen)  

It’s working as intended now. I’m not sure why it can’t be connected through editor but I’d say the issue is solved.
Thanks everyone for trying to help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.