Connection Lifetimes
Lesson 32 of 48 in Coddy's GDScript for Lua Developers course.
A Lua observer function can implement one-shot behavior by clearing its stored callback after delivery. This is application logic, not a built-in Lua connection flag. GDScript provides CONNECT_ONE_SHOT, which disconnects a listener after the first emission. The signal itself remains usable. Ordinary connections persist until removed or an endpoint is destroyed. Connecting the same Callable twice normally reports an error; use is_connected for an idempotent subscription API. The example emits twice to distinguish connection lifetime from signal lifetime. Explicit ownership also prevents retained callbacks from forming RefCounted cycles.
Complete Lua 5.1 comparison. Run main.lua from the directory containing all shown files:
Lua file main.lua:
local seen = {}
local callback = function(n) table.insert(seen, n) end
local function emit(n)
if callback then
local current = callback
callback = nil
current(n)
end
end
emit(3)
emit(4)
print(#seen)
Lua output:
1GDScript file demo_ping.gd:
class_name DemoPing
extends RefCounted
signal ping(n: int)
GDScript 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). Attaching must be idempotent; detaching an absent connection is safe. Sending emits once. The solve function attaches one callback, sends each integer, detaches, and returns the 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. The supplied main.gd 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 Lua 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