Unreal Engine C++ Fundamentals - Interfaces !

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Glad to see more of your videos comung up! Im following up your punch playlist ( im at the beginning though) and i am enjoying it a lot. I would only say to zoom in the visual studio a bit, aside from that i find your videos incredible! Keep it up.

👍︎︎ 1 👤︎︎ u/Dungstenium 📅︎︎ May 31 2019 🗫︎ replies
Captions
[Music] hey guys this is Wojtek and today we're gonna continue our fundamental series talking about c++ interfaces and specifically how they are available to us within the Unreal Engine how we can determine if our objects inherit them and how we can assign them to pre-existing actors within our world so without further ado let's get started alright folks so to begin we're going to create our interface class and doing that just through the editor by going to new C++ class scrolling all the way down where we'll find the Unreal interface object definition let's just click Next and we're gonna call this our interactive and essentially this interface is going to allow all of our future actors to implement a method of interact and then you know wonderful things are gonna happen when we execute that so let's go ahead and create this interface alright so there's our interactive actor interface and we're also going to create a new class that is going to represent our interactive actor so this particular case was going to create our our base actor class and we're just gonna drop it into our props folder so we're gonna call this guy our interactive crop and again this can just get a little second and I'm gonna create this class okay and there is our interactive pro now just before we get too far ahead I'm just gonna quickly we organize this folder structure better so we're kind of are destructible props into your own father physics props into another folder and then we're gonna create a new folder for our interactive box so just move over this chunk and there's our physics and why would I move it as the correct folder there we go much better okay so now that we have our interactive prompt we're just gonna create a new blueprint just so it's there and then we don't have to think about the lighter so we're just gonna find our interact pro class select as VP all right cool so we're gonna save this actually save all of us and we're gonna switch it over to visual studio and you'll notice within our source definition we now have our interactive prop as well as our interactive actor interface so let's take a look at this thing and if we look at the CPP file you'll notice it's quite Berendt there's not really anything going on here because the majority of the work happens in the header and essentially what unreal is doing is it's using this empty class which they reference to as an interface as a way for their reflection mechanics within their engine to basically process these classes as if they were interfaces so folks can get from the world of Java C sharp or any other sort of language you you will realize that this does come with an actual implementation file but it's not super necessary so now how do we actually go about defining interface method so very similar to how we handle this view how we handle our regular method definition so we're going to annotate it with the are decorated with you function oh my good blue for mobile and also make it a main event of a category and this is just going to be a simple interact function so it's a void the parameters going in and that's kind of it this carried down now through our object definitions is going to basically expect any class that includes this interface to implement and override the interact method so it just happens that we have this lovely interactive prompt so let's go ahead and attach the interactive actor interface to our problem then we do that it's part of your class definition you're going to see public an interactive actor now you'll notice my resharper is being helpful and it's basically saying am missing this header file so let's just be good citizens and make sure that is in place because so there is our interface definition it is now attached to our interactive prop class and we can actually go ahead and now start implementing details for it this is where it gets just a little bit kind of wacky as we are forced to basically create the prototype declaration for our for our interface method and the way you do that is you basically replicate the method definition that we have in our in our interface and and this you function definition does initially have to be the same but I'm just gonna keep it consistent from you and we're gonna say avoid attract and this ends up being the this is our prototype declaration so this method doesn't actually excuse me doesn't do anything in order to actually support it we need to create a implementation version of this method and to do that you basically say virtual void interact underscore implementation and then we override and this is the internal method signature that the engine is going to call as it processes the interface definition sister reflection so let's just come at this and this is the method that we're gonna override in our CPP file for our interactive prop in order to you know have it do some stuff so let's go ahead and make our prop just a little bit more exciting because right now it's kind of kind of boring so let's go ahead and add in actually now we're gonna be a little bit lazy we're gonna grab the this guy we're going to grab the static mesh component that's gonna here and we're just gonna carry this over back to our interactive prop and this is just so we can have something to actually operate on and showcase that things are actually changing when we when we call our interact okay so we have our base mesh to find that's okay now we're gonna go ahead and flip over to the interactive prop CBP file and implement our method so we're going to say interactive presentation and there it is so let's go ahead and also include just not a problem so what I'm gonna have this interactive method actually do is just scale our base mission object so it's gonna go from small to big and vice versa so it's gonna be basically a little toggle so I'm gonna say bull because let's say big and troops let's go back to our prop and in here we're gonna say if I'm not big then I want to say base mesh and before we get too far let's actually initialize this component so base static component so there's that guy and then we're gonna set him to be our route great the other thing to do because my autocomplete has a hairy canary if I don't actually define these component of emissions so there we go and now we should be able to say okay for our bass mash when we are interacting with this particular prop we're going to set the scale the world scale to a new vector so we're simply going to say our scale vector is going to go from whatever it currently is which is you know one on one to just two and we're gonna say B is moisture now on the flip side reverse this set this back to one so everything is nicely explicitly defined and we can even go a little bit further and throw out some debug messaging but I think this is fairly straightforward that we don't really need to we don't really need to troubleshoot too much just now okay so there is our prop we implemented our interface definition our our interface is now kind of done its job you don't really need to mess around with it too much but we also need to make use of this interaction so how do we do that well in our player character if you guys recall from one of our previous slide trees tutorials I have this method here fireline trace and what this does is it simply shoots out a line trace of a certain of a certain length my trace distance and in this case I was using it to showcase how you can do like a shotgun spread shooting from the camera shooting from player head and and we're not really going to do that but we're gonna create a new method for interaction and we're gonna use this line trace logic to basically shoot from our players face down to the object that we're facing out in order to trigger that interaction so let's go ahead and implement that so I'm just gonna flip back to my header and create a new function and it's going to be called interact then we're going to just quickly flip over to its definition and then what I'm gonna do is I'm just gonna start quasi copying and pasting this particular logic there's sort of enough meat and potatoes in this particular firelight race that we can use it as a baseline so we factor we are also going to determine the position of the players head so we're going to say rotator and we're going to get the actors eyes point of view this is where we pass on our player wise location as well as our rotation and that's going to fill up these two vectors and then we're going to say the start of our light race to be fair his location and the ends to be the air location just a player Lecter has been noticed this is the same logic we have down here just without all this extra spread and sort of camera logic it wasn't okay oh sorry this should be us so we have our start and end now we're going to set up our collision parameters so to do that we're going to create our query prints we're gonna do sprints params so you want to say yeah let's make it a complex choice and we're gonna pass ourselves as the ignore parameter because we are shooting from the camera out for words we want to just ignore a player in case there is any sort of collision so I think we're okay with the trace parameters and then were gonna create some awesome pictures of Pluto so interact hit Aref it results and when I was going to determine whether or not we actually hit something so I'm gonna say B is hit I'm gonna cast a line trace going out from our world a single by Channel call which takes in our interrupts pit it also go it's also going to taken us start and the collision channel that we're going to use and I'm actually gonna lift the one that we're using in our fire line trace as well as our trace grammars excellent so this is gonna fire our line trace and determine whether or not we actually interact with something now if we actually hit something we want to do a little bit of logic so first things first I'm going to lift this little debug statement just so I can see and we hit something and I also want to see what did I hit so we hit this guy and sorry this is going to be intricate so this is the actor we hit and we're in draw a green line going out from the start to the end direction okay so with this in place we can now recompile just make sure everything is up and running so I'm going to do that do a quick check and I'll go back to determining if we hit the proper actor with the proper interface and then execute that interact option on them okay so everything is recompiled and now let's go ahead and actually make sure everything's still operational forgot who's running this is our fire line trace but we don't actually have a button for interacts why don't we go ahead and re remap that real quick so I'm just gonna go back to the input down here and we're gonna change range by one trace to attract and we'll be so that's that and actually I did make just one little mission here so we don't have a key binding for intercom so I'm just gonna go ahead and throw that in and make sure our in track button is actually doing stuff so we should see a line trace that's green if everything is working correctly and it's our occurring all right let's take a look now and if we hit F nothing is happening we walk up to our cube and we hit it and you can see there's our lovely light rays now it's coming from the the head of the player outwards so obviously any sort of rotation like I haven't shooted from the back of his head mm-hmm we're not gonna worry about that too much we can clean all that up in later tutorials we actually rotate and sort of behave accordingly but there's our line trace and Desert Storm to come together so let's go back to our C++ code and flip back to our interact method so our line trace is working we're actually hitting objects now let's determine if these objects are are things we can interact with so we have a couple different ways of going about that let's walk through a bunch of way so we're going to do it using it using this interface we're also going to do it using it's a which is a symbol or col2 implements interface as well we're also going to pass so what do I mean by implements introduced well we're gonna say if our hit actor so we're going to say interact good good actor get close and there is a implements interface methodology so into this we're going to say okay do i implement this interface of view and here and before we do that let's make sure we actually bring in a header file just forget about that so there's an interactive prop let's go back to our slightly broken piece of code and now we should be say hey do i implement an interface of you interactive actor and there's our static class so if i implement this interface i can now do stuff with it and the way you go about doing that is you can you actually end up calling the interface signature so you say i interactive actor interact and inside of that no there's no parameters mmm strange you may also notice we have this execute interact and this guy takes in a parameter and it takes in a parameter of the object you want to execute this method on so it's a little bit backwards from you know sort of our regular code definitions where you end up having to pass and the actual actor against the interface definition basically saying you know execute this operation on this guy based on this interface and if our check here passes you know this thing is going to do something so let's just we compile and and see if we can get this guy actually doing stuff okay so our project is we compiled let's go back to our game and let's see what this little cube is gonna do for us when we hit it and the answer is absolutely nothing because it does not implement our interface but never fear we created this lovely interactive prompt object we're just gonna tweak it a little bit to make sure it actually has a mash that does something when we when we click on it then we don't want to pipe pipes are boring let's do a exciting ok so if you guys can recall our interrupt method on our interactive prop is just supposed to scale it from one to two and then back so this cone it's just gonna grow a bit actually the expectant behavior is going to be this so let me just gonna walk these scales to one to one this is what I interact method should do if everything is working correctly so let's go ahead and drop this cone into our world and now let's start interacting with it hmm not working and that might just be due to the collision that we have on the smashing let's just see that's the case there's yeah so as you can see I fire my light race I determine if this is an interactive or intractable actor and I execute you know the method now my logic is a little bit broken and let's just see if we can determine why oh because I'm not flipping this around so rather than doing this why don't you just clean this up a little bit and we're going say yeah that's gonna flip flop it a little bit better okay sir well recompiled let's go back to our project and now bit go a little common bit go look up so this isn't overly impressive but the fact that we now have a generic interface with a method signature that you know is going to do some stuff behind the scenes we have a lot of power at our fingertips so what we can do is we can go ahead and create this interact method now on pre-existing objects and very quickly I don't need to adjust any code in my player character you know I don't need to determine if it is a destructible problem for some attractive prop I check my interface and you know I go from there but before we do that let me just show you a couple of different ways of going about getting at the interface definition so this is our I'm using the implements interface call let's steal this bit but let's comment out the rest of it oops so we can use is a to basically return the same logic is a and we're gonna pass in our new interactive actor static class and again so this passes we're going to execute our operation again so let's recompile and just try that out okay so where I'll recompile and let's now try out the is a execution and it doesn't work I let you down a false sense of security just when you thought I was working it's not and the reason it's not working because this picked me in the butt a couple of times is this actually examines the class definition of the of the object as a post to the interface remember when I said they were primarily used for a reflection of the engine so don't do this is gonna give you about types but we do have a secondary way of getting out this information and that is by doing a quick cast and the way this plays out is we say I Interactive actor equals and we're going to counsel it to our interactive doctor then we could simply say if our interactive actor is not null then execute our method signature based on this interface definition so let's recap I'll just try that out and I promise he'll not trick you anymore okay I'll recompile and let's double check this method so there you go if we cast it to our interface determine if it's null we can go back to our regular expected execution now I don't necessarily like this method the most because we are doing a cast which is just another you know extraneous operation or us here we're just getting we're just doing put comparison on the interface definition so I'll leave this guy in just so you guys have another example I'm gonna bring this guy back just due to my own personal preferences deal with it okay so we've gone ahead and introduced a new new prop we made a do stuff now let's go ahead and add the same interface onto an existing problem so we're gonna take this destructible prop and we're also going to make it slightly more interactive you talked about your public doctor and then very similar to other prop definition we're going to say you function actually you know episode we are just going to copy and past up because we are lazy developers so we're gonna grab this and we're gonna pop it back right over to our destructive the pump and then we're just gonna go ahead and specify the implementation so what do we want this guy to do well we're gonna have him basically get destroyed on interaction so somewhere here we have our trigger destroyed method so simply gonna say trigger the story pretty simple one thing I'm going to do is I'm just going to comment out the overlap logic for this particular prop because right now if we step into its boundaries it's going to cause the various overlays or overlaps and things are gonna go walking we're also gonna take off D component hint never I'm gonna do any damage with it just for sake of presentation so there's a trigger deploy interact with annotation I'm going to recompile will drop one of these destructible props and world and test it out okay so we have our destructible prop all set up let's go ahead and now drop this guy in and you know I'll do the rock we'll do a couple of these guys oh no no no where'd I go come back you oh man and there's another rock go so are Kellum bigger smaller still works and now look at that laser vision I just look at these rocks and they fall apart so there you go look at that very quickly we were able to modify existing objects within our within our game to include additional functionality we had almost no additional interaction with our with our player character we were simply just just adding another object with the same interface through the same piece of module and you know you can now have the power of basically doing well you know kind of whatever it is you want with these types of interests of interfaces lots of different bits of logic can be applied you can see you know you can have now a number of different set pieces that perform in different ways while our actor is executing or our players executing the same bit of logic um so this is how a lot of games do their interactions with doors and objects and all this wonderful stuff so you only have to hit one button and and magic things happen so thank you for tuning in and I hope to see you guys next time [Music] you [Music]
Info
Channel: Jolly Monster Studio
Views: 4,120
Rating: undefined out of 5
Keywords: unreal, engine, c++, uinterface, interface, linetrace, ue4, example, tutorial
Id: I7z8gVgM9rE
Channel Id: undefined
Length: 33min 41sec (2021 seconds)
Published: Thu May 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.