Connect custom signal

Godot 4

I’m making a Suika game. I created a custom signal that is triggered by two balls when they touch.

ball.gd

extends RigidBody2D

signal target_touch(Object_I, Object_He)

var Texture_Maxwell := load("res://sprites/maxwell.png")
var Texture_2 := load("res://icon.svg")

var rng = RandomNumberGenerator.new()

@export var lvl_ball = randi_range(1, 2)

@onready var spr := $Sprite2D
@onready var PC := $Physic_Coll
@onready var AC := $Area2D/Area_Coll

func _ready():
	if lvl_ball == 1:
		spr.scale = Vector2(0.159, 0.159)
		spr.texture = Texture_Maxwell
		PC.shape.radius = 25
		AC.shape.radius = PC.shape.radius + 1
	if lvl_ball == 2:
		spr.texture = Texture_2
		PC.shape.radius = 35
		AC.shape.radius = PC.shape.radius + 1

func _on_area_2d_area_entered(area: Area2D) -> void:
	var par := area.get_parent() 
	if lvl_ball == par.lvl_ball:
		target_touch.emit($".", par)

Since the balls are dynamic, I create a preload. I connect the signal, but the code in create_ball does not work.

main.gd

extends Node2D

@onready var ball_scene := preload("res://scenes/ball.tscn")

func _ready():	
	var ball := ball_scene.instantiate()
	ball.target_touch.connect(create_ball)

func create_ball(Object_I, Object_He):
	Object_I.queue_free()
	Object_He.queue_free()
	var ball := ball_scene.instantiate()
	$Balls.add_child(ball)

Tell me why it doesn’t work, please

Are you sure the _on_area_2d_area_entered is running? It seems strange to use Area2D for a Suika game.

Are you only creating one ball on _ready? can you show how you create other balls?

Yes, I checked it out.

They originally exist in main.tscn for tests

Did you connect the signal of these balls in main.tscn? Does the third one spawn on ready? Can you show the scene tree too?

No

There may be something offset with your ball scene then, your ready function should spawn a third ball at the origin (0,0)


The rigid body does at least have a connection, but I don’t think it’s target_touch connected to your main.gd, or the script would show a green marker. You are only connecting the signal on the mystery third ball that you spawn in _ready()

1 Like

Hmm, that sounds logical, I’ll try it now

@gertkeno IT’S WORK THANK YOU VERY VERY THANK YOU