Send a signal to other scenes nodes

Godot Version

4.6.1

Question

Hi i create an FPS game so at the moment of create an ennemy i place it in the scene but for showing the hitmarker so i want to send a signal so i ask how can we send a signal to the player ?

help me please

Make a signal connection when you instantiate using connect()

ok sorry for the noob question but how my player can recive it ?

Connect the signal to a handler function in player’s script.

like that ?

for the ennemy

signal hitMarker

func _ready() -> void:
	hitMarker.emit()

for the player

func _on_ennemy_hitMarker() -> void:
	hitMarker()

Well you need to connect the signal in order for callback to work, either in the editor if the enemy is placed in the editor or via script if you instantiate the enemy in script.

ha but next time i will create an enemy spawner and there will are thousands of enemies

It’s probably better so set things up so that marker sends the signal when it hits an enemy.

OMG i found it just i d’ont use signals but when my bullet collide with the ennemy it search the player in the scene with that :

func _ready():
	player = get_tree().current_scene.get_node("Player")
	await get_tree().create_timer(timeBedoreDestroy).timeout
	$".".queue_free()


func _process(delta: float) -> void:
	position += transform.basis * Vector3(0,-speed,0) * delta
	ray.force_raycast_update()
	if ray.is_colliding():
		if ray.get_collider().is_in_group("Ennemy"):
			ray.get_collider().hit()
			player.hitMarker()
			queue_free()
		else: 
			queue_free()

I don’t see from your video how you think that is working.

You’re calling queue_free() on it the moment it is created. It lasts for one frame.

This:

$".".queue_free()

Is exactly the same as this:

queue_free()

Also why does a bullet hitting the enemy need to know about the player when it hits?

await jumpscare :sweat_smile:. I also got caught off-guard when quickly looking through the code; doesn’t help that it’s not highlighted as a keyword on forums.

This code will result in errors if the ray hits something before the timer runs out. Besides, I don’t see any signals there.

1 Like

:sweat_smile: Sorry like im a noob in Godot so i was thinking to use signals for communicate between nodes but it was more harder so instead of using signals which is more harder i created a variable of the player and call a function. Thanks for the other advices

because it call a function of the player for show the hit marker and thanks for the advice