Connection Lifetimes
Lesson 32 of 48 in Coddy's GDScript for C Developers course.
C supplies no general one-shot callback registry: the registration policy belongs to your API. A callback can be explicitly cleared after delivery, and its context must remain alive until registration ends. GDScript normally rejects connecting the same Callable twice; is_connected supports idempotent attachment. CONNECT_ONE_SHOT removes that connection after its first delivery while leaving the signal usable. Disconnect ordinary subscriptions as appropriate. Avoid strong cycles where a RefCounted emitter retains a callback that captures the same emitter.
Complete C comparison. Save the listed files together and compile the .c files; main is the entry point.
C file ping.h:
#ifndef PING_H
#define PING_H
struct Ping { void (*callback)(void *,int); void *context; };
static inline void ping_send(struct Ping *p,int n) { if(p->callback) p->callback(p->context,n); }
#endif
C file main.c:
#include <stdio.h>
#include "ping.h"
static void count_ping(void *ctx,int n) { (void)n; ++*(int *)ctx; }
int main(void) { int seen=0; struct Ping p={count_ping,&seen}; ping_send(&p,3); p.callback=NULL; ping_send(&p,4); printf("%d\n",seen); return 0; }
C output:
1GDScript 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:
1Challenge
EasyImplement 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))
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in GDScript for C Developers
1Scripts and Scalar Values
Run Script FilesText and CommentsBindings and TypesInteger ArithmeticBoolean ResultsRecap: Stock Label7Events and Failure
Connection LifetimesFailure as DataValidated ConversionRecap: Safe DivisionScript Loading2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command CountPractice on your own: Online GDScript compiler