02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Q:Why Dispatch Events?

Students Question Adam: What is the use of dispatching events?

Video Transcript

00:00All right, so before I continue any further, because we learned as we just learned a lot of stuff right now, so let me just put it back to you that you guys ask me a few questions, anything you want, and then I'll ask you guys questions if you don't have any questions.
00:14That's a good point. Why? Well, because we haven't really built a lot of classes yet, but let's think about a good sample of why would you want to create your own event.
00:28Right now until now we work with things that existed mainly and I'm going to connect to Adam you're building now a game. Now, imagine that in your game, there's specific events that happen such as whenever a student whenever let's do this.
00:43Okay, you have a background. You have a scoreboard. You have all these animals and features and things now.
00:50Wouldn't it be great if you could have all of them know whenever a level change because every every object has to do something when the event when the level changes the background needs to change your enemies have to stop moving maybe maybe have to kill themselves or other things of that nature.
01:06So, when I would dispatch an event what that enables me to do it enables me to be less generic. If I'm just listening to click events and mouse events and events and all these of their ability, I'm dependent on very generic things.
01:19Now, if I have specific needs, you know, I know that the most important thing in my application, the thing that most of the objects in my application need to know about is, for example, that background changes to level change.
01:33Then I would create an event that's a triggers that I would trigger that event saying I would dispatch the world and say, okay, everyone know that right now, know that something changed.
01:42I want to connect this down and this connects the intro to that course. But let's answer that question and let me answer that question as a way to continue this point.
01:52Why do we use events, you know, like, isn't it easier just go to the dog and say dog move isn't that easier than dispatching events and going all this stuff and obviously it's harder to use events events are not as intuitive events create this invisible barrier between your code, it creates like this line invisible line that you have to follow somehow.
02:11Contrary, if I just go to the dog and I say on if flash would say if there wasn't events at all, and we just say like, I would have a function, I would have a trigger that would whenever a click happens automatically would call a specific function, then I would have a I wouldn't have the freedom to connect the dots and say, okay, all of you guys are treated in the same way, you'll be using this function.
02:32Not only that, I wouldn't have the option of saying, well, you know what, when the cat moves, it means that it's bedtime. And when it's bedtime, it means that we should the sky should get darker and this should be this and that should be that and there's four or five things that need to happen.
02:47There's four or five things that are waiting for the moment that this cat will move.
02:52I would even put it in a way like imagine that you have your game around really random game you have your cat and whenever you move the cat around. Whenever a dog is close to that whatever a cat moves around all the dogs need to be an alert. Now we have 50 dogs.
03:07So you could have you could have gone done done created a loop inside the cat and treat and alerted telling the dogs, I'm moving now, telling every single dog, you know, there's every single dog, I'm moving now be careful, or I'm moving now you're updated.
03:23And whenever I'm stopping them telling them all but the problem would be then what would happen if you have to add another 20 dogs later.
03:28So you have to go back into the cat class and update and now the cat needs to know about every single dog. Contrary to that, an event enables me saying, whenever I add a new dog, I can tell the dog, well, keep track on this camp.
03:40That's it. The cat doesn't even know this dog is exists and it doesn't need to know. So if you connect this also to a game is you're playing like a, if you have a game and you have like fire shooting and things going on. How do these enemies know that you're approaching.
03:53You're it's true you're to if we're working in a laboratory the programming you're triggering to the world you're letting them know you're moving and then they know you're there. They can't know that you're there unless you tell them that you're there, but you don't know they're there.
04:04They're your enemies, you know, you have no idea what's there they could be in level one there's 10 enemies in level two there's 20 enemies level five there's birds that are enemies and it's a completely different thing. Your animal can't know about everything.
04:16Now until now it was good enough to work with the with the built in stuff the click events the the regular enter frames and so on and so forth. Now that you're going into this object oriented world.
04:27You need to take a step back and start thinking about, okay, I have your world. Now, I have as in this this topic really, really connects these events connected this to this concept of scope and and keeping things private and some call this encapsulation but I don't like using big words that are there's no point to them in this stage.
04:45So really, what you want to create is you want to create worlds where the cat doesn't need to know about what's around it. All it has to do is tell the world a few specific things.
04:55What the world needs to know about the world needs to know about 1234 now that the world knows about it.
05:01It's the responsibility of the world to come to my, for example, this click event, it's a responsibility of my anem's to let. Well, it's the responsibility of my main to let anem's know that I want to know about whenever there's a click.
05:18Right, if that that that dispatch mint inside of anem's that dispatching and let's everyone know whenever it was clicked on enables us then to listen to it. And that's really all you have to take out of this if I would tell you what how do you actually now implement this at home.
05:32How do you figure out when do you dispatch your own event? If I know like from working looking at your code, you know, like now that you're building your game, start thinking, well, I don't need every single thing to know about every single thing.
05:45If I do, then I'm going to have lines and lines of code hundreds and hundreds of code that are not needed. What if I can instead of that, just dispatch the world from one central place and have listeners whoever needs to know about it, whoever's going to react to whatever happened will already know it automatically.
06:01So yeah, it's going to be less visual, it's going to be harder to see it. But that enables me to have all these little buckets, something's going to do here, something there, something there. And they're all just listening to that cat. So they're all just listening to that cat.
06:14It's not a direct relationship, I could add more things later on. It's smaller buckets of information. In that way, the cat doesn't have to worry about it. I don't have to worry about building one massive function that has to know everything, because it's just really specific small things that are dealing with with all these type of information.
06:32Okay, so let me go back and just let me see if that answered that question.
06:37Instead of saying if level, yeah, yeah, exactly. That could definitely happen. Yeah, exactly.
06:45It's more it's even at a level like right now when you're working, you have a main class and that main class has hundreds and hundreds of lines.
06:52Why? Because until now your main had to know about everything. If it didn't know about everything, you wouldn't be able to build your application.
06:59Step after that, after that is starting to build these objects. And now you could split authority. Now you could have you don't have to have a central hub, you could slowly, slowly break apart from that central hub, and break apart.
07:11And obviously, you're not going to do this overnight. This is a very, very big, you know, shift in thinking. So you first of all, you start with the small things, you start with like a level change, you start with, you know, those extra features that might be there today, might not be there tomorrow.
07:26Okay. Yeah, well, you know, it's challenging. And you guys weren't ready for yet. So now you guys are ready for it. And now it's time to take, you know, take it up another notch. Until you guys tell me enough with the notches.
07:42Exactly. Well, start from small, start seeing what could you, you know, like first, when you do this episode, you have to start thinking about a strategy.
07:52So first of all, you need to figure out you need to point down and find what are things what are things that are worth talking? What are things that are worth dispatching?
07:59Not everything. You don't have hundreds of dispatches. Okay. Alright, so any other questions?

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!