Learn Unreal Engine C++ In One Hour

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Thanks, man! PS Make a learn c++ in 5 seconds next time plz

👍︎︎ 77 👤︎︎ u/EddieMurphyIsTheBest 📅︎︎ Jan 30 2020 🗫︎ replies

Learn C++ in 1 Hour. Video length, 55 Minutes...

YOU DIRTY LIAR!!

Seriously though, thanks dude! this look great :D

👍︎︎ 16 👤︎︎ u/ColonelVirus 📅︎︎ Jan 30 2020 🗫︎ replies

I hope this helps a few people out! You should already know the basics of C++ prior to watching, this is more intended to teach the Unreal Engine specific stuff :)

Any questions feel free to PM me and i'll do my best to help.

👍︎︎ 37 👤︎︎ u/ReubenWard 📅︎︎ Jan 30 2020 🗫︎ replies

I started watching this to see how good it is. This was really informative and well done from a watching perspective. Gonna follow when i get time.

👍︎︎ 7 👤︎︎ u/Mr401blunts 📅︎︎ Jan 30 2020 🗫︎ replies

I learned the basics of c# api in unity in a little over 2 hours, and the grasping unity's logic in scripting was a blast. Learning UE4 C++ in less than half a year would be a miracle for me. It's a completely different animal. Just the macros and C++ verbosity alone are enough to break me. The blueprint/C++ code fragmentation. macro vs vanilla C++ logical fragmentation are just icings on the cake. If you need so many macros to make your logic usable by a human, might as well make the whole thing a macro or... use a different language. I'd be all over unreal engine if they replaced blueprints with c# for scripting purposes and limited blueprints as a "prefab" system, just like unity does.

This combined with documentation being split between c++ and blueprint and worse API documentation than unity's , unreal is strictly a team-based project engine to me

👍︎︎ 21 👤︎︎ u/[deleted] 📅︎︎ Jan 30 2020 🗫︎ replies

This is great, took me years to become adequate. Now you can come work with me on a vr project!

👍︎︎ 4 👤︎︎ u/RuptureGames 📅︎︎ Jan 30 2020 🗫︎ replies

This was super helpful to me. Just joined your Patreon. Downloading the Survival course now. : D

👍︎︎ 4 👤︎︎ u/MetalNode 📅︎︎ Jan 30 2020 🗫︎ replies

Sounds promising

👍︎︎ 3 👤︎︎ u/a_grigorov 📅︎︎ Jan 30 2020 🗫︎ replies

I'm not really sure what the intended audience for this video is. It would be too confusing to someone who's never coded in c++ before and too simplistic/slow for someone who has. You don't really cover anything in any particular order, rather going over a few disconnected topics in seemingly random order (Having one of the very first things you teach be input delegates, followed by actor component initialization is very strange. . . and then you throw in a short, mostly unexplained reference to network models out of left field.)

This is essentially a one hour sample/advertisement for your course, because nobody watching this would walk away with any significant understanding of C++ or Unreal, which hopefully prompts them to pay you money. That's is all good and fine, but you made your video title completely misleading, not to mention you never comment in this subreddit except to self-promote your stuff. I hope someone maybe can glean some helpful advice from this video, but your presenting in a terribly disingenuous way that leaves a bad taste in my mouth.

