In a recent discussion over at Hidden Developers, some people brought up the argument that events are essentially loops under the hood.
So how do events actually work, do they use while loops under the hood, only firing when a condition is met, or is there something else that’s going on?
Are conditions under the hood only firing when something happens?
Events are code that only runs under specific circumstances, yes, but they do not constantly check for that condition like a while loop may.
I can’t vouch for the actual implementation, but consider the .AncestryChanged event of Instance, a very basic event. The code is not checking every clock cycle to see if the Instance.Parent property has changed, but, rather, updating that property involves some backend, unexposed logic to run. This logic includes firing the AncestryChanged event. Any code listening for that event will then run as a result. In this sense, event hooking is highly preferred over loops because the code only runs when it is needed.