Signals and Payloads
Lesson 31 of 48 in Coddy's GDScript for Lua Developers course.
Lua functions stored in a sequence can implement an observer list. The language does not supply Godot's Signal type or connection lifecycle. GDScript declares a signal, connects a Callable, and emits its payload. Ordinary nondeferred connections run synchronously during emission; emitting is not automatically asynchronous. Update observable state before notifying listeners when the event contract requires the new state. Avoid capturing the emitting object in a callback that it retains. The examples deliver one message and remove no state, while the task adds change detection and event ordering requirements.
Complete Lua 5.1 comparison. Run main.lua from the directory containing all shown files:
Lua file main.lua:
local seen = {}
local listeners = {function(text) table.insert(seen, text) end}
for _, callback in ipairs(listeners) do callback("hello") end
print(seen[1])
Lua output:
helloGDScript file demo_notice.gd:
class_name DemoNotice
extends RefCounted
signal message(text: String)
GDScript file main.gd:
extends Node
func _ready() -> void:
var source: DemoNotice = DemoNotice.new()
var seen: Array = []
var callback: Callable = func(text: String) -> void: seen.append(text)
source.message.connect(callback)
source.message.emit("hello")
source.message.disconnect(callback)
print(seen[0])
GDScript output:
helloChallenge
EasyImplement PulseSource with signal changed(value:int), value initially zero and set_value(n:int): emit changed once only when n differs from the current value, after updating it. The solve function applies inputs and returns the final value. Locked probes separately verify event payloads and count.
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 pulse_source.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 source: PulseSource = PulseSource.new()
var observed: Array = []
var record: Callable = func(n: int) -> void: observed.append(n)
source.changed.connect(record)
source.set_value(4)
source.set_value(4)
source.set_value(2)
source.changed.disconnect(record)
if observed != [4,2] or source.value != 2: 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 Label2Flow and Functions
Branches and ValuesPattern MatchingLoops and RangesTyped FunctionsDefault ArgumentsRecap: Command Count3Collection Ownership
Array OperationsTyped ArraysShared and Copied ArraysDictionary PresenceDictionary TraversalRecap: Word Frequencies6Object Relationships
Inheritance and SuperDynamic DispatchComposed ObjectsRecap: Tiered FeesSignals and PayloadsPractice on your own: Online GDScript compiler