👍︎︎ 3 👤︎︎ u/demonwing 📅︎︎ Jan 31 2020 🗫︎ replies
Captions
before I start the video I made a poll about making a C++ survival game course and it got a really good response so that is now available on patreon I'll show you how to make a networked inventory system a weapon system we make steam matchmaking and Linux dedicated servers we make a modular clothing system everything you'd expect an apology aim become a patreon you'll get instant access to this course the link is in the description and enjoy the video ok before the video starts I am using Unreal Engine version 4.2 3 and I went to new project C++ third-person and then the name of the project is called learning I would recommend doing everything the exact same so that during this video you have maximum compatibility otherwise some stuff and your project might vary from what I'm showing you ok enjoy the video okay so I'm gonna be rushing because we have a lot of stuff to get through so let's start with actors and components now if you've done a little bit of Unreal Engine you're probably going to be falling asleep during this part but we have to talk about actors and components so an actor is kind of anything that exists in the level you can see that there are different actors that we can click on and the world outliner here holds all of the actors that are in our level you can spawn actors in so if I shot a gun I could spawn a bullet actor in and then there's also actors that are just placed in the level so you can both spawn actors in and place actors in the levels so here are some of the actors that are placed in the level you can see here my player is actually an actor and so these are some examples of actors actors are made up of components so if I click on this cube here you can see that there is a static mesh component so this is an actor that literally has one component and that is a mesh component but if we look at the character the character is actually a little bit more interesting so I'm going to click on the character here and open up the editor for it and you'll notice that it's built up out of components but it uses more components because there's a lot more that needs to be going on here so for example the capsule component is the component that we use to make sure that the character doesn't just walk through walls the mesh component is the actual part of the character we can see the camera boom is this is a spring arm component is a special type of component that tries to not let something go through a wall which is perfect cuz their camera is attached to it and we don't want to act camera to be able to go through walls so we use this thing called the spring arm component so a lot of what you're doing in Unreal Engine is you're making actors and you're building them up out of components if you wanted to make a gun you might make an actor called gun and that actor might have a mesh component for the actual the pistol mesh or the rifle mesh or whatever it might have some particles so a particle component that that does the gun shot you might have an audio component that plays the gunshot sounds and so you you come up with what you want to make and then you generally make an actor and then you you sort of build it up out of components to achieve the functionality that you want and that's exactly what we've done here with this character okay so let's talk about actually building your game so you might have a template like this and you might want to add to your game add a gun add some chick points add vehicles that you can get in I don't know whatever you want to do right so because unreal is kind of a framework we want to sort of utilize the framework and the way that we do that is by creating your own classes that extend Unreal Engine classes so let's talk about that so here's our character right now I suit earlier that this is an actor right it's an actor it exists in the level but it's actually a special type of actor called character so if you go to file new C++ class you can see that under the parent class there's actually a lot of different stuff here right you've got an actor which is you know just a simple object that can be placed in the level and then you've got something a little bit more specialized right you've got a pawn a pawn is an actor except we can place it in the level and it can receive input so now we're talking about an actor that you can move around with the controller or the WASD keys right okay well that's a little bit more functionality great well let's look at character now this is building even more on top of it so a character's a porn right so you can possess it you can receive input but now it's got even more stuff right so it's got like character movement it's got some basic networking it can walk it can jump it can fly it can swim so the idea is that instead of coding all of the stuff yourself the Unreal framework has already built all this stuff for you and you don't actually really need to do anything the character class already defines how you walk and fly and swim and then they just give you a lot of customization options if we go into the character here and click on character movement look at all these options we get for customizing the walking the jumping the falling the swimming flying we can customize all of this stuff that's already been made for us so at this point you're probably thinking well that's great right we can harness all this all the stuff that Epic Games has made for us and put in the engine right you can walk you can jump you can fall you can swim but this seems very rigid right it doesn't really seem like it's very customizable what if I want my player to be able to climb up stuff well epic games haven't made that so how do I make the ability to climb up stuff and the answer is that we subclass so what we do is we go to new C++ class right so think of it this way right we've got actor the porn is then an actor that can receive input the character is then a porn that can walk around and fly and swim and all that stuff and now we need to subclass this further so we click on character and now we're going to subclass and we're going to say my special character so just as if it games have built on the actor and made the porn and then made the character we're going to build further on what epic has already done and maybe create a climbing system or create you know further functionality and the way that we do that is through sub classing okay so now that we've gotten like a really basic introduction about actors and components done let's sort of talk about like how this even works right because when you've opened unreal for the first time and you hit play it's kind of hard to really get what's going on here like yeah I get that I'm playing this character and he's made up out of components and that's all great but like how does this work you know it's it's pretty cryptic and we could go into the Unreal Engine source code and look at every single line but it would take so long I think there's more value and giving you guys sort of a high level overview of what and to do that I think we need to talk about the game mode so if we go to edit project settings you'll see here that it says maps and modes and if you click on that you can see that there's this thing called the default game mode and one gets created for you automatically and we'll have a look at that in a little bit but let's just go down and you can see that there's some things that the game mode actually defines right so the game mode defines what character we play what was the porn that gets spawned in and in our case the porn that we play by default is this guy and actually we can delete him from the level and hit play and we'll still play as this guy he still gets spawned into the level and that's because the game mode is defining that option it's telling us to spawn them with that player there are some other things like the HUD class which defines you know the the UI that gets drawn on our screen the player controller which we're about to talk about the game state and the player state as well are defined in here let's talk about the the game mode as well so so yeah the game mode defines like who we players and things like that but the best explanation that I can give you is the game mode defines the rules for our game right so what happens when you start playing the game if your character dies how do we respawn your player things like that are defined in the game mode the game state is not so much defining the rules but defining the current state of the game so the game state is responsible for how many kills does each player have how long until the next round is over stuff like that is defined in the game state the player state defines information about our ping whatare username is stuff to do with our own players state and the last and most important thing that I want to mention is the player controller so the thing about the player controller every player gets a player controller and the player controller takes control of a pawn so the porn is the it's this guy the thing we can see but the thing that is controlling my character now is the player controller so the player controller you can't actually see it however it possesses pawns and allows them to walk around and that's what Unreal Engine calls them I don't know why they call it possessed but they do so basically our player controller is created when I hit play the player controller then possesses this pawn and allows me to walk around and this player is coded in such a way that when I press the WASD keys it will then add input let's show you how all that is actually done so if we look at Visual Studio here we have our learning character and our game mode these are classes that have been created and we'll probably go and create our own custom one but I think because we're short on time I really just want to get through this quickly and if we have some time at the end maybe we could create some stuff but let's look at the character so I told you before that the player controller will take control it will possess our character and then allow us to play it but that doesn't really answer the question of when I press WASD on my keyboard why does the character run around how has that been set up so let's talk about how that works Unreal Engine has an input system if you go to edit project settings and you go down to input down here you can define two types of mappings there's something called an axes mapping and there's something called an action mapping so in action mapping is for one-off behavior like jumping shooting and X's mappings are for things that have kind of a scale so move forward W is 1 and s is negative 1 so there's kind of a scale to moving forward or there's a scale to turning we use the the mouse for doing that but when you jump there's no scale you just press the spacebar once and then the character jumps so when you create this project it comes with the default character with the template and there's some code that comes written in here for you that's the whole point of a template is that you know there's already a bit of code here for you and but notice that its subclasses the character class and an important thing is that in the character classes defined this function called sit-up player and put component again we could look at the source code and figure out how everything works but this is where you actually sit up the input so you can bind actions and bind X's so let's talk about that so let's start with like how does the character move around when I press WASD X I think we can learn quite a lot from looking at there so we use something called blind X's and we give it the name of the axis and Unreal Engine will look for this axis that we've defined and then it will call this function called move forward ok so the move forward function has a signature where it takes this value and then what we're doing here is we're figuring out which direction is forward and then we're applying movement input and I think that someone gets throws a lot of Unreal Engine beginners off is they see stuff like this and they get a little bit sort of scared like what is this even doing right we're calling this function called add movement input and we're giving it the value but what is this even doing right like it's it can be started confusing the good news is that Unreal Engine is open source so you can look exactly at what this is doing so remember how I said that the learning character comes with a movement component well let's have a look at this function we're going to go to definition and you can see exactly what Unreal Engine is doing when we call this function right it's going to grab our movement component component and then it's going to add an input vector to the movement component and then the movement component is going to have the in handle movement so that's all that we're doing here with our input we're simply binding two functions and then these functions might do something if you were making a shooter game you would create an action called shoot and then you would bind it to a function and that function might spawn an a bullet and it might deal some damage to someone who's in your across here and so on okay so now let's talk about how the game is implemented so Unreal Engine is a collection of these things called modules and in our project here if you go to ue4 go to source and then you can look at like for example the editor all of these things here are modules so the editor itself is literally completely made out modules like the blueprint compiler this is it here they call it kismet because that was the old name for blueprints but like this is literally it right here you know the editor style that's a module it's right here so because unreal serpents or so you can literally go in and have a look at this stuff like here's the AI graph tool all of these things are simply just modules and you can go and look at these modules as you want and then the other thing to know is that your game itself is actually a module which is kind of interesting so we have this learning project here but if you open up learning dot you project you can see that it actually has a module section and there is a module defined called learning and it is a runtime module and it also has a loading phase and this is their game right this is literally the the module of our game and if you open up the learning edge and learning dot cpp you can see that there's literally one line here called implement primary game module and this is like the line that's basically registering your game module you will probably never need to touch any of this like module code unless you're writing like plugins but it's kind of nice to know how this all works let's then go on to talk about the build file so the build file defines how your game gets built there's a ton of things that you can do in the build file I'm not going to go particularly in-depth but you can see here we're defining some of the modules that our games actually going to use right so there's an engine module which defines a lot of the Unreal Engine stuff that we're going to be using right there's something called input core which I would assume defines like the input component and stuff like that so obviously we're going to need that module as well we're also using the head-mounted display module which allows us to have a VR game and so you're going to use a ton of modules and when you're making a plug-in or you're using someone else's plug-in that will almost always be made as a module so the idea that everything is just a module is kind of something it's nice to know as well the last thing that I think throws new people off when they're learning is what these target files do and I thought I just explained them really briefly so the target files kind of explain how your game should be built per target so there are different types of targets right you might be targeting your build for a server or a client so the dedicated server is going to run a different game than the clients going to run right there are some things that are going to be customized maybe the server needs to have some steam information on it whereas the client doesn't maybe when you're building for Xbox you want to define some extra information in there so that's kind of what these target files allow you to do and as someone who's like beginning to learn say boss boss you're probably not gonna touch these target files and you're probably not gonna deal with modules very often but it's always nice to just know how that works as well okay this is another thing that throws a lot of people off that are beginners to Unreal Engine C++ is these Heeter these midi tags here so there's something called the unreal hit at all and when you compile your project the Unreal hit at all is going to look for all of these new properties and you classes and new functions and there's some other ones as well and then from there it'll be able to store all this information and give it to the editor and that allows the editor to let us change values like this in unity you would put public in front of us and then unity would would go through and let you change it that way and Unreal Engine it's a lot more customizable so you would declare it as a you property and then there are different options like visible anywhere blueprint read-only things like that and you can give it categories as well and that will sort of define how the editor treats it and a cool thing is again Unreal Engine is completely open-source right so if you right-click on your property and go to the definition you can see all of the different meta tags here and if you scroll down you can see here are the keywords for the you class macro if you scroll down you've got like the you function keywords you've got you property keywords and it's really awesome because you've got descriptions of what all this stuff does right so if you're inside of learning character dot H for example and you see this key word here visible anywhere and you go I have no idea what that means you can just go in here and just look for visible anywhere and here you go it's got a comment indicates that this proper is visible in property windows but cannot be edited at all and you can see that this is true if we actually go into the character and have a look at the base turn rate this value is visible but we can't change it and the reason that you might do something like that is because you might want designers to be able to see what this value is but you might not want designers to be able to change this value because it might be quite important that they're not allowed to change it so in C++ you're writing a lot of code and functionality and then you're sort of exposing options for designers to actually customize so if I was to change this to eat it anywhere now that would let any designer change this value if they wanted to so it's up to you as the C++ programmer what are you gonna let designers change and tinker with and what are you gonna lock down what are you gonna say hey we can only change this value in C++ I don't want any designers to be able to mess with this that is what you are exposing with these meta tags that is the importance of these meta tags okay so let's get a bit more practical I talked earlier about how if you want to create something what you do is you make an actor and then you choose the components that you need to build the functionality for your actor so how would we go about doing that and C++ well the first thing we would we would want to do is we'd want to think about what sort of actor do we want so if the actor needs to be playable if we need to be able to control the actor and walk around the level we'd want to make a pawn and if it was an upright character like a humanoid sort of a character then we would probably want to use the character class but if director was maybe a bullet that gets shot out of a gun then we would probably want to use actor instead so there's different parent classes that define different functionality let's create a bullet by the way because I think a bullet would be fun to do so we're gonna select actor as their base class and then we'll go to next and we need to create a name for the actor I'm just gonna call it bullet I think that would work fine and then go ahead and click on create class a good habit to get into the practice of even if you're a C++ developer is looking at blueprints and proto typing and blueprints so I might decide to prototype my bullet and blueprints before I go on to create it in C++ and in fact blueprints work for a lot of things C++ is great for performance but sometimes you really don't even need to use C++ so I thought we'd prototype our bullet before we just go ahead and code it so again we make a this time I'm going to make a blueprint class so just right-click make an actor and I'm gonna call those BP underscore bullet so this is the exact same thing I'm about to do and save us pause I'm just gonna do it in blueprints first so need to select the components to build the functionality that we want so the bullet I'm gonna decide that my bullet is going to be a ball so we need some sort of mesh so if we add a component here all of the miche's that exists or all of the components sorry and we want something called a static mesh this is a mesh that doesn't move there are other things like a skeletal mesh that's a mesh that can animate but we don't want that we just want a static ball that's going to get shot across the level so we'll go ahead and click on that and you need to name your components naming of components is really important because other people need to know what this component is for so I'm gonna call it bullet mesh because anyone that reads that is going to go oh that makes sense it's the mesh of the bullet perfect there is also a component that comes with the actor by default I'm gonna drag the bullet mesh onto that and make that the new default one so components have a bunch of properties that exists within them and in the case of the static mesh component there is a static mesh property that we can choose I'm gonna search for sphere and I'm just gonna click on I'm gonna click on shape sphere so we'll just click on that one ok great so we have a sphere we've made the mesh for the bullet the next thing to think about is while the bullet needs to be shot across the level so there are two options we can create a component that will shoot this ball across the level and that's very possible you could totally do that but the thing is Unreal Engine has thought of this problem a lot of people need to do this and so Unreal Engine already has a component that will do this for you if you go to add component and type in projectile movement click on that and that is going to shoot our ball across the level let's drop our bullet into the level let's go to the details panel and we'll click on the projectile movement and we're going to search for speed and there's initial speed and max speed let's sit both of those to be like 3000 and then go down to where it says simulate and click on simulate and notice that when I hit simulate the ball gets shot across the level so notice how simple that was like we didn't even write code we literally just added two components and now we have a bullet so it's super easy to do in blueprints of course but this is a C++ video so how do we do what we just did in blueprints in C++ well we're gonna go back to visual studio I'm gonna close this and I'll show you exactly how to do the same thing but in C++ okay so we're gonna start off with this problem of how do we add a component in C++ so in blueprints we just click a button and that's it it's done well in C++ we have to use something called create default sub object and there is such a bad name like I wish it was called create component because it would be so much easier for people to understand but for some reason it is called create default sub object and a sub object you can create things that aren't components as well with this function but this is the function that you use to create a component so again I don't know why it's called this it's so confusing but anyways so this is a function that actually is a template function so in these brackets here we're going to pass in the type of component that we want to create so in our case we want to make something called a static mesh component but instead of clicking the add component button we use this function and then we pass you static mesh component to it and then you simply just type the name and here so we would say bullet mesh and so when we run this code this is going to give us the same effect but just in C++ the problem with this approach is that it kind of sucks right like in the editor it's so easy you click the add component button if you want to change the static mesh there's a handy little drop-down menu but it's really confusing to do this in C++ if I want to set the static mesh I have to do set static mesh and then I need to get a reference to a mesh which is really confusing to do and see bus pass so this is why I urge people who want to learn C bus pass learn to use blueprints as well because the two really do have to work together and you really want to use the editor as well as your code so I'm gonna show you the best approach to using these two things together so what I've just done is I've created a static mesh the problem is that I can't edit that static mesh inside of the editor how do we do that well we need to go back to this thing that I talked about before the you property so to gain the ability to edit the static mesh in the editor which is my goal because I don't want to have to use C++ to modify this this component it is such a pain to do that you really want to expose it to the editor so let's talk about how you would do that it's actually really simple we'll just go anywhere in our hit a file I'm just gonna put it here and we're gonna make a you static mesh component a pointer and I'm gonna call that bullet mesh and because we want to edit this in the editor we mark it as a u property and we're essentially just saying hey in today you need to know about this the unreal header tool is going to go through it's gonna see that we've put this here and it's gonna tell the editor hey you need to know about this bullet mesh I want you to be able to do stuff with this doing this alone is not enough though we actually need to say edit anywhere we want to be able to edit the bullet mesh anywhere if we wanted it to just show up we could do visible anywhere but then we wouldn't be able to edit it so I'm gonna do edit anywhere and then we're going to give it a category as well and the category I'm gonna give it I'm just gonna say components right because this is one of the components that makes up our bullet okay cool so coming back to create default sub object what that's going to do is it's going to create a static mesh component and then it's going to return it to us so all we have to do now is just store it in this variable that we just made so we just say bullet mesh equals create default sub-object easy done now because this is C++ we do need to include the header file for static mesh component you may need to google this if you don't know where it is but in my case I know that it's in the components folder and it's static mesh component dot H so you just include the header file for that the last problem you're going to have is that you can't compile this code because the header file is over here and the compiler is going to look at this and say I don't know what this is because the header files in here it's not in here so we could add the header file in here but that's actually bad practice you don't want to do that what we're gonna do is forward declare it by just saying class and it just tells the compiler don't worry about looking for this yet because we will define this in here I promise but just ignore it for now okay that's that's the best way of explaining it that I know it sounds stupid but just put class there and then simply click local windows debugger and it should be good to go you will want to make sure that you have development editor and 164 selected and you will have also needed to right-click on this and set it as your startup project if you haven't done so you can just click on set as startup project as well so let's see what we've just done with our lovely C++ code we just wrote if we go to C++ classes and drag our bullet into the level what we have is a bullet actor so congratulations we've created a C++ actor and you can see the bullet mesh that we created is here and because we did edit anywhere it actually shows up and we can change different stuff about it and in our case what we actually want to do is go to static mesh and we're just going to do the same thing that we did before sphere and click on shape sphere and there we go so now we have the same thing just in C++ the one thing I didn't create in C++ yet is the projectile movement component so if I hit play it doesn't go anywhere but you can see that this is like a starting point we've created a component one thing that I really want to stress is that blueprints are not evil like you are going to want to use blueprints because the blue today is great it really is it's awesome so here's the thing right if I just spawn a bullet in when I shoot it's not gonna have the mesh sit up right like it's not sit up properly if I just spawn listen and you can see this is nothing so I'm gonna be shooting nothing I want to shoot these things right so the really good way to design your game is to design the functionality in C++ and then use blueprints to actually set up values so if you right-click on our bullet that we just made in C++ and create a blueprint class based on it I'm just gonna call this BP underscore my bullet and I'll put it in the content folder so now we're modifying our C++ class but we're kind of making a prefab right we're making something that we can come back to and change the values on I'll show you if we click on bullet Mich let's just do what we did before and we'll hit compile and save and we're gonna take the my bullet and just drag it into the level so this is Air C++ class but now when we spawn it into the level it's actually set up it's ready to go so you want to kind of create functionality in C++ and then use the blueprint editor to change some of the defaults so that you can just drag as many of them into the level as you want I'm just gonna delete that blueprint one that I made by the way we're not gonna need that anymore so this is purely C++ we're just creating a blueprint because it's nice to be able to change values so while we're at it I thought we'd just finish that off the thing that we were working on so we're going to go into a constructor and we're going to create that projectile movement component in C++ so again this is just practice to help nail this idea down so we want to use create default sub object to make the component it is called the projectile movement component you need to give it a name in our case we're going to call it bullet we'll just call it bullet movement and remember that we need to store this in a variable so we're going to create a variable for that now so we'll just go whoops I'm gonna say bullet moves meant and we're going to expose it to the editor using our you property and then finally store that in the constructor one thing you're also going to want to get into the habit of doing is sitting the root component so actors are made up of a bunch of different components but there is a root component right and then other components attached to that component so you can build hierarchies of components that sort of attach to each other and in our case I think it makes sense to have the bullet mesh be the root component so to do that you just call this function set root component and we want to set the bullet mesh to be the root component let's go ahead and run the local windows debugger and we'll actually have a look at this functionality as well so again we're gonna have to include the header file in our case we need to get a game framework slash projectile movement component you'll understand where all the header files are as you do more coding this is kind of just memory like as you code more stuff you'll just remember where these header files are but of course you can always just Google and the documentation will tell you where the header files are as well so let's run another debugger session and I'll show you what the result of that is now so let's go to the content folder and open up the BP my bullet that we made before and we'll open the full blueprint editor and check it out so we have the bullet mesh that we created and we've set it to be the sphere and we also have this new component called bullet and movement and if I click the drop down here there are a bunch of different categories but projectile is what we're going to look at and you can see that they're the initial speed is zero and the max speed is zero so if we drop this in to the level it doesn't do anything but like I said the blueprint is the perfect place to actually set these values up so I'm just gonna set this to five thousand and five thousand we'll hit compile hit save and now when you drag this in to the level it's already set up it's ready to go it's got the the ball it's got the values sit for five thousand if you have a look here you can see these are set to be five thousand and if at play you can see that they shoot across the level just fine lastly I kind of want to talk about best practices when it comes to these components so while blueprints are nice for changing values if you can change a value easily in C++ you probably should so sit the bullet mesh to be a sphere is actually really annoying to do from C++ it's way better to do that sort of thing in blueprints but in C++ it's really easy to set the initial speed of the bullet movement in fact all we need to do is bullet movement initial speed equals 5,000 and then bullet movement max speed equals 5,000 so that's actually really easy to set up for in C++ and you probably should sit it up in C++ because if any other C++ developers need to read your code they can look at your code they can see these values and it just makes sense otherwise they're gonna have to go open up your blueprint and look at that instead so the general rule if it's easy to do do it in C++ and if it's more difficult and as better to do in the blueprint editor then you want to set it up there instead so we've placed our bullet in the level but let's talk about spawning something and how do we spawn the bullet into the level let's talk about how to spawn an actor at runtime now and specifically I would like to spawn the bullet in when you click the mouse button so we can actually shoot them by clicking the left mouse button so let's have a little look at the input system and we'll do that now let's go to project settings we'll go down to input and we're going to add an action mapping called shoot and we'll make it the left mouse button and let's actually set this up so that when we click the left mouse button we spawn one of these guys in and then it shoots across the level let's try it now so if you come into Visual Studio and open up learning character dot CPP remember we were looking at the set up player and put component and this is where you set up all the input so let's actually set up an input that allows us to shoot so we're going to grab the player input component we're going to bind an action and the action we want to bind shoot that's what we named it remember so you have to make sure that the name is correct and then you have these things called input events so in our case when we press it we want to call the function there's other ones like when you release it or when you double tap the particular key but in our case we just want when we press the left mouse button then you need to tell the input component what function it needs to call when we press the left mouse button and we haven't actually made a function yet so let's make a function so in the section here we'll just we'll just put it here why not and I'm gonna call it void shoot and then we can right click and go to quick actions and create a definition so what we're gonna do is we're gonna say we want to call the learning character shoot function when you press the left mouse button so now when you press the left mouse button the input component is going to call this function for us all right so I feel like we're getting slightly ahead of ourselves so I want to show you a little bit more of a simple concept before we start spawning stuff in I just want to show you how to log because logging is actually really useful so we've got our shared function here and the shoot function is going to be called when we press our left mouse button because we set it up to do that so let's actually just log something when this function is called so an Unreal Engine if you want to send something to the log you just do Yui log and then there are some things here so this category name you can define your own categories and there are some categories that exist already one of those is called log temp and I'd recommend using that one there is also the verbosity so you can give it era which will show up and read you can say warning which will show up in yellow so there are different verbosity as well I like to use warning because it actually shows up in a yellow text and then finally you need to give it the the ticks to print out so we're gonna say text and this will define some text and we're gonna put some text in here we'll just say bang bang now I'm just gonna save and then run the debugger and I'll actually show you how to view logs in the editor okay so I'm going to click this drop down and I'll just play in the selected viewport let me show you how to do logs so we'll go to output log here this should show up by default but if it doesn't you'll want to go to window developer tools and then click on the output log so when I hit play watch when I click the left mouse button it says bang bang in the log if you want to know more about logging I'd recommend googling unreal engine logging and there's a really good page on the documentation where they sort of go into more depth about how logging works because you'll be using logging a fair bit so it's nice to learn so let's talk about how to spawn something in so to spawn something in we need to get a reference to the world so the world is where the level exists it's where all the actors and the level exists the world is kind of the big thing that holds it all and you can have multiple worlds but generally there's just going to be one world and so what we're going to do is we're going to get the world now because the character is something that exists in the world you can actually just call get world so any actors can call this function called get world and that will get the world and then at this point the world has a function called spawn actor so we're going to talk about how to spawn an actor the first thing to know is that it is a template function so you want to tell it what type of actor to spawn in in an air case it is called bullet so we want to spawn the bullet notice the bullet has an a before it by the way that's because it's an actor and you also notice their character has an a before it that's because it's also an actor so the a is just a nice way to visualize and show people hey this is an actor okay so when you're spawning something in it actually needs to know some information the first thing needs to know is like what type of bullet are we spawning in we could spawn in the default bullet but you remember we actually wanted to spawn our blueprint bullet you remember that we blue printed the bullet and then we set up some values we want to spawn that bulletin so what we really want to do is spawn the blueprint and we don't just want to spawn this in because remember this doesn't really have any stuff set up it just has that set up but it doesn't have the mesh or anything we want to spawn the blueprints so how do we do that coming back into learning character age I'll show you how to get a reference to a blueprint so we're gonna type T sub class of and then in brackets we're gonna say a bullet and again we're just using that forward declaration thing that I showed you and we're going to say this as the bullet BP and we want to be able to change this from within the editor and you remember how do we do that we use u property and we say edit anywhere and we're going to give it a category I'm just going to say that the category is bullet or we'll call it shooting it's in the shooting category and when we launch the editor I'll show you actually what this does but let's just go back to spawn actor and we're gonna give the spawn actor function our bullet blueprint that we want to spawn and the other thing that it wants to know is where do we spawn the actor you can see it's asking for a transform so a transform is just a location a rotation and a scale so let's make a transform and this is like where do we spawn the bulletin so we're gonna call this the spawn transform and if you think about it we kind of want to just spawn the ball at where our player is right so so let's get our actors transform we'll just use the transform of our actor and I kind of want the bullet to spawn above the player cuz if we just spawn up where our player is it's gonna like shoot out of the players body I'd rather that it's a little bit higher up so to do that I'm just gonna say spawn transform transform position and I'm gonna give it a if victor will just say 0 0 and then we want to spawn it a hundred units above our player okay so we're given it the bullet blueprint and finally we're just going to give it the spawn transform so we're spawning the bulletin where we are and we're giving it the bullet blueprint there's one more thing that we can actually give and that is the spawn parameters so spawn is not going to be the same every time and so on the spawn params like if you go spawn params here you can see that there's a bunch of functionality right so so like for example the spawn collision handling override so if the bullet is colliding with a wall do we want to adjust the location of it or do we want to you know always spawn it and how do we want to do that there's other stuff like the owner so the owner is the actor that spawned this actor so for example if I shot the bullet it would make sense to make me the owner and there's another thing called the instigator so this is the player that's responsible for damage done by the spawned actor so if I spawn a grenade I want to sit myself as the instigator of the grenade and that way we know whose grenade it is right so there's just a bunch of functionality for that I'm not gonna really touch any of this stuff because I just think it's it's a little bit too much detail for right now especially if you're new to Unreal Engine but feel free to play around with some of the spawn params so I'm gonna pass my spawn parameter and at this point I'm just gonna go ahead and save and we'll jump into the editor and I'll actually show you what this does one thing I forgot by the way is I'm spawning the bullet in but as always I need to include the heater file so I just want to come up to the top here and we're going to do include and we call that bullets so just include bullet dot H this guy over here so let me show you what I was talking about when I talked about that subclass stuff before so if you go to content and go to third-person CPP blueprints and then open our character up the the sub class off thing here here it is here so we made a category called shooting and in there we have the bullet BP and you can see that there's a drop down if I click the drop down the the phrase sub class off it's referring to any classes that are a subclass of bullet or in fact bullet itself so the only options that come up here are bullet or BP my bullet we can select one of those two and if we created more bullet blueprints then we could select those as well potentially so you could have different types of bullet let me show you actually let's let's go into content we'll duplicate my bullet and I'm gonna call this my bullet too and if we click on this bullet and then go to transform and set the scale to be twice as big now our game has two types of bullets right it has one type of bullet which is a little bit smaller and it has one type of bullet which is a little bit bigger and the cool thing about what we've done using that subclass of is that in the character we can select my bullet or perhaps we want to change our game up so that the bullets are a bit bigger well now we can just select my bullet too and now we're gonna be shooting really big bullets so we'll start off by selecting my bullet and we'll just jump into the level and it appears to spawn them underneath us I think I did something wrong hang on a sec okay so I don't want to spawn the bullet literally inside of my character I want to spawn the bullet slightly in front of my character so how would I do that so what we know is that the camera is pointing in the direction that we're looking at so if we get the rotation of the camera we can use that to get the point right in front of the player and then we can spawn the bullet just slightly in front of the player so there might be a little bit confusing but just bear with me here so here's our camera so what we're going to do is we're going to get the follow camera and we're going to get its rotation using getcomponent rotation and then we're going to say dot vector to make it a vector we're going to multiply it by say I don't know 200 so that it's 200 units in front of the player and then we simply add that to our players location so we do get actor location and finally we're going to say spawn transform dot set location and so what this is is just a little bit of vector maths and you might want to do some reading up on victim s if you're not super familiar with them but basically what this is allowing us to do is get the point that's 200 units in front of my player and that's where we're going to actually spawn the bullet in so let's go ahead and run the debugger okay guys so if we jump into the game you can see that I'm now shooting these balls the rotation is not matching my camera's rotation because we have not suited up to work like that it's simply going to shoot in the direction that my character is facing it and if we go into the character and change the ball to be the bullet too so let's go ahead and find oh we were actually shooting bullet two if we go my bullet you can see now the bullets are going to be smaller so we're using these blueprint classes to create different kinds of bullet as well that's the other power of blueprints as the the pallidus sort of define different values and create different effects this is how you would create different types of weapons different types of bullets different types of cars and so on okay so I thought for the the end of this video cuz we've only got like 14 minutes left I feel we'd talk briefly about networking because unreal is I feel like unreal is almost made for online games it is really good at handling networking and you don't really need to be very like clued up on how networking works per se because unreal has something called the replication system and so you basically just learn the replication system and the great thing is that unreal kind of networked by default so if you click this drop-down and select perhaps three players and then click on new editor window it's going to create three players here and you can see that you can run around and the games actually network so this lets you test networked games and actually make sure that they work properly and so this is what you again would look like if it's networked have a look though if I shoot a ball notice the other players don't actually see the balls that I'm shooting so if you create new features they're not going to be networked you need to develop your game with networking in mind you can't just add a networking it doesn't really work like that but things like the character movement are networked so when I run around other people see me run around and it is some other really cool tech built into it like being server authoritative and being predictive and some other things but let's go ahead and just really briefly talk about networking so notice the one I hit play it sees learning game preview server and then it's also got client 1 and client 2 you can see that each of the windows has a different thing so it's important to know that when you're playing an online game say there are 10 players in the game there's actually 10 separate instances of the game running right one on my computer one on platters computer player threes computer and so on and the goal of a networked game is to keep all of our games in sync we need to be seeing the same thing and that's what I mean by replication system we're replicating the same experience across multiple different machines notice that one of these players is the server though and the server is the person who we put in charge of the game so imagine a game where you have an inventory and you want to use an item well say that you're using a health potion right what you would do is you would tell the server this guy here you would tell him you want to use the potion and then the server's machine would actually process that request and then grant you some health we don't let clients drink the potion themselves because you can actually cheat right you can use software like cheat engine and basically hack and make it look like you've got health potions when you're done however if we put the server in charge the server should hopefully not be cheating right and this is why in some games like Call of Duty if you wanted to host a hectare Lobby you would need to be the server because only the server gets to grant stuff like that and this is called server authoritative so I'm on the server I'm shooting nothing happens if I go to the client and I shoot nothing happens let's talk about how replication works I'm gonna open up BP my bullet and there's actually a checkbox here called replicates so you need to set an actor as replicating it won't just do that by default and when you turn on replication that doesn't just do everything for you right it's not perfect but one thing that it will do is if the actor spawns something in and it's marked as replicates then it will show up on all the other clients too so let me show you that now so check it out when I'm on the server and I shoot you can see that it spawns the bullet in however if I go to the client to try to shoot notice that it does not do anything the client shoots his own bullet but none of the other players in the game see the bullet because remember the server is in control of spawning actors N and so if the server spawns a replicated actor this will notify all the other players in the game to also spawn that in but if me just a humble client if I just try spawn it in none of the other clients in the game received the information that I shot because I am NOT authoritative over the game the server is so what I need to do is if I want to shoot I need to tell the server that I've shot and let the server spawn the bullet in for me the whole reason we're doing this is because if we make the service spawn the bulletin then the server can check that we have enough ammo it can check that we actually hit another player and stuff like that the server needs to be in charge of the roles otherwise players can cheat so we've established that if you're a client and you try to shoot a bullet we don't shoot the bullet we need to tell the server to shoot the bullet so let me just give you a really quick run-through of how you'd actually do that so we're going to make a second function called server shoot and to make a function that is cold on the server what you do is you put you function and then you want to do server and then you can put reliable which means always call the function or if you do unreliable it will try to call the function but if it's really laggy it won't call it on the server because we need to send a network request to the server to make this happen in the case of shooting we want it to be reliable because we want to always shoot it doesn't matter how laggy it is we want to always attempt to shoot and I'm using visual studio visual assist sorry so if I click on this it's going to implement it for me but what you want to do is implement the function and then just add underscore implementation onto the end and so when we try to shoot air gun we're gonna check if we're the server and if we're not the server then we need to tell the server to call this function instead we don't want to spawn the bulletin we want the server to spawn the bulletin so the way that we check there is we say f roll is less than roll Authority so you've got roll authority roll autonomous proxies simulator proxy but the way to check if we not the server as we say is roll s the enroll authority and if that is true then that means we're a client and not the server if role is equal to role authority then we are in fact the server but in this case we want to check if we're not the server if we're not the server we want to actually say server should so tell the server to shoot for us and then we're just going to return because we don't want to do any of this but we just want the server to do that button so when the server calls this function what do we want the server to do we actually just want the server to call the shoot function so if we're a client then call this function this is gonna get cold on the server and now the server's gonna do this part for us instead of us doing it but if we were the server then we don't bother doing this we're already the server we just spawn the bullet straight in so this is kind of the idea of our pcs and replication and unreal I wish I had more time to explain this because it really is quite a difficult topic but let's jump back in and I'll show you that this should now work ok so check it out I'm the client and when I shoot it now shoots the bullet properly you can see that when I shoot I'm actually telling the server to spawn the bullet on the server then spawns the bullet in for me and because the bullets replicated everyone in the game can see it there is a lot more to networking than just this this is just a really basic example of networking and also note that if you were to ship this game and someone was really laggy and they went to shoot a bullet it's gonna take some time to tell the server to shoot the bullet so if you are really laggy you might click the mouse button to shoot and then it might only shoot the bullet one second later because you're on a slow connection so that's the SuperDuper basics of networking I could probably if we do a follow-up video I could explain more about networking but I think it's about an hour so I'm gonna leave it there guys thank you very much for watching and I'll see you in the next video once again a way to quickly mention my C++ survival game course it is now available on patreon the course comes with around $1,000 worth of assets alone with the modular character and weapon assets that you using your own games and the first 22 lectures are already up on patreon now click the link in the description to learn more
Info
Channel: reubs
Views: 211,522
Rating: undefined out of 5
Keywords: Unreal, Engine, C++, Tutorial, Beginner, Game, Programming
Id: nVm-DYdAsts
Channel Id: undefined
Length: 55min 43sec (3343 seconds)
Published: Thu Jan 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.