C++ Tips and Debugging in Unreal Engine 5

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video i'm going to cover some tips with working with c plus in unreal projects i'll cover how to get a new project up and running some of the common classes how to use the debugger and a few other topics along the way if you haven't worked with c plus before i recommend checking out some basic tutorials to understand how it works if you have used c plus plus before but not an unreal engine i recommend checking out unreal's c plus plus intro guide unreal has built a lot of functionality on top of the core c plus language for example they've added things like a reflection system and garbage collection i'm not going to go into too much detail about how to set up a new project for that i highly recommend this video from alex forsythe he goes into quite a bit of detail about how to create a new project and also shows you a workflow not using visual studio if you're into that i've been programming with c and c plus plus on a command line with unix and linux for most of my career so this workflow is pretty appealing but for now i'm going to give visual studio a try so let's get started run the unreal engine 5 editor and when the project browser comes up click on games and then blank we'll of course choose the c plus project and then for the target platform we're going to choose desktop we'll keep it at maximum quality and we're not going to include starter content the starter content has a bunch of assets you can use but we're going to be creating all our own assets instead i'm also not going to check ray tracing this is for hardware ray tracing support and my gpu doesn't support it now for the hardest part choosing a name this doesn't have to be the name of your final game it just has to be a name that you're going to use in your source code for this reason you probably want it nice and short and easy to type i'm going to be writing a multiplayer fighting game where your character is some kind of flying robot spacecraft so i'm going to use the name flybot for short now choose a place to create your project and hit the create button after a few moments visual studio will open up with your new project it's going to include all the engine source code as well you need this because you're going to be deriving a lot of your classes from that code it's also a very handy reference when you're trying to figure out how the engine works under the games folder you can see the new project and the files it generated for you to start up the editor with your new project go to debug and start without debugging or just press ctrl f5 this compiles the source code that was created and starts the editor in the main part of the editor you'll see the default level that was created for you in unreal projects you'll see both world and level used a world has at least one primary level called the persistent level but it can also contain sub levels you'll also see map or umap which is the asset type that levels are stored in worlds and levels are made up of actors actors aren't just a character but they can be anything that's placed inside the world for example it might be a rock or a light actors are also used to store things you can't see such as the state of your game on the right hand side in the world outliner you can see all the actors that have already been added i find it's useful to keep a few other tabs open in the editor such as the output log message log and project settings if we close the editor go back to visual studio and open flybot gamemodebase.h you'll see a gamemode class that was created for us there's not much to it yet but we'll be adding more in the future this is a really important class that ties a lot of pieces together for your game it derives from a gamemode base if we click on this and press f12 it'll jump right to the header file in the engine source where this is defined it has a good description of what this class is used for and you can see all the methods and properties it provides i find jumping into the engine source like this and reading comments in code is very useful you can read all the documentation you want but at the end of the day sometimes you just want to read the code to see exactly what it does you can easily rename files and classes if you need we don't really need the word base at the end of flybot gamemode base so let's remove it now that we're writing code one thing to consider is the copyright notice at the top if you never plan on sharing this code you don't have to worry about it since no one's ever going to see it i'm going to be sharing this on github so i want to put an open source license on it you have to be sure not to use a copyleft license with unreal engine code this is stated in their faq i'm going to use a header that says you can basically do whatever you want with the code you can also change the default header that's used in new files by going into the engine editor and going into project settings search for copyright and then update the default header here while we're here in the project settings let's also update our default game mode under maps and modes i'm going to use the custom flybot gamemode class i'm also going to save our untitled levels a new map called level and then set that as the default map inside of the maps and modes inside project settings we're now going to add a custom log category for our module this is useful when you're trying to filter the output log to see messages only from our module in flybot.h we declare the category name of this macro we're going to use the name log flybot then in logbot.cpp we define the log category with another macro let's add a method to our gamemode class to test it out we're going to add init game which overrides the parent class method this is called after the levels loaded but before any of the actors are really set up it's called with the name of the map as well as any options that are passed while loading the map in our nit game method we're going to call the parent method first and then simply log the map name and any options we have to include flybot.h to make sure the log category is defined press ctrl f5 to build it and run the editor and then inside the editor play the game hit escape to stop the game and then go to the output log if you search for log flybot you'll see the message was logged when a net game was run now let's take a look at some of the ways using a debugger can be useful instead of pressing ctrl f5 just press f5 this will start the editor again but this time it's going to be attached to the debugger inside visual studio once you're in your project editor go back to visual studio without closing it then press the blue pause button in the upper left hand corner if you go back to the editor you'll see that it doesn't respond to any input now this is because the process is frozen now you can go back into visual studio and see what it was doing if you click on threads you can see all the threads that were running in the process with the thread labeled game thread selected click on the call stack here you can see all the functions that were run to get to the point in time where the process was frozen double clicking on these function names takes you right to the source code where it's being run you can see it starts at win main which is the entry point for all windows programs then it calls the engine's guarded main function which does setup that's not specific to just windows then there's a series of tick functions eventually getting into a sleep function that we're currently stuck in to slowly walk over the code while it's running we can use the step into step over and step out operations step into runs the current line of code going into a function if there happens to be one step over runs the current line of code but does not go into the function step out runs the rest of the code in the current function i highly recommend using the shortcut keys mapped to f11 f10 and shift f11 for these walking through code like this while the engine's running is a great way to figure out how it works if you press the green continue button it'll keep on going where it left off now you can go back into the editor and it works as normal to see how a specific piece of code is running we can use breakpoints these will make the process and debugger stop when it hits that line of code we're going to set one on our gamemode class where it calls the init game parent function when you press f5 and run the editor and then press play you'll see it jumps back to visual studio stopping on that line of code if you click on the call stack you can see all the functions that got us to that breakpoint if you press on locals you can see all the variables currently defined in that function though this variable is the instance of the mode itself if you click on it you can see all the properties and their values that are currently set if we go back to our gamemode class and step into the init game parent function you can step through the code and see how it works at any point you can hover over a variable to see the value or go back to the locals tab to see the value of all the current variables once you're done using a breakpoint just right click on it and click delete breakpoint one more way the debugger can be useful is tracking down why your program is crashing we're going to add a null pointer to your reference here so our program crashes when we run the game once you run the editor under the debugger and press play you'll see it jumps back to visual studio and shows us where the bug happened an exception message pops up and sure enough it says we are trying to access memory zero which is the null pointer reference when debugging real bugs in a game sometimes it's hard to know how that state got there in those cases you can set a breakpoint earlier on and step through the code until the point where you actually hit the bug the last topic i want to talk about is putting your project under version source control this allows you to easily track changes as you make them as well as go back in time to test against previous versions it's also great for collaboration allowing you to work with others on different parts of the project at the same time it's also great for keeping a copy of it somewhere else as a backup the git version control system is by far the most popular and this is what unreal engine itself uses i'm going to be keeping a copy on github so i'm going to create a project there first i'll cover the basics of getting a project up on github but i suggest taking a closer look at the docs for both git and github once you click create repository just give it a name and a description you can either have it public or private i'm also going to have it create a readme file that we can fill in later once you click create repository it'll bring you to the new repository page and you'll see the readme file it created for us it also has a single branch called the main branch for early development this is all you're going to need but if you start creating version releases or start working with more people you'll probably start adding more branches to your project we'll now set up our project directory as a git repo we do this with a git init command now we'll connect it to the repo on github by using the get remote add command we use the url that we copied from the github repository page to do this you'll need an ssh key setup with github so check the documentation on how to do that with the two repos connected we can now run git fetch to pull down what's on github this is just the main branch with a readme file in it run git checkout main to start working on that main branch and then run git status to see all the files that it doesn't know about yet some of these folders and files we don't need to check in they're automatically generated from the other project files the things we do care about are config content the u project file and of course the source folder run git add to add these to the main branch if you run git status again you'll see all the files that we added you'll still see the folders we don't care about and you can ignore these by creating a git ignore file you can do this by going to windows explorer creating a new text document and renaming it to dot get ignore we'll add this to the visual studio solution so we can edit it there we'll copy and paste all the folders and files we don't care about from the get status command when we run git status again you can see they no longer show up of course now we see the dot get ignore file and we do want to track this so run git add.ignore as well now these files aren't added to the main branch quite yet they're just staged to be added to make it final you need to run git commit it's good practice to provide a message with our commit so we're going to use dash m and then our message between double quotes if you run git status again it no longer lists the files since they've been committed but it does let you know that your main branch is behind the one on github which is the origin branch if you run git push this will send all the changes to the github repo now you can go back to the project page on github and view the files there we now have it set up to easily make changes commit them to our local repo and then push these up to github i used github here but you could use any other git hosting service you could even just keep a clone of that repo on another hard drive the important thing is to keep it backed up somewhere while we've covered a good amount getting started with c plus and unreal there's still quite a bit more to cover beyond the video i mentioned earlier i highly recommend watching the other videos by alex forsyth i found the video covering the game framework really useful it's a great summary of the common classes you'll be using while working with unreal engine in future videos i'll definitely be diving a lot deeper into the c plus code but for the next video i'm going to be looking at the sky atmosphere and fog actor components i'll be customizing these to give the look and feel for the game's world if you have any questions be sure to ask in the comments below thanks for watching [Music] you
Info
Channel: Lively Geek
Views: 18,076
Rating: undefined out of 5
Keywords:
Id: i735dIqyJqY
Channel Id: undefined
Length: 11min 51sec (711 seconds)
Published: Wed Aug 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.