In this lecture, you'll learn how to debug an unreal script using visual studio. Recall that in a previous lesson we talked about the debugging process. We'll use that same debugging process when we debug an unreal script. I've created a project that has an actor called the buggy trigonometry, so let's go take a look at that actor. I've added our buggy trigonometry code to the script, except that we're not prompting for in getting in the angle, because we haven't learned how to do that yet in unreal. So I've declared in angle variable and set it to 45 degrees, I'm trying to convert the angle to radiance here, but M PI gives us a compilation error. So let's find out in the unreal engine documentation what we should do instead here in the documentation, I'll search on pie and I get a number of results. So let's click on this get pie thing, because that seems like it might be useful. Unfortunately, this is the blueprint for get pi but it says the target is the kismetMathLibrary. So let's search the UKismetMathLibrary, and if we click on the top result, we see that there's a math library that has lots of functions available to us. So let's look for get pi And luckily, this is an alphabetical order, so we found get pie. We can click on it to see that it returns the value of pi as a float so we can call this function in the UkismetMathLibrary to get pie still will say, I'll steal some code from down below. So remember when we fully specify in name space, UkismetMathLibrary is a name space. So we put the name space colon colon, and then we can call get pie to actually get pie. Now one of the important things in that documentation was at the very top of the documentation, and it said we need to include the kismet/kismetMathLibrary.h header file. So I've already done that here in the script, I already knew I needed to do that, so I did it here. So we're using the UkismetMathLibrary name space, which is specified in the kismeMathLibrary.h header file. So we have to pound include that header file so that we can use stuff in that name space. I made a mistake here I wanted us to have a bug so let's add our bug back in and I meant this to be the buggy code from before. And as you can see when I run it, we get incorrect co sign and sign values in our output log panel so now it's time to actually debug our code. Now there's lots of information on the Internet about how you can debug your C++ code with unreal engine and visual studio. Unfortunately, my personal experience has been that sometimes those things work, and sometimes they don't and I'll even do something and it will work the first time. And then it won't work the second time, so debugging in visual studio with unreal engine is a hit or miss proposition. Much of the time, I will say that you can always use the UE log macro to print out intermediate values of variables to see what they are as you run your game. But I'm a huge fan of using an actual D bugger to do stuff, so I may have found something that consistently works and I'll walk you through what that thing is. So I'm going to actually close down the unreal editor and I went to the folder that holds the unreal project and double click the solution file the visual studio solution file. The extension for that file is SLN and it's the same solution file you would double click if you're doing a console apt, except, of course, this is in an unreal engine game. Once visual studio opens, you need to make sure that up here under the solution configurations, you select development editor and then you need to rebuild your solution. So you select build and you say, rebuild solution I've already done that takes a long time, so I'm not going to do it again during the video. But now I can set a breakpoint, and I can just press F5 to debug just like we did in a console app. And what's going to happen here is it's going to actually open up the unreal engine editor and we'll be able to run our game with the visual studio solution attached to that unreal engine game so that we can debug. I'm going to turn off the magnifier again and now when I run my game, I stop at that break point and I can look at the value of angle either here or down here, and I can see that the value of the angle is horribly incorrect. I will point out that I added a little extra to my code to make this work, though, so compilers will optimize code. And optimizing code means that it makes it run more efficiently by sort of restructuring, sometimes as it generates the machine code that's going to run on the machine. The problem is that in the debugger, sometimes that optimization makes it so. We can't actually look at the values of variables so, for example, I might try to hover over angle. And if the compiler has optimized that variable away just because it's used temporarily here in this function, then I won't get anything when I hover over it and I won't be able to look down below to see it. And in fact, even the thing down here in locals that says this, that I can expand and look at characteristics of it that also will say this has been optimized away, and that's not helpful to us while we debug. It's usually helpful to us as we run our game because we want our code to run as quickly as possible. But if we can't look at it with the D bugger while we're trying to fix bugs, that's not helpful to us. So I've added what's called pragmance and this pragma I might as well stop debugging for now. And this pragma, optimize I'm basically saying turn off optimization for everything. There are various levels of optimization, and by providing this empty string, I'm saying turn off all the optimization. So I turn off optimization before the function that I'm trying to debug and then I turn it back on after the function I'm trying to debug. So this is a function level command, if you will, to the compiler and this is a visual studio compiler command. Other compilers will have potentially different commands for turning optimization on and off. So I needed to do that so I could actually look at the value of the angle variable while I was debugging. We know that this is backwards, so I will multiply by pi and then divide by 180 and I'm going to debug again and I'll just press F5 and I'll run the game again. I hit the breakpoint again, but this time angle is the correct angle remember, about three quarters. So now that I believe that I've fixed the problem, I can get rid of the break point and I can close the visual studio solution and I've opened the project in the usual way. And now when I hault p to run the game, we see that the co sign and sign values are now correct. To recap in this lecture, you learned how to debug an unreal script in visual studio. Recognizing that it may be easier to just use UE log to output the values of selected variables in your code. The process I showed here does not work in X code. So to debug an X Code unreal script, you're really going to just have to use UE log to print out those variable values