Signals, scopes and time
Two ideas organize everything that happens over time in Dialect:
- A signal is something that happens: a part was touched, a player joined. It is a value you can connect functions to, pass around and combine.
- A scope is something that lasts: a script, a part, a round. Everything started inside a scope (a loop, a timer, a connection) ends when the scope ends, so nothing is left running by mistake.
Signals
Connecting
Every event is a typed signal on the thing it belongs to. connect runs a function each time it fires and returns a connection:
local pad = world.root:find("Pad")
local connection = pad.touched:connect(function(other)
print(other.name .. " touched the pad")
end)
connection:disconnect() -- stop listening
connect takes whatever the signal fires with. part.touched fires with the other part; world.frame fires with the time since the last step; player.chatted fires with the text. The API reference lists the arguments of each.
A handler runs as a task of its own: it may sleep or wait on another signal without holding anything up.
Once, and waiting
door.touched:once(function(other) -- the next time only
print("first visitor:", other.name)
end)
scope:spawn(function()
local who = door.touched:wait() -- pause this task until it fires
print("opened by", who.name)
end)
wait returns what the signal fired with. It can only be used inside a task (see Tasks).
Making your own
local scored = Signal.new()
scored:connect(function(player, points)
print(player.name, "scored", points)
end)
scored:fire(somePlayer, 10)
Combining signals
Signals compose. Each of these returns a new signal:
local hits = part.touched
:filter(function(other) return other.name == "Ball" end) -- only balls
:debounce(0.2) -- once things go quiet for 0.2 s
hits:connect(function(ball)
print("a ball landed")
end)
| Method | Returns a signal that... |
|---|---|
signal:filter(keep) | fires only when keep(...) is true |
signal:map(change) | fires with what change(...) returns |
signal:take(n) | fires for the first n events, then stops |
signal:debounce(seconds) | fires once seconds have passed with no further event |
signal:throttle(seconds) | fires at most once per seconds: the first of each window |
Signal.merge(a, b, ...) | fires whenever any of them does |
Watching a property
Any property can be watched. The signal fires with the new value and the old one, whoever changed it (your script, another script or the physics engine):
part:watch("position"):connect(function(new, old)
print("moved", (new - old):length(), "studs")
end)
Tasks
A task is a function that may pause. Inside one you can sleep, wait on a signal, or start more tasks.
scope:spawn(function()
print("ready")
sleep(2) -- pause this task for two seconds
print("two seconds later")
end)
A script's own code runs as a task, and so does every handler, so sleep(1) works at the top of a script and inside a connect. sleep outside a task is an error.
time() is the number of seconds since the game started, and world.frame fires every simulation step.
Scopes
Every script has a scope, called scope. Every entity has one too (part.scope). Starting something in a scope ties it to that scope:
scope:spawn(function() ... end) -- a task
scope:after(5, function() ... end) -- once, in five seconds
scope:every(1, function() ... end) -- every second
part.touched:connect(...) -- the connection belongs to the scope that made it
When a scope ends, everything inside it stops where it is: its tasks, its timers, its connections and its child scopes. Nothing keeps running after the thing it was for has gone.
Entities have scopes
Destroying an entity ends its scope. Anything you tie to a part with part.scope ends when the part is destroyed:
local spinner = Part.new { name = "Spinner" }
spinner.scope:every(0.1, function()
spinner.rotation += vec(0, 10, 0)
end)
scope:after(5, function()
spinner:destroy() -- the loop above stops with it: no flag, no check
end)
This is the difference from loops that run until a flag is cleared. There is no flag to forget.
Children and ending a scope
local round = scope:child() -- a scope inside this one
round:every(1, function() ... end)
round:spawn(function() ... end)
round:cancel() -- ends everything in the round
Ending the parent ends its children. defer registers a function to run when a scope ends, last registered first:
round:defer(function()
print("the round is over")
end)
Waiting for several things
These are for use inside a task:
-- Run functions as tasks and wait for all of them. Results come back in order.
local results = scope:all {
function() sleep(0.2) return "a" end,
function() sleep(0.1) return "b" end,
}
print(results[1], results[2]) -- a b
-- The first to finish wins; the others are stopped.
local index, value = scope:race {
function() sleep(5) return "slow" end,
function() sleep(1) return "quick" end,
}
print(index, value) -- 2 quick
-- Give something a time limit.
local finished, result = scope:timeout(3, function()
sleep(10)
return "done"
end)
print(finished) -- false: it took too long
If one function in all raises an error, the others are stopped and the error is raised in the waiting task.
Time limits
A task, handler or step may run for 0.1 seconds at a time before it is stopped with script exceeded its time limit. Waiting does not count. The limit cannot be caught: a script that wraps a runaway loop in pcall is stopped again at its next step instead of carrying on. One task stopped this way does not affect the others.
