Is it possible to a node gives a signal to a node that are from the same tcsn?

4.3 stable

Question

Well, so basically the ally and enemies are from one tcsn and is divided with enum. So i want when I clicked on the enemies, it gives out signal to the allies. Is it possible to do that? Btw I’m really a begginer, I just started a few days ago

Here’s the full code(Yeah, it didnt works):

extends CharacterBody2D

enum side {KAWAN, LAWAN}
enum jenis {PINK, BLUE}

var mouse_over = false #Mendetek apa cursor diatas karakter
var is_selected = false
var target_pos: Vector2 = position
var posisi_awal: Vector2 = position
const SPEED = 120
var waitingTarget : bool = false
@onready var animated_sprite: AnimatedSprite2D = $AnimatedSprite2D
@export var my_side : side
@export var jenisku: jenis
@onready var collision_shape_2d: CollisionShape2D = $CollisionShape2D
@onready var monsters: CharacterBody2D = $“.”

signal target_selected(position: Vector2)
signal waiting_target()

var animasi = {}

Called when the node enters the scene tree for the first time.

func _ready() → void:
posisi_awal = position
target_pos = posisi_awal
if jenisku == jenis.PINK:
animasi[“Idle”] = “pink_idle”
elif jenisku == jenis.BLUE:
animasi[“Idle”] = “blue_idle”
if my_side == side.LAWAN:
animated_sprite.flip_h = true
else:
animated_sprite.flip_h = false
animated_sprite.play(animasi[“Idle”])
animated_sprite.stop()
# Pastikan tidak ada koneksi ganda
$“.”.connect(“waiting_target”, self._on_waiting_target)
$“.”.connect(“target_selected”, self._on_target_selected)
print(“Sinyal waiting_target dan target_selected terhubung untuk karakter:”, self.name)

Called every frame. ‘delta’ is the elapsed time since the previous frame.

func _process(delta: float) → void:
if kawan():
if mouse_over == true:
in_action()
elif not is_selected:
animated_sprite.stop()
if is_selected:
emit_signal(“waiting_target”)
animated_sprite.play(animasi[“Idle”])
if not mouse_over:
if position.distance_to(target_pos) > 2:
move_to_target(delta)
if position.distance_to(target_pos) < 2:
back_to_position(delta, posisi_awal)

	if Input.is_key_pressed(KEY_P): #Cek is_selected, boleh hapus
		print (is_selected)
		print ("waiting",waitingTarget)
else:
	if Input.is_key_pressed(KEY_Q):
		print (waitingTarget, self.name)
	if mouse_over and Input.is_action_just_pressed("Select"):
		emit_signal("target_selected", position)

func kawan() → bool:
return my_side == side.KAWAN

func in_action(): #Fungsi mendetek cursor
var now_position = position
animated_sprite.play(animasi[“Idle”])
if Input.is_action_just_pressed(“Select”):
selected()

func selected(): #toogle select
is_selected = not is_selected

func _on_mouse_entered() → void:
mouse_over = true

func _on_mouse_exited() → void:
mouse_over = false

#func targetting() → void:
#if Input.is_action_just_pressed(“Select”) and not mouse_over:
#target_pos = _on_monsters_target_selected() #Sudah tidak relevan lagi kodenya
#print (target_pos)
#elif Input.is_action_just_pressed(“Select”):
#selected()

func move_to_target(delta: float) → void:
position = position.move_toward(target_pos, SPEED * delta)
if position.distance_to(target_pos) < 2:
target_pos = position
func back_to_position(delta: float, posisi_awal) → void:
target_pos = posisi_awal
move_to_target(delta)

func neutral() → void:
is_selected = false
waitingTarget = false

func _on_waiting_target() → void:
waitingTarget = true
if self.my_side == side.LAWAN:
print (“Sinyal diterima karakter:”, self.name)

func _on_target_selected(position: Vector2) → void:
print (self.name)
target_pos = position
neutral()

Yes.

$sibling_node.signal_name.connect(method)

Or you can just use the inspector.

The receiver just needs a script to act on it.