Signal is not working inside of if statement/collision check

Godot Version

4.3

Question

Hi! Im trying to send out a gun fired signal which is autoloaded. The signal works perfectly fine without a collision check, but when i add the collision check back in, the signal stops working. There doesnt seem to be any correlation between the collision check and the signal emitter.

This is the faulty code:

extends Node2D

@onready var gun_raycast = $GunRaycast
@onready var sprite := $Sprite2D
var _smoothed_mouse_pos: Vector2

signal gun_fired

func _process(delta: float) -> void:
	_smoothed_mouse_pos = lerp(_smoothed_mouse_pos, get_global_mouse_position(), 0.3)
	look_at(_smoothed_mouse_pos)
	
		
	if Input.is_action_just_pressed("left_click"):
		if gun_raycast != null and gun_raycast.is_colliding(): #The collision check. When this is commented out, the signal starts working.
			
			print("Testing!") #This works
			emit_signal("gun_fired") #this doesnt

Signal receiving code:

extends CharacterBody2D

@onready var healthLabel := $HealthBar
@onready var health:= 2200

func _ready() -> void:
	Gun.gun_fired.connect(enemy_shot)


func enemy_shot():
	print("Hit!")

Have you tried Gun.gun_fired.emit() instead of emit_signal("gun_fired")? How does godot know that you want it to emit “gun_fired” from the Gun autoload if you don’t tell it?

1 Like

Thanks! This worked for some reason even though the autoload was not my first thought obviously, as it was working as intended before I added the if statement.

1 Like