Game Events - Power & Simplicity in Unity3D

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video i'm going to show you a really cool tip that solves a problem every unity developer runs into it's something that's easy to use and easy to implement in just about any kind of project and it really applies to any kind of game or even non-game project that you might be working on the technique you're about to learn will also allow your game design to be a lot more flexible you'll be giving your game designers the tools that they need to really make a great game and to do it without having to come to you for code every time they want to make a minor change now if that all sounds a little bit crazy or you're like i don't know what this means just hang on for a little while because i think that by the end of this video i really should have expanded your view on game development and giving you something that you can really use going forward in almost every project that you do it'll really make a big difference i think for you so please just follow along and don't forget to hit the thumbs up button and subscribe if you like this because then everybody else will learn these cool techniques and make cooler stuff too and if you have questions about it or just some comments about things that you think i missed or just want to learn more about please drop them down below all right let's get started with the problem the general problem goes like this people start to build out games and they build it out relatively simply they think about it as hey we're going to build this game get it working make it work right and later on we're going to add in a couple more levels and start expanding on it but then when they go to expand they start to realize that things aren't quite so easy they can't just reference stuff together they can't just kind of uh link things together the way that they want to without restructuring and reorganizing their entire project another one of the problems that we're going to solve is the fact that designers usually want to do quite a bit of stuff when they're developing a game if you're working with somebody who's a game designer or somebody who's just really creative and artistic they want to do all kinds of crazy cool things when stuff happens if you kill an enemy they want to have all kinds of cool stuff happen maybe some light starts flashing over here and a little mechanical thing moves across the screen and some particles play they want to be able to do all kinds of crazy stuff and they're going to constantly ask you to add these crazy things to the game you've got two options you can either manually start coding these things in every single time you could say no i guess i guess that's option number two and there i guess i lied there are three options the third option is to use this trick that we're about to go through and give them the ability to do quite a bit of that on their own so you're going to solve two major problems with this and some other ones that you'll see kind of along the way and probably really find just solutions or i guess you'll find that this is a solution to a lot of problems that you come across in your own code and in your own projects so let's get started with my sample here right now i've got a simple game where everything is all in one i've got some characters set up a light setup you know three different characters that i want to be able to click the kill and then have them pop up some text on the screen i've got my text objects already set up if i go to my game view and just click them on you can see i've got a little message that says hey the enemy was killed or maybe when i click on this big one i want some message that says you win to pop up or maybe it's when i kill all of the enemies i get the you win messenger i want something to happen there pretty easy for me to hook that all up i could create a simple click to enable script that allows me to click on the npc and make the text appear but what happens down the road when my project grows and i actually need to start splitting things out maybe moving my ui into a separate scene or having things that are in different scenes interact with each other say i've taken my event system canvas and camera and moved them over to a new area suddenly these prefab references aren't going to hang around so how are we going to solve this problem well the core of it comes down to scriptable objects and a really great tutorial on how to use scriptable objects to create your own game eventing system after you finish watching this video you should definitely go check this tutorial out it's a really great written tutorial that goes step by step through a lot of the things that i'm going to cover and some stuff that i'm not going to talk about today before i go into how this system all works and how you can recreate it let me show it in action show you how you can hook it all up and just how simple it is so here i've changed up our scene just a little bit our click to enable script is now a click to kill script and if i hit play i should be able to actually click on these guys watch them die and see some text pop up so how does this all work how is it hooking into this ui element that's in a totally separate scene well let's take a peek we'll dive into the click to kill script and take a look at how it's all put together so in our click to kill script you'll see that we have a single serialized field for a game event named on died that's the thing that we're going to magically hook into when our character dies let's see how the death works first it's relatively simple we have a single boolean value for whether or not we're dead when we click on the character with our collider so on mouse down will just fire off whenever we click on a character that has a collider in 3d so if the character hasn't already died we call our die method in the die method we get our animator component set the dead boolean to true to trigger the animation and then we fire off the on dyed game events invoke method that's what we're doing right here on line 18 and then on light 19 we finish off by setting the dead variable to true so that we don't die multiple times so how is this game event working what is invoke doing how does that somehow magically hook into a ui element that's in a totally separate scene well let's go take a peek at the invoke method i'll click on it hit f12 in my editor so let's examine this game event class what is the invoke method doing what is it exactly since it's not a mono behavior and how does it work the first thing that we want to note is that it's a scriptable object and not a mono behavior that means that we can't add it as a component to one of our game objects unlike the click the kill script that we just drag right on descriptible object needs to be created a different way and you can see on line four kind of how we create it we add an asset creation menu using the create asset menu attribute we give it a name and then we can optionally give it a new file name let's see what that looks like when we've created a scriptable object and we want to add it to our game if we go to the project view i'll go to my game events folder right click choose create and choose game event because i have game event as my menu name that's showing up at the top of my creation menu i'll name this npc died and now that i've created i can see it in the inspector and i can go assign it to that character so if i go select my character female right here that has that click the kill script since i've removed the old game event i need to now assign a game event because it's missing so i'll take this npc died and just drag it right into there i can also hit the little search box and find all of the events that would fit into that slot now that i've got that done let's go take a look at how it actually invokes we know how to create one what's the rest of this script actually doing so we'll go take a look at our game event script and see how it all works so here you'll see we have an invoke method and it loops over all of our listeners and then it calls the raise event method on those so what's a listener and how are those added in well right here on line 7 you'll see that we have a hash set of game event listeners it's a little bit different than game events in fact they're quite a bit different but they're really tightly coupled they link together and they're kind of how the magic all works together so what's a hashtag i think we should cover that really quickly first a hash set is just a simple collection just like a list or an array that we can add things to but we can't add the same object to it twice the reason that we use a hash set here instead of a list is just to avoid accidentally listening to something multiple times if we're registering for a game event we don't want to accidentally double register for it so using a hashtag will prevent the same listener from listening to an event two times so how does a hash set help us or how is it actually getting used or filled in let's take a look at line 15 where we register our game event listeners the register method just says hey listeners.add which literally because the hash set prevents it from getting the duplicate and then the d register just removes it so all we really have here is a list of game event listeners that we're adding into this list here or this hash set and then calling raise event on this is actually a very simple observer pattern and that might make sense because hey this is an event so we're just implementing our own well implementation of the observer pattern let's go take a look at the game event listener now and see how ray's event works because that part must be complicated right that's got to be where it gets hard and really difficult to use but let's go see we'll click on raise event hit f12 and take a peek well here it is the raise event method as you can see on line 11 actually just calls another invoke method for a unity event if we look up here at our serialized fields we've got a game event that we're listening to and then we've got a unity event that we're actually going to act on or do as our thing whenever the game event fires off so again the way this works is the game event listener will listen for a game event and then fire off or perform a unity event now the way that it listens for these game events is in our awake method we call the game events register method remember the game event is a scriptable object that's at the project level or in the project view so there's only one reference to it and we're just registering for that when we awake and then when we get destroyed we deregister for it because hey we don't want to call an event back on something that's been destroyed that's probably a gone object that probably shouldn't be firing these methods off or shouldn't be getting the events anymore so we de-register or remove it once we're done with it or once this object is no longer around or no longer loaded so how does this all hook up and work in the editor well let's go set it up for the npc death text here i've got some text that i just want to show up when an npc dies i wanted to say hey the enemy was killed to do that i'm going to add a game event listener and then i'm going to select the game event by hitting the little search box and choosing npc diet i could have also just drug it right up there then i'll hit the plus button to add a unity event take my text mesh pro text drag it right down there as the target choose the function which will just be the text mesh pro text or text mesh pro ugooey dot enabled or the boolean enabled right here and i'll set it to true i'll save my scene then i'll hit play and we'll click on one of these guys and i expect my event to fire off just like that and the text to pop up now let's go through the process of adding another one i've got a boss death text here that just says hey you win like you beat the boss let's add in a listener for that we'll add a game event listener and maybe now i don't want to listen for a npc died i want to have this be a more specific npc like this big blue boss in the background i'd go to my project view go to my game events or my game designer would go right click choose create make a new game event and say hey this is the boss diet event or boss died event and as you can see they could start to build up a whole variety of these different events that fire off when different things die there's no reason we could only fire off one two we could of course fire off as many events as we want if we so decided so let's go see how we would hook this up i'm going to choose the female boss i'll assign the boss died event to her and then i'll go over to the boss death script and on our game event listener we'll just hook that into boss diet as well and i'll hit the plus button and i'm just going to enable the text here again but remember this is a unity event there's nothing that stops me from playing an animation running a tween or doing a dozen other things right now i don't just have to show text this is just one of a million different things that i could do with a unity event i could really do just about anything and in our game event listener we could always customize it and have it do things that a unity event still couldn't do if we run into something a unity event can't do make a custom game event listener that can do that or if it's something that's so common that you're doing that you don't want to have to hook in a unity event for it constantly because it's something super repetitive and also consider writing a separate event listener so let's try this out this should now turn on the boss death text when i click on the boss and kill it and turn on the enemy let's see enemy killed enemy killed yep and oh i didn't get the you win script so let's see why that happened ah it's right here i didn't check the box right there if i hit play again check the box to actually enable the text mesh pro script or the text mesh pro text ui element here then bam i win so let's talk a little bit more about the extending of the game event listener i briefly mentioned it just a moment ago i want to show you one way we could extend it so that maybe we pop up text for a short amount of time perhaps i've got listeners that i want to run a thing and then maybe wait a little while and then run another thing like pop up some text wait a second and then hide the text how would i go about that process well here is actually a perfect place for inheritance and here's where i would create a sub class like a game event listener with a delay let's take a look at how this is set up and how it differs and how it kind of makes this an easy to accomplish task without over complicating our game event listener so the first thing to notice is that it inherits from game event listener which means that i also had to make a couple minor little changes to support that inheritance let's go take a real quick peek see what those are the only difference is i made these two serialized fields protected so that they would be available in subclasses and we made the raise event virtual so that we can override it let's go back and take a look at where we're doing that and how that all works the other two things we've added are a serialized field for the delay and then another serialized unity event for the delayed unity event remember that we fire a unity event when we raise or invoke our method and then we want to wait a little while and then fire the next one so this will be the second unity event or the second set of unity events because remember you can stack these up and have as many things as you want in the unity event so what do we do here well we register and deregister and technically we don't really need those in here i don't know why i added them let's delete them just make sure that we know that because they're already in the base class i don't need those in there unless they happen to override or re-implement those methods so what is it actually doing differently well in our raise event method that we're now overriding because it's virtual remember you need the virtual keyword there so that we can override it we are calling our first unity event just like before then we kick off a co-routine because remember this game event listener is not a scriptable object it's model behavior that's on our text mess pro object or on some other game object whatever it is that you're using it for so we'll start a co routine we'll wait for the delay and then invoke the other event that's it nice and simple right so i think you can start to imagine some scenarios where you can set up your own game event listeners that do maybe different things that aren't always just firing off a unity event but most of the time what i found is that just giving the ability to use a unity event is the most valuable thing because people will start to do it and what you can do is look for areas where people are recreating the same unity event re-hooking up the same thing doing it over and over and kind of a repetitive process if you see that that's a pattern then consider making a new script for it but until then give them this flexibility let them kind of hook things up and hook up events and have multiple different things listening for events and doing all kinds of stuff because don't forget not just one listener could listen for something we could have a million things listening for every single event performance might get really bad but you get the idea anyway i hope this is helpful and i think that this should make a big difference in your projects there are a lot of other things that you can do with scriptable objects but this is probably the easiest and most valuable and i guess most impactful one that i know of and that really i've seen kind of push people's game development faster and just make things easier for them to put together so if you like this kind of thing again please just share the video somewhere you know grab a link go share it up on facebook or instagram or whatever things you use or hit the like and subscribe button also thanks for everybody on patreon i i still amazed every day that so many people just support the channel and and the stuff that i'm able to do and just put it out there so i really like it um i guess of course i really like it yeah i love doing this stuff and i hope that you guys like it too so if you do just hit hit thumbs up button and thanks again goodbye
Info
Channel: Jason Weimann
Views: 32,021
Rating: undefined out of 5
Keywords: unity3d, scriptable objects unity, unity3d events, events, unity, game event unity, game event listener, unityevent, c# event, game development, scripting, game programming, brackeys, c#, dani, game dev, game architecture, unity architecture, prefabs, unity3d college, unity3d tutorial, unity3d solid, unity3d architecture, unity tutorial, unity 3d, game development unity, unity 3d tutorial, unity game dev, game programming patterns, gamedev, unity tutorials
Id: lgA8KirhLEU
Channel Id: undefined
Length: 17min 14sec (1034 seconds)
Published: Thu Apr 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.