unreal engine 4 tutorialintro to programming â hello and welcome. this video is going to be an introduction to unreal engine 4 code, sort of a crash course for coding, if you will. we're going to go through the process of starting a new project. we'll implement a simple feature, and maybe show a few interesting other things along the way. i'm going to assume that you have installedvisual studio, or xcode beforehand,
that you've downloaded the unreal engine 4 source from github, followed all of the directions there, and managed to successfully build the engine once. i'm also going to show everything on a pc, but, if you're on a mac, you should be able to follow along just fine as well. if you do want to follow along, and you haven't done these steps, now would be a good time to pause and go do that. if not, let's get going! first thing i want to show is...
you already did this if you've built, but we have this "generateprojectfiles" batch file. this batch file will do what it says; it will generate the project files for you. so, this is handy to run from time to time if you're manually adding or removing source code files from your project. it's not required to compile or run or anything like that, but it will make your life a little bit easier sometimes to have those files show up in your ide. so, if you want to get them in your ide, that's a nice way to do that.
also, and this is going to be more of interest to visual studio users, we have a couple of things to make your lives easier. one is this ue4.natvis file. you can install it with this batch file right here. this will customize the display of some of our unreal specific common types in the debugger to make them easier to use. so you don't have to go digging through in your watch windows to find the values that you're interested in. so, i highly recommend installing that.
if you are a full version of visual studio 2013 user, this won't work for express, but this unrealvs plugin will give you a little toolbar, which is handy for a couple common workflows in unreal. so, i would definitely recommend installing that as well. and it has been... and i'll show this to you in a couple of minutes... crap. so, let's find our solution file and open it up.
okay. a couple things i want to point out here real fast... this drop-down right here is the platform drop-down. we'll stick with win64 for now, but, if you're going to build for another platform, that would be the place where you would choose that. here in the solution configuration drop-downs, typically you're going to want to build one of these editor configurations, because it builds in the editor and it's pretty useful
to have the editor for developing your product. so, the big difference between, debug, debuggame, and development here is you're going to be choosing a trade-off between run-time speed and debug ability, with development being the most highly optimized but the least debugable and debug being the least optimized but most easily debugable. i tend to stay in debug as much as possible, because i like being able to step through my code in trust with what the debugger's telling me.
if you're spending a lot of time working exclusively in your game code, and you're not stepping down into the engine very often, the debuggame configuration is very useful for those scenarios. it'll optimize all of the engine modules, but it will leave your game modules in debug mode, so you can debug what you want to debug and still get some good performance. also, i want to show, this toolbar right here, is the unrealvs toolbar, which i mentioned earlier.
so we have two key drop-downs here. this first one is the project, the starterproject. it's really a shortcut for picking a project out of here and saying, "set as startup project" so, instead of having the codedig through this panel over here, you can just kind of get a little short drop down for what you want. and this one right here is the command-line drop-down; it comes with a little history built into it. and that's really a shortcut for opening up your project settings
and saying, "here's my command-line arguments" you can do a lot with command-line arguments in unreal, and it's a very nice way to quickly set what you want to set without having to dig through a bunch of menus. okay, so, let's run our editor and i'll show you how to set up a new project. okay, so, since we didn't select any project, we're just kind of running the raw engine here. so it's going to ask us if we want to open or create a new project.
if you had some projects, you would see this, and you can pick a project you've already made. but, since we don't have one, let's make a new one. so here... let's... if you want to start completely clean, you can click blank, or basic code, and that basically gives you a completely clean slate to deal with. we also have several, several templates down here you can choose from. which are nice elevators if you want to, just, you kind of know,
vaguely what you want and want something to play with. so let's make a side scroller. i'm going to leave this box here checked, because i want to copy starter content into it, into the new project. so, this way, we have some toys in our toy box that we can play with. it's about lunch time, so let's make this project about pizza, because pizza is delicious. so, we're going to go ahead and create that. and you can see it's copying in the starter content.
it's going to go ahead and, basically, fully set up our project in a compilable state for us. here it is opened up, into visualstudio for us, and we should be ready to go. so, you can see, we still haveour engine code in our solution. here's our pizza game code. you can see what it gave us. it gave us a couple of files here. so, like, this is our precompiled header.
this is the module and the implementation. it's given us code for our basic character, so we'll have somebody that we can, so there's a guy that can run around. and it also gave us the implementation of a customized game mode, and a game mode is typically a set of rules that defines your game. in this case, it just tells us to, you know... let's use one of our pizza characters it made for us. so, let's go ahead and build this and see what it did for us. i'm actually going to stop this, because i changed my mind.
let's set this to debug editor so we can use our debugger. and also, i forgot to set this, so this is important. we want to actually build our pizza project, and you can see it altered the command line for us to load that up by default. so now let's build it. okay, so now we have our editor loaded up, and you can see we're editing our pizza project. if this is your first time in the editor, just a quick tour here.
so, over here, is our content browser. this is where all the content in the game is accessible. as you can see, we have quite a bit of stuff in here, and this is our starter contentthat we copied in. this over here is the scene outliner. in the 3d window, you can navigate around and click on things and select them, or you can select them through the scene outliner. it's a nice quick way if you don't want to try and pixel hunt sometimes.
you'll notice that, if you have something selected, it shows up here in the details panel with a lot of information about itthat you can edit, or see. one nice thing, when you're using code buildsis you'll notice this link up here. this will take us to the class definition of the object that you have selected. so, in this case, i have selected the playerstart.h, and i can click on the playerstart page. and here i'm on the playerstart.h,
and i can look and read the comments and see what properties there are. that can be a potentially useful feature for quickly navigating between the editor and the code. let's just go back to the editor real fast. also, i want to show this feature up here that we call play in editor. so, you can click on play and actually play your game, and play your level,right here in the viewport. so, as you can see, we have, you know, it's a side scroller. you can run around , you can jump, and you can jump again..
so, you know, about what you'd expect. so, let's make this a little more interesting. we called it pizza, so let'smake a powerup in our game here, like a pizza powerup that we can pick up. because collecting little floating things is a side scroller staple. so, first thing's first, we need to add a code module, or code class for our power up. so, let's make a base powerup class, and we'll go ahead and customize that once we get in the editor.
so, here in the file menu, we have this option to add code to project. this is a nice quick way to handle a lot of the unreal specific boilerplate. so, i click on that, and i can choose my parent class. and we have here a list of commonly used parent classes. if you want to see them all, click on this and you get the full tree of parentable classes. but, really what we want is something fairly simple, and we're going to choose "actor" because an actor is our most basic thing that exists in the world.
let's go ahead and pick actor, and click next, and we're going to call this a powerup. so, this will just be the base class for all of our powerups. and it's going to update our solution file, and our projects, and copy in the code files for us...and it's done. so, yes, we want to edit it. i'm going to go ahead and reload our environment and stop debugging. so, as you can see, we now have two new files -our powerup header and our powerup implementation file.
it's given us the class definition here, and we're ready to go. so, just sort of a quick tour around in unreal class, if you will, i'll check out one of these. as you can see, if you recall back to unreal engine 3, either we had a special language for defining classes and for scripting, and, in unreal engine 4, we're using straight, native c++. simply decorating the properties, and classes, and structures, and functions, that we think are interesting
and that we want the unreal object system to know about. so, in this case, our pizza character is a u class. this side view, camera component, is a u property, which means it'll show up in the editor and you'll get lots of other benefits. let's go back to our powerup class. so, thinking about how our powerup should behave, we want our little guy to run across, and touch it, and pick it up, right? so, we need something that we can touch. let's keep this simple.
we're going to have a property for a sphere. so, this is going to be our touch sphere. it's a sphere component. i've tagged this u property so that we can see it in the editor and we can edit its properties if we need to. now that we have this variable, let's go ahead and create it in our constructor. so, you can see, we've made it into our constructor already. and.. i'll just gonna..paste it in...okay.
so, you can see our touch sphere. we are going to create it. it is a sphere component. this is the owner of the...who owns the sub objects. so, that would be us, the powerup is going to own its own sub object. we're going to set its radius to 20; by default, that's 20 centimeters. we're going to set it as the root component, which means it's the... if you think of an actor as like a..
as being able to contain a hierarchy of physical components, little blocks you can assemble you know, this one's going to be root, so anything else we want to attach to this we can hang off of this sphere. but this will be the base of the hierarchy. so, to make it interesting, let's also have our powerup rotate to make it animated a little bit. so, let's give it a rotation rate, and i'm going to make this a u property also, so it's editable in the editor.
this will be in degrees per second. let's go ahead and initialize that in our constructor as well. so, we'll have it spin at 180 degrees per second. in order to actually do the updating, let's override the actor's tick function - this is the function that will get called every frame. and.. we can...implement it thusly. so, you can see, what we're doing here is, in our tick function... first order of business,
we're gonna go ahead and just call our super class's tick, so we don't want to miss anything important that it might be doing. then, in our code, we're going to get our current rotation into an f rotator. an f rotator is an unreal structure that contains the euler angles of orientation, so its pitch, yaw, and roll. and we're going to take its roll, and we're going to increment it by the amount of rotation we have done this frame.
and then go ahead and just set it back to ourselves. we're going to set our own rotation to the new, newly updated rotation. one more thing we need to do is that actor's do not tick by default, for efficiency reasons. so, we need to tell it that it is allowed to tick, like so. so, can it tick is true. looks good.
let's go ahead and build this, and see what we've done. okay, we're back in the editor. so, we've made our base class power up, but now we will make a specific powerup. so, we're going to use the blueprint system for this. a blueprint, or a class blueprint in this case, is essentially an editor defined sub-class of a native class or of another blueprint class. so, you can actually extend code using only the editor.
so, let's go ahead and demonstrate this. i'll go ahead and make one, and we'll call it, let's see... new blueprint...you can pick your parent class. since we want to make it a powerup, we're going to come down here to custom classses and then look for powerup.there it is. and now we have our blueprint. so, let's name it, pizzapowerup. okay, and we'll go ahead and edit that.
okay, just sort of a quick tour around the blueprint interface here... a couple different parts of a blueprint... you can think of this as sort of a visual representation of a code class. it's the same concept. we have a bunch of defaults, and these are the default values for the, you know, pizzapowerup,almost like a constructor, if you will. you can see the one we just added, rotationrate is here, and a bunch of other things you can set. so, these are the default properties of a pizzapowerup in your world.
the graph is where behavior code would go. you can set a...to respond to events every tick. we could have done our rotation here, if we wanted to, but we didn't. and here in the components, we can sort of... before, i was talking about the hierarchy of components, here we can sort of assemble that hierarchy visually, instead of typing coordinates and stuff and offsets in code. so, this is a really nice way to visually construct our actors. so, you can see, we created our touch sphere in code,
and here it is at the root of our hierarchy. so, let's give this a mesh, so we have something that we can see. i know in the shapes here we have a wedge shape, which is vaguely pizza-like. so, let's go ahead and add a static mesh component to this under the touch sphere, so it will be attached to the touch sphere. now, that looks awful big for pizza, maybe it's a deep dish, but i like new york style pizza.
so let's go ahead and scale this guy a little bit. there we go. now i'm going to rotate him so he kind of faces the camera by default. that looks vaguely pizza shaped. a little grey, but can't be picky. in the graph, let's go ahead and give it a simple behavior. so, let's say, when we touch it, we want it to just disappear just for now. just to test that it's working. we're going to use our overlap functionality.
so, if any actor overlaps this actor, which means overlaps the sphere in this case... so, when this happens, we will go ahead and just destroy the actor. just hit destroy. if you wanted to do anything interesting, like increase health or something, this would be a great place to do it. but we'll keep it simple for right now. one more thing i want to do...
let's go back to the components here. i'm going to turn off collision on the static mesh, because it's on by default. it's really just there to look pretty, since we're actually going to interact with the sphere. so, let's go ahead and turn that off, because i don't want to get stuck on it. i'll close this guy and go ahead and play and see what happens. well, nothing happens, because i didn't put one down. we'll go back to the blueprints real quick.
let's put one of these in the world. that would help, so we can see it. okay, that looks pretty good. all right. now, we'll play. there it is! look, it's rotating, and if i walk over and touch it, it goes away. beautiful!
first try.well, second try. one thing i don't like here is this is rotating... i wasn't really anticipating it to rotate like this. i wanted it to rotate like this, which is not easy to describe with my mouse cursor. let's try and change the axis rotation on this, to make it match with what i had in my head. so, let's go back to the code real quick, and you can see thatwe were rotating about the roll.
let's change that to the yaw axis. so, go ahead and save that, and go back...the code. so, this is a great opportunity to demonstrate our compile, reload, and compiling feature. so, now that i've changed my code,i can come up here to compile and say, "recompile the game code" it will kick off a compile of the game library, and reload it when it's finished, assuming there were no compile errors. this is a great feature
for being able to make little tweaks to the code without having to completelybring down and restart the editor. it doesn't work in all cases, like if you add new properties to a new class or change your u class constructors, or something like that it doesn't handle those cases, but, if you're just making tweaks to your code, like you messed up your matrix math or something like that... quick iterations...
this is a wonderful feature for quickly turning those kinds of things around. okay.so, our compile's finished. let's go ahead and hit play and see what happens. and there it is! it's rotating about the yaw axis now instead of the roll axis, just like we wanted. or just like i wanted, at least. i can still touch it, and it still works.
excellent.ship it. alright. if you want to find out more about coding in unreal, i highly recommend checking out some of the docs on the website and some of the other videos. there's a lot more information about the object model and different data structures that come with unreal. thanks for watching, and have fun!