Menu
CoddyTech

Connection Lifetimes

Lesson 32 of 48 in Coddy's GDScript for Go Developers course.

Go callback registration and duplicate policies belong to the API; function values cannot generally be compared to each other, only to nil. GDScript exposes connection identity through Callable and normally rejects duplicate connections, so is_connected supports idempotent attach behavior. CONNECT_ONE_SHOT removes one connection after delivery without deleting the signal. Disconnect ordinary listeners when appropriate and avoid self-retaining RefCounted callback cycles. The Go example explicitly clears its callback after one notification.

Complete Go comparison. Save the listed files in one directory and run all .go files together; package main supplies the entry point.

Go file ping.go:

package main
type Ping struct { Callback func(int) }
func (p *Ping) Send(n int) { if p.Callback!=nil { p.Callback(n) } }

Go file main.go:

package main
import "fmt"
func main() { seen:=0; p:=Ping{Callback:func(int){seen++}}; p.Send(3); p.Callback=nil; p.Send(4); fmt.Println(seen) }

Go output:

1

GDScript example file demo_ping.gd:

class_name DemoPing
extends RefCounted
signal ping(n: int)

GDScript example file main.gd:

extends Node

func _ready() -> void:
	var source: DemoPing = DemoPing.new()
	var seen: Array = []
	var callback: Callable = func(n: int) -> void: seen.append(n)
	source.ping.connect(callback, CONNECT_ONE_SHOT)
	source.ping.emit(3)
	source.ping.emit(4)
	print(seen.size())

GDScript output:

1
challenge icon

Challenge

Easy

Implement EventRelay signal tick(value:int), attach(callback:Callable), detach(callback:Callable), and send(n:int). Attach must be idempotent; detach absent connections is safe. Send emits once. Solve attaches one callback, sends each integer, detaches, and returns captured sum.

Input is one JSON array on stdin. The locked main parses it, calls the public APIs and prints result= followed by the returned text and a newline. No helper should print. Independent fixed probes verify required API behavior before the input case; they do not enforce implementation style.

Your code goes in event_relay.gd, exercise.gd. main.gd holds the supplied input/output code and cannot be edited. Keep each class_name type in its named file.

Try it yourself

extends Node

func contract_ok(data: Array) -> bool:
	var relay: EventRelay = EventRelay.new()
	var seen: Array = []
	var callback: Callable = func(n: int) -> void: seen.append(n)
	relay.attach(callback)
	relay.attach(callback)
	relay.send(7)
	relay.detach(callback)
	relay.detach(callback)
	relay.send(9)
	if seen != [7]: return false
	return true

func _ready() -> void:
	var data: Array = JSON.parse_string(OS.read_string_from_stdin())
	if not contract_ok(data):
		print("API_CONTRACT_FAILED")
		return
	print("result=" + Exercise.solve(data))
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in GDScript for Go Developers

Practice on your own: Online GDScript compiler