Do they mean the same thing?
If so, which do you prefer? I think the Roblox API interchanges them freely
If not, are signals the objects which have events, or what is the difference?
Signals and events are often seen together, but there is a difference between them. Generally signaling is an asynchronous operation like adding an element to a message queue or sending a signal to a process in Linux. Generally event systems are implemented upon signals, so you could think of events as a specific way of using signals. Events are signals, but not every signal should be called an event.
Signals in some applications are actually callbacks while in others they are data. Iād say that pretty much any IPC or cross-environment communication that doesnāt stream and is asynchronous can be safely called signaling. Synchronous operations and streaming can be built on top of signaling systems though, so sometimes they are referred to as signaling as well (including events).
Iām not sure I understand your distinction, could you perhaps clarify by categorizing these examples?
An addition expression: 1+2
A function call to a helper addition function: add(1,2)
A function call to print: print'hi'
A function call to an arbitrary function: somefunction()
A function call to a callback when something is finished: callback(wait(1))
A roblox bindable event: event:Fire()
1+2
is neither a signal nor event, however it can internally be implemented using signaling. Iād imagine that most operations at the hardware level inside of the CPU use signals.
add(1,2)
Function calls are sometimes called signaling, but I donāt think it is proper. It is synchronous, and can be built using signals though. Functions each have their own environment and calling a function is passing data between those environments.
print'hi'
This function call does execute C code, however pretty much the same principles as above apply.
foo()
Another function call, same as above two.
callback(function () wait(1) end)
This has multiple levels to it. Calling the callback
function is just like above, however passing a callback enables asynchronous operation. I mentioned above that signals are sometimes functions, and a callback counts. If callback
was adding the passed function to a queue, then Iād say that you are signaling the owner of the queue. (Assuming that the owner isnāt you. If you arenāt doing communication over some bridge, then is isnāt signaling. You need a second party to do that.)
event:Fire()
Iām not sure how Roblox internals work in this case, but I could see it working as follows:
- Your script signals the scheduler and passes the relevant RBXScriptSignal.
- scheduler receives the message and puts the sender on a stack and begins executing the handlers while passing any relevant event data.
- each function in turn does its thing and optionally returns a value to the scheduler (this is how modules work)
- scheduler takes signal sender off of the queue and returns execution to it, optionally with some values.
Since the whole process is synchronous it is a bit of a stretch to call the whole thing signaling the handlers. Each synchronous call again could be built using signals and perhaps the script could actually continue running after signaling the scheduler but then makes a second call to suspend execution until the scheduler is done. It is definitely an event system, and could be called a type of signaling.
Would you consider these definitions accurate?
A function call is considered a signal iff it is calling an external, second party function
A signal raises an event iff it calls a middle-ware function whose responsibility is to pass data to listeners
Also, I didnāt mean to ask about callback(function () wait(1) end)
, but I think thatās a really good case
Would callback(wait(1))
and callback()
qualify as signals too (but not events) since they are probably passing data to a second party (judging by their naming ācallbackā)?
On Roblox, we usually use the word āsignalā to refer to the RBXScriptSignal
data type, and the word event
to refer to members of classes which are signal objects, such as GuiObject.Activated
. This convention is not necessarily always consistent.
They are just words though. People both on and outside Roblox use them to refer to many things. It depends on the context.
Iād accept that. However since every function maintains its own environment, every function can be called its own entity/party and calling is always signaling in a synchronous manner between functions.
Iād accept that that is a case of how a signal could raise an event, but not the only way. More generally an event is a one to many method of communication and may be implemented using middle-ware functions or not. An event could be seen as the duplication of a signal to any number of listeners and need not even be synchronous like function calls. However they are synchronous in Roblox so I would hesitate to call them raw signals. It could be built using signals, but it is really signals plus a bit extra.