Events - The Big Engagement!
Posted on April 23, 2015 by NoahWillCode
So when you hear the word "event," do you think of, say, a concert? A movie? Perhaps you think of something more signficant, such as a wedding or a convention (BLOXCon 2013 anyone?). While everything I just listed is correct, what do you think an "event" is in the world of scripting? Scripting doesn't have concerts, movies, or weddings... So what must it mean?
Events
in scripting are notices that something has happened, and these events
call their listeners
as soon as it happens.
Before I go further, here is how you can think of this in a real-world example.
Let us say you have a cousin and her name is Gabrielle. You and your cousin are close, and you two often catch up about what's going on in your lives. You know that Gabrielle has been dating a guy for a long time, and they're really happy together. You're often there, listening to her tell you about her boyfriend and how much she loves him. You're a great listener!
One day, Gabrielle's boyfriend proposes to her. Gabrielle says yes, and now she is engaged!! The very first thing she does after this amazing event, this engagement event
, is call you up and tell you about it! You're a great listener
, after all, and so you listen and you get excited! You react and you're happy for her and you set out to think of a wedding present to give the couple in a few months. Gabrielle also calls up several other friends and family members and tells them the same news, because that is what an amazing bride, such as Gabrielle, would do!
local Gabrielle = { Engaged = Event.new() } Gabrielle.Engaged:connect(function() print("Congratulations!") end)
The above story, while grand, is also very similar to scripting events
. Let's look at Gabrielle as if she were her very own ROBLOX object
. And let us say that you, Gabrielle's cousin, are a function
. Let's say Gabrielle has an event
called Engaged, and you, as a function
, are a listener
for that event. When Gabrielle gets engaged, her Engaged event
fires. What this means is every listener
to her Engaged event
will be called, meaning the functions
will be run. This is super similar to the story - Gabrielle got engaged, she called you because you were a listener
to her, and you got excited. You can see that last part, getting excited, as the body of the function
that you are as the listener
.
The only difference in our story and in scripting is that, in real life, Gabrielle can only call one person at a time. In scripting, all of the listeners
of an event
are called practically simultaneously, all at once, and react in unison. So maybe it'd be better to think of Gabrielle having a big group call, including you and ten other people, and she says "I just got engaged!" to all of you at once. Then you all say "awww!" or "congratulations!" or whatever else you may say at the same time. This is more similar to scripting, in that all the listeners
are called practically at the same time, and they all react, they all handle, the event
at once with the same information as every other handler
gets.
So events are, essentially, just that - events
. They are there to let you know when something happens, and you can listen in to these events
with any number of listener functions
that you would like to have handle the event
!
Want a more visual explanation? Here is a webpage with videos on events! http://code.theoremed.com/category/events/3
Commentary
Leave a Comment