Getting into C++ with Unreal Engine - Part2 - Blueprint Function Library

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again this video is the second part in a short series aimed at giving people who want to use C plus plus with the Unreal Engine a helping hand to get started in this part I go over creating an unreal project that uses C plus coding we take a brief look at some important project files and familiarize ourselves with the IDE I then work through a simple example using C plus to create a blueprint function Library allowing you to add your own custom blueprint nodes to your projects so hopefully now you've got the Unreal Engine installed plus the IDE of your choice or several of them if that's how you roll so it's time to create a c plus unreal project there's more than one way to get there but for this example I'm going to take you right from the Epic Launcher on the library tab you should see each version of the Unreal Engine that you have installed each one will have a button on it to launch that particular engine with a drop down to mess with the installed features useful if as I mentioned earlier you need to come back after you originally installed it to add support for specific platforms or whatever you can go ahead and click that launch button or alternatively in the top right corner there is a button that will let you launch the default version of your engine which is usually also the most recent one you could click that too the editor for your specified version will begin to load and as it is without a project it will display a page showing you all of your projects or allowing you to create a new one this will probably be familiar to you if you've already been working with unreal just using blueprints the difference now is that we're going to create a new project let's select that it will be C plus plus based don't worry though that won't stop you from using blueprints in any way blueprints are an essential part of unreal even when you use C plus and creating a c plus unreal project doesn't even mean you have to actually write any c plus if you don't want to crazy I know so we give our project its name and we choose the storage location for it hit the OK button and We're Off to the Races which depending on your specific hardware specs might be erased between a tortoise and a sloth and a garden snail be patient though an unreal will create your project structure for you configure some things Splash a few progress bars on the screen and if you're really lucky in won't even tell you that it's recompiling shaders that's its favorite it freaking loves that I swear you can sense it grinning every time that message appears So eventually your project will finish being massaged by the unreal toolset and emerge from the sauna fresh and glowing with its newborn Vitality cradled in the loving Embrace of the unreal editor which should have loaded what we'll also probably have loaded is your IDE if you installed more than one then it will be the default one which you can change in the settings of the unreal editor if it's not the one you actually want to use right now your project will have its own directory inside which will be a whole bunch of subdirectories and other files some of these directories and files will only exist under certain circumstances for example you will only have a plugins directory in your project if you are writing your own custom plugins for your project or if you're using Rider as your IDE which puts its own plugin code in there so that it can interface with the editor what you should have though is a directory with a DOT you project file and hopefully also a DOT sln file the dot U project file is rather unsurprisingly the unreal project file and the dot sln file is the visual studio solution file for working with your code switch to your IDE if if you have it open and if you don't have it open double click that dot sln file and the default one should open for You So eventually regardless of if you're using visual studio or Rider as your IDE you will have access to a view of your solution laid out as a tree structure which is generally referred to as the solution Explorer in Visual Studio or simply Explorer in Rider using this tree view you can navigate to the various source code files that make up your project and also the Unreal Engine source code itself take a moment to think about that because once you get used to it it means you can search through the code of the engine itself to learn how to use various classes of the framework or to see how epic have coded various things and you can learn an awful lot from It Anyway under the engine code folder there is the games folder which is where you will find an entry or your project inside that you can find the folders for your project source code and the source code for any code plugins which you are developing or have manually copied into your project structure that folder structure we're seeing in the Explorer isn't actually the real physical folder structure on the hardware disk for example The Source folder is a linked folder see that little arrow thingy on its icon it does point to a folder in your Project's directory structure but if you just go and add or remove files from the underlying folder don't expect your IDE to just pick up on that this can be a little Annoying to many people who have used C plus outside unreal because you might be used to Simply adding new source files to your projects from inside the IDE or from anywhere else when and however you please after all that's kind of what an IDE is basically about the problem is Unreal Engine projects are a bit more than just vanilla C plus plus projects and for a while I was kind of angry about not being able to work in exactly the manner I was used to with other types of projects but once you get used to it it honestly isn't so bad and eventually you come to understand why I pick set things up in the way they have and why they made many of the choices they did certain plugins or ID features can go a long way towards reducing friction with C plus plus development for unreal for example and at the risk of going on about Rider again too much Ryder does allow you to easily add new unreal objects such as classes and Strikes simply from the exploratory View and unlike the default experience in Visual Studio it all actually works because Ryder understands unreal projects and updates the solution file automatically hold up there a second pass voice over me you've been so wrapped up in using Rider you didn't notice that visual studio with the specified workloads installed into it now supports that kind of thing too if you right click on the source folder in your project and select add there is now a UE class option which brings up a selection of unreal templates I will say though it seems to take its sweet time about creating the files and updating the solution on my Dev machine I thought it crashed after nearly two minutes and it failed to include the copyright line from the project which is kind of an important deal for anything you want to put on Marketplace but oh well it is a step in the right direction I suppose and hopefully someone will fix it if you manage to delete the sln file for your project for some reason or if it's out of date or has gotten a bit confused or messed up because maybe you've been poking around directly with the source code files you can generated by right clicking on the you project file and selecting the generate Visual Studio project files option as long as the general structure of your project isn't completely fudged up it will rebuild the sln file using the latest project files this is often a lifesaver in cases where some careless change has rendered the solution inoperable trust me it can happen well I mean I was told it can happen obviously I've never poked around with those files manually and screwed things up myself so the way I generally work is by first opening the dot sln file either in Visual Studio or Rider I usually don't open the U project file directly unless I'm not going to be actually coding if I'm just working with assets bringing meshes in working with materials Etc then sure I run that new project otherwise I open the dot sln file in my IDE and I run the project from there I do this because it means I'm always running the latest build of my project and although the editor will detect newer source files and offer to rebuild them for you sometimes it can't usually because you left some half-baked nonsense in one of the source files you were working on and in that case unreal will tell you you have to build it yourself in the IDE anyway so I cut out that step and just run everything from the IDE there are also some other benefits to doing that unreal has a pretty great system for logging messages from your code you may have already seen those messages in the output window inside the unreal editor of course it's not going to help you if you just crash the editor but those messages can also be displayed in your IDE and you can still view them there even if you did just crash the editor which is quite useful and saves a bit of time and then of course there are the C plus plus debugging features which are not going to surprise anyone who has previous experience of C plus plus coding all the standard stuff is in there you can run your game set break points step into over and out of functions see the contents of memory and variables examine running threads blah blah blah blah unreal uses different build configurations and build targets to determine how the source code and other features of the project are handled when the project is built certain code that is useful for debugging is excluded in builds that are intended to be the final published shipped product likewise there are things you can have in your code that only affect the editor and will also be stripped out in a final published version the build Target determines if you are compiling for a standalone XC a project which will then open in the unreal editor or specific client and server targets for a networked game using the UE client server model once again the unreal documentation has a page which tells you all about these things and which ones you should be using why and when generally speaking in most cases you are going to want to start out with the development build configuration and the editor Target which are usually the default choices anyway defaults yay live coding live coding is a feature that is intended to allow you to Monkey around with your source code while the project is already running in the unreal editor we compile binaries and slip them in there without stopping and reloading the editor think of the film Raiders of the Lost Ark you know the thing with the little bag of sand and the idle head like that except in this case the little bag of sand is your new code and the idle head is the previous code you want to replace the general idea is that it makes iterative development faster iterative development in this case means when you don't actually know exactly how something should work and you just want to keep randomly changing your code until perhaps you stumble upon the solution the general point is that it should mean you can code faster because you don't have to keep stopping the editor and reloading an updated version of your project but personally I don't actually use it I don't disable it or anything it's on by default you just give a three-fingered salute Control Alt f11 on a PC to set it off but there are some things it won't do it won't update any changes you made to a Constructor of a UOB derived class in a CPP file why because of a whole thing to do with class default objects which I'll talk about more in a little while but if you change default values in a dot h file it does pick up on those there are a couple of other things it just doesn't like as well in general if you already have a well-defined class with all of its properties and methods written and you're just making minor changes to some code in a CPP file method then all is fine but the moment you start altering the structure of a class adding new properties changing a function to accept different parameters or things like that then don't expect too much from it again it kind of brings me back to that Raiders of the Lost Arc scene I mean with a little bag of sand and the idle I'm not saying if you use live coding it won't work just eventually you know Big Balls that's why I don't bother I might be missing out on a great time saver but I prefer to just stop the project running which closes the editor make whichever changes I need to my code recompile and if I didn't just write a bunch of errors and the code still builds run the project again it generally doesn't take all that long to relaunch the editor congratulations you got this far or perhaps you just skipped directly here no soup for you regardless this is where we're going to look at some practical examples we're going to use the project which we created earlier to try them out first a really simple one just to ease in anyone coming from a blueprint background okay let's get into it the first example is simply a basic way blueprinters can stick their toe into the C plus plus water it's a way to add a little of that C plus plus secret source to your otherwise totally blueprint projects and it can be a quite Gentle Way of moving into the world of C plus plus with unreal we're going to make a blueprint function library in C plus plus that will allow us to add new nodes that we can use in Blueprint programming and the great thing about it is that you won't need to know about all those classes and things I was previously describing to be able to do it so hopefully you created a new C plus unreal project as I described earlier make sure you have the project solution the sln file open in whichever IDE you're going to use I don't want to assume you how Rider so I will show you how you can add a new class from the unreal editor in the unreal editor if you have an open content browser window you can use that if you don't open up the quick content drawer in the tree structure for your Project's content find the C plus plus classes folder which by default will contain a single folder with the name of your project this is actually the name of the code module and it's the default primary one with the same name as the project you can see that there are a couple of classes in there already a character class and a game mode class right click in the empty space and select new C Plus Class A window appears allowing you to choose which class you want to inherit from to make your new class the window is divided into two either a list of the most commonly used classes or a tree view with basically everything in it because we are doing something common we can just use that common classes list and scroll down until we find the blueprint function Library click on it and then click the next button in this page we need to give a name to our new class remember how I tried to hammer into you those mandatory naming convention for certain types of classes well you don't use them when you use this thing you just give your classes the actual name you want them to have and The Wizard type Affair will prepend the appropriate capital letter on the front for you okay you don't need to change anything else on this page just a name the other options are for another time just click create class after you've chosen the name you want I'm going to keep it simple here and just call mine blueprint function Library a but in a real project you would likely want to give it a more useful name something descriptive relating to the type of functionality you intend to put in there so now we just have to wait well unreal does its thing creating new source code files and updating the sln file for us if you didn't already have your ID open at this point unreal will most likely open it for you so it can proudly display the new file it just created while wagging its tail and gazing up lovingly at you for approval who's a good unreal is it you yes uses well done so in our IDE we can now see our new shiny empty and not particular particularly useful blueprint Library class you should be looking at the header file dot h by the way you will note that it has unreal reflection macros u-class before the class declaration and generated body inside the class body but it's not actually doing anything for us yet so let's change that we're going to do three things here just to give you a little flavor for how you can boost your blueprints with C plus if you're new to C plus plus the way things usually work is that if you make a class you will have two separate files one with a dot h extension which is called the header and one with the dot CPP extension which is where your actual code goes the header file is generally used to define the overall shape of your class including what data properties it will contain what functions it will have and for those functions what parameters they will take and what values if any they will return and then the CPP file is used to contain the actual code that implements those functions I'm generalizing there are obviously exceptions to this but what I said is fine to get you started so let's try it out first with something kind of pointless but illustrative I'm going to add a new function called get pointlessmessage this function will return a text message which in this case is going to be a string value unreal C plus plus calls that type F string so you can see I have that right before the name of the function then there are some brackets and inside those you would list any parameters that your function needs in this case there are num so the brackets are empty now blueprint function libraries are not things we make instances of like objects or actors they are just well libraries of functions that you can run without having to have an instance of something so we have to say that the functions we make inside them are static static functions don't need an instance of the class to run now don't worry too much if you don't understand what that means at this point just remember that the functions in your blueprint class Library all need the word static in front of them and you'll be good to go for now so let's jump into the dot CPP file and create the code for this function we are not doing anything clever here just returning some hard-coded text if you compile and run everything as it stands you will be somewhat disappointed blueprints won't actually be able to see your function yet because we need to use another of those reflection macros first so back into our DOT header file we add right before the function a u function macro call inside its brackets we specify blueprint callable this tells the Unreal Engine That blueprints are allowed to use that function okay let's see if it works in Visual Studio I first checked to make sure development editor is the selected build configuration which is kind of difficult because it's all squished up here so I'm just going to set it again to be sure and then I just hit the green play button labeled local Windows debugger first Visual Studio builds all our code and as long as everything builds okay which it has it then launches the unreal editor with our newly compiled project inside it quickest way to test our blueprint function library is just to open the level blueprint on the default map so I'm going to do that I'll test our code from the begin play event so if you don't already have a node for that right click and search for it so let's find our new node we could look through the list to find the name of our blueprint function library and then see all the available nodes under it but it's much quicker to just do a search so I'll type pointless and there it is I can add it to the graph and hook it up to the execution pin coming from the begin play event let's take the return value and plug it into our trusty old friend print string node I'll make the duration a bit longer so we have a chance to read it and press the green play button to run play an editor and up comes our message well done remember to stretch properly before trying to Pat yourself on the back at this point I'm going to close the unreal editor and go back to our code the next quick example is going to demonstrate the usage of more of those specifiers which you can use inside the you function macro we're going to make a little function to return the value of pi I know I know there's already a node for pi it's just an example Anyway by using specifiers our version of Pi will be so much better than the regular one first up I'm going to add a private static constant double value to our class called Pi that's a bit of a mouthful and I'm going to give it the value of pi which I copied and pasted from the unreal code for the pi it already has because you can't have too much Pi am I right anyhow this private static value is only available to the code inside our class because it's private and what we're going to do is make a function that will simply return it to whatever calls our function so the function is defined as static double get my Pi it returns that double Precision value and it takes no parameters and the code for it is even simpler than for the pointless message function because we just defined that constant for pi all we have to do in the function code is say return pi and that's the function done which is great because it lets us focus on those specifiers I mentioned firstly we're going to need that same blueprint callable one we used before otherwise we can't call it from our blueprint next because this function doesn't change anything it's just returning a value I am going to add blueprint pure that will make it appear as a pure node in the graph you know those ones with no execution pins the next thing I'm going to add is the meta specifier nothing to do with zark or metaverses or anything like that it's basically for some extra specifiers which only affect the unreal editor in this case I'm going to add tooltip text and the thing which will make our High node better than all the others a compact node title compact node title makes the node smaller in size without a title bar or text on the pins and just has the text quite big right in the center I want our PI node to have the Greek Pi symbol on it and I have no idea what magical keys I need to press on my keyboard to do that so I quickly fire up Windows character map set the font to Roboto and search for pi there he is lowercase Pi copy that one and paste it into our compact node title hopefully we didn't make any mistakes in the code I hit the play button and visual studio starts compiling jump back into the level blueprint right click empty space and search on get my pie and there it is click it to insert and oh look at that cute little Pi node complete with its Greek symbol and if I hover the mouse over it the text I provided for the tool tip pops up home stretch how about we do something which actually does something I'm going to make a function which takes an array of numbers and calculates the mean modal and median value I know it's not very exciting but we are gradually building up some experience here so this function I want it to accept an array of integers and I want it to return three numbers generally speaking in C plus plus functions can only return one thing however we could make it so some of our parameters are considered output values but there is a nicer way I'm going to make a quick struct and we will return that from our function and because this is just a little struct which will only be used by this blueprint function Library I'm going to create it inside the same header we're already using right before our class definition so we add struct and we give it a name I'm using F averages the unreal naming convention is to use f whatever for this kind of thing so that's what I'm going with to make it a struct which we can use in blueprints we need to add one of those reflection macros this time it's u-struct and it needs blueprint type as a specifier the blueprint type means blueprints can use it also inside destruct we need a generated body macro call similar to what what went into our class with that basic framework set up I'm going to add three float properties for the three values I want to return and decorate each one with a U property macro I'm going to use the blueprint read-only specifier here because I want blueprints to be able to see these things but they don't have any need to change them the last thing I'll add is a couple of Constructors to simplify initializing these things a default parameter list Constructor where the values are all set to 0.0 and one where I can pass in three separate values because all I'm doing is initializing values I can just use C plus pluses initialization list syntax and I don't bother putting anything in the C plus because there isn't really any code as such with our struct made we can move back to the blueprint function library and Define the actual function so it's going to return one of those F averages structs we just made I'm going to call it calculate averages and it's going to take an array of integers as its only parameter I Mark that parameter const because we don't tend to modify the array that's passed in so now the actual code in the dot CPP file this is a little bit more involved than the previous examples but I've commented it and it's not beyond your ability to follow what's happening basically we're using the past in Array of numbers and calculating those three different types of statistical average from them once we have the results we stuff them into one of those structs we made and spit out the result back to the caller you don't have to always run the editor to make sure your code compile is okay in both visual studio and Rider pressing Ctrl and B will build your code and the build output window will show you if all is okay if not you will get some errors displayed and you can read them to try and figure out where you went wrong and then fix it everything builds okay excellent so I can just press that play button again and it will launch the unreal editor once more it doesn't even need to build a code because we just built it separately and it knows we've got the latest version once more we open the level blueprint this time right clicking and searching for calculate averages click to add it and there's our new node execution in and out pins an input pin for the array of integers parameter and an output pin for our struct result we can click on that and choose the split struct pin option to see the individual value properties so before we can test it we're going to need an array of integers I'll add a new variable I'll call it some values change the type to int and change the container to array compile and save the blueprint and now I can manually stuff some values into it for testing so now I'm going to hook everything up and add a nice append string node so we can format the results for displaying on the screen let's play and see and up comes our debug message showing us the three types of averages for the set of given values that was a fairly simple example of how to set up a blueprint function library in C plus to get new nodes that you can use with your unreal blueprints into the unreal editor this is a pretty nice way to ease yourself into C plus while still being productive with unreal using blueprints often the main reason blueprinters look at C plus plus is because there is something they want to do which has not been implemented as nodes using the examples we just saw as a starting point you can now search online for the answers to some of your needs and when you find c plus code is the only solution you may be able to wrap it in a blueprint Library function and get right back on with making your game I'm not saying it's the best way but it may help keep you productive the next video in this series on beginning c plus with unreal will take a deeper look at how to use all of those classes we looked at in the first part of this series you might need to be patient though I have a few other videos in the pipeline plus my own Game Dev projects if this sort of content is interesting or useful to you please go ahead and subscribe to the channel and turn on notifications if you'd like to know when I release new content and if you want to chat about anything covered or talk about Game Dev in general there's a link to my Discord server in the video description so until next time good luck with all your endeavors
Info
Channel: GGameDev
Views: 27,183
Rating: undefined out of 5
Keywords: ue5, unreal engine, c++, getting started
Id: _GQwpppMgiI
Channel Id: undefined
Length: 25min 8sec (1508 seconds)
Published: Sat Apr 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.