C# Events in Unity

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey man what's up hey charles you've got some good timing oh yeah why is that well i'm just about finished working on a new feature and i could really use your guidance sure i've got time great let me just uh share my screen oh hey that's that project jason was helping you with right yeah i was having a scalability issue with my interactable objects and he suggested i use interfaces nice so what seems to be the problem here check it out so i've created this keypad here that i want to use to open doors and for a couple of other puzzles cool thanks i've got the basic functionality down i could add input clear it and i also have the ability to press this enter button although it doesn't do anything yet that's where i need your guidance sure let's have a look at the code okay i think the main classes to focus on are probably the keypad and door classes okay walk me through it sure so the keypad class has a bunch of logic related to adding and clearing input but as you can see its enter method is completely empty i need to add some code to open the door which has a method called open okay now obviously i wouldn't want to reference a door and call its open method directly from the keypad right yeah because that would create a tight coupling between them and you already mentioned you wanted to use keypads for puzzles too exactly so the other approach i thought of was to do something similar to my keypad display class which accesses a keypad's current input each frame my thought was to create an intermediary class that would reference both a door and a keypad then each frame it would check if the keypad had been unlocked and open the door accordingly something like that okay that isn't a bad approach but sounds like you aren't a fan yeah i mean it works but this whole check is useless once the door is open if i have a bunch of other doors and puzzles with similar components they'll all be making unnecessary checks for most of the game's run time so i don't know i just want to explore my other options do you have any thoughts well the one thing that jumps to my mind is using a c-sharp event you know i thought about that too but it's been so long since i've used events that i kind of felt intimidated oh no they're super simple trust me you won't have any issues using them yeah you think so i guess i was just feeling too lazy to read the documentation no worries man we've all been there you want to walk through it together sure if you don't mind no not at all happy to help go ahead and delete that code and let's add an event to the keypad class okay there perfect so c-sharp events are based on the delegate model which follows the observer design pattern the two key points to remember are that an event is a message sent by the object to signal the occurrence of an action and a delegate is the type that holds a reference to the event method so in a nutshell we're going to define an event that represents the moment that the keypad is unlocked to do that you're going to need to define your own delegate type go ahead and do that now above the awake method okay so that'll be public delegate and then the next part is the return type right yep and you can just return void for now okay then the delegate name how about unlock handler sounds good to me and it doesn't need to take any arguments so i'll just finish it off with some parentheses beautiful now use the delegate to define your event okay so public event unlock handler and what should i call it the general convention is to use an action verb in the past tense so you can call it unlocked oh yeah i've seen that before unlocked nice yep and that's it the only thing left to do now is to invoke it when the keypad's input value matches its unlock code cool i'll add that condition to the enter method and then call unlocked dot invoke if it's true sweet now there are a couple of considerations that we still need to address but let's wire it up to your door and get it working first okay sounds good should we make an intermediary class well we could but why don't we keep it simple for now what do you think about subclassing the door class huh yeah that makes sense i can call it uh keypad door then have it subclass door of course add a serialized field for the keypad and then in the start method subscribe the door's open method to the unlocked event exactly and by the way that works out so nicely because door's open method matches the signature of your unlock handler delegate method that we created oh yeah it's kind of like a happy little accident something like that let's switch back to unity and set up your door right so i'll use this interior door here add the keypad door component reference the keypad and play the scene okay enter the code no peeking now and boom the door is open heck yeah that looks great man thanks i'm feeling much better about this code it's definitely more scalable and will be way easier to maintain in the future yep that's the idea speaking of which we still need to address those two things real quick oh yeah that's right so for the first thing go ahead and run the scene okay now delete the keypad door from the hierarchy and unlock the keypad sure see that error in your console missing reference exception object of type animator has been destroyed but you're still trying to access it what happened happened is that your keypad door subscribed to the unlocked method on start but it never unsubscribed when it got destroyed oh so the event fired and tried to trigger a method on an object that no longer exists bingo do you know what you have to do next let's see switch back to the code and then unsubscribe from the unlocked event in the keypad doors on destroy method is that it yep that's right for now go ahead and test it again sure run the scene delete the door punch in the code and hey is a different error message object reference not set to an instance of an object on line 62 in the keypad class let's check it out okay let's see here line 62 huh unlocked is throwing the error is that because oh we unsubscribed so now the unlocked method is null you got it oh well that's easy enough to fix i'll just use the question mark dot operator fun fact that's actually called the null conditional operator oh yeah i kind of like my name better whatever floats your boat man why don't we switch back to unity and check it out alrighty start the scene remove the door unlock the keypad and nice looks like everything works as always thank you for your help ah no problem man i really do enjoy it and helps keep me sharp still you're definitely getting a credit in this game oh so this is a game now i thought this was just another prototype maybe maybe the jury's still out but we'll see well i'm gonna go catch a run before my next meeting so uh i'll talk to you later all right all right man enjoy talk to you later bye a special thanks to my top supporters burkwas 3d dark rush photography r star thomas trond yakub al safari and iron alex you
Info
Channel: Infallible Code
Views: 31,980
Rating: undefined out of 5
Keywords: unity c# events, unity3d c# events, c# events in unity, c# events in unity3d, unity events and delegates, unity events and listeners, unity3d events and delegates, unity3d events and listeners, unity3d events listeners, unity events listeners, unity events delegates, unity3d events delegates, unity programming tutorials, unity3d programming tutorials, unity3d coding tutorials, unity coding tutorials, unity3d, unity 3d, gamedev, game dev
Id: GUTURxgcoj4
Channel Id: undefined
Length: 8min 34sec (514 seconds)
Published: Sun Apr 05 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.