Using Interfaces in Game Development (Unity Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
I finally finished huh good timing hey Jason what's up not much just getting ready to play some overwatch I want the joint with your ass sounds good but it's kind of in a groove with some code I'm working on oh yeah what are you working on yeah you know me just another prototype that I'll probably never finish want to take a look sure I've got time awesome cool let me just share my screen all right so the concept is simple I created a room with some interactable objects like this lamp interact with it you have to look at it click your mouse and voila turns off click it again and turns back on so far I've only implemented this lamp a radio and this door but I'm hoping to add a whole bunch more yes then I want to use them to create puzzles and some sort of first-person mystery game that sounds pretty cool shouldn't be too hard either we'll definitely make for some good practice I hope so but I'm already starting to feel overwhelmed already after just three objects that's the problem well I want to add more but the code gets more complicated with every object that I add mmm really mind if I have a look sure so all the interactions are driven by the player class you can see here in the update method that it finds the nearest game object which is based on the player's view and checks for fire one input then it searches the game object for each of the interactable object trips that I've already implemented and calls their respective interaction methods hmm I can definitely see why you're overwhelmed you're gonna need to add additional checks for each one of those interactable objects that you add at this range of player class is going to be huge there's gonna be a monolith yeah exactly funnily enough though it's not even why I'm overwhelmed I've been reasoned through all my options and honestly I can't decide which approach to take to solve this problem ethic analysis paralysis we've all been there yep I could bore you with all my research but I'm curious do you have any thoughts well let's break this into first have a look at your requirements you need to implement potentially endless numbers of interactable objects for your player and your problem is that your code cast scale with that requirement so at the moment you're gonna need to add new lines of code for every single item I think you'd be better off making a contract hmm contracts you and Charles covered that in the stream this week right we did cover that in our stream I didn't know you watch this yeah they're a great way to start the week so if I'm remembering correctly you define an object's contract using an interface right yeah that's right hmm okay it's funny I've used interfaces before but I never really about them like that like like a contract or a promise to do some work but it makes total sense well believe it or not that's how they're defined in Microsoft's own documentation an interface defines a contract in any class are struck that implements that contract must provide an implementation of the members defined in that interval I should really serve reading those they do come in handy every now in the ti bit so to apply that to my situation I just need to create an interface that each of my interactable objects can implement and then have my player classes update method search for that instead right mhm pretty much and you've already got a good name for it too what's that interactable exactly clear get the point across no need to do is create an interface called AI interactable with a method called interact in fact if cut time now if you wanna jump into it sure alright so I'll create a new interface I interactable and hey why does everyone prepend their interfaces with an eye I've always done it out of habit but I never really asked why honestly it's just a convention that most developers use you don't have to add the eye but it makes it easier to tell that you're referencing any interface at a glass wow it's really that simple who would have thought next I'll add the interact method and finally back in the player class search for eye interactable and called dot interact instead nice and don't forget to update your implementations to right of each one implement I interactable and move each of their behaviors into the interact method and that should be it right yep nothing to wire up now why don't you test this okay I'll switch back to unity hit play and you everything still works oh and now you can add as many new interactable objects as you want without turning your class into Lola that's awesome in fact now that you mention it I do have one new interaction in mind one that wouldn't have been possible before oh yeah what is it let me show you first I'll make a new implementation of AI interactable called composite interaction I think I know where you're going with this one huh I bet you do basically it'll have a list of interactable objects that it'll loop through when the player interacts with it it'll be an easy way for me to group interactions up so I can trigger more than one looks good all right all I have to do now is wire it up press play possum not bad bottles not bad haha thanks and thank you for your help yeah no problem now it's the important thing now you have to save the project and go play over alright fair enough let me just wrap this up and I'll call you back when I'm in game alright either a special thanks to my top supporters Bergquist 3d dark rush photography our star Thomas Tron Jakub Asif Ari and iron Alex [Music]
Info
Channel: Infallible Code
Views: 81,849
Rating: undefined out of 5
Keywords: unity3d interface c#, unity3d interface, unity3d interfaces, unity 3d interfaces, unity3d c# interface, unity 3d coding, unity game programming, game development, gamedev, unity3d, unity 3d, unity, unity tutorial, unity3d tutorial, unity 3d tutorial, interfaces in unity, how to use interfaces in unity, unity c# tutorials, interfaces in unity c#, unity interface tutorial, unity interfaces c#, game dev, infallible code, jason storey
Id: 2LA3BLqOw9g
Channel Id: undefined
Length: 6min 59sec (419 seconds)
Published: Sun Mar 22 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.