‫Hello and welcome this lecture.
‫We're going to be looking at how we can write things to the output log and specifically how we can write
‫the values of our variables into it that can help us debug our games.
‫Let's dive in and see how this works.
‫So in Blueprint we were able to write things to the screen when we wanted to quickly display some information
‫or potentially to just debug something in the past.
‫We have in C++ used a new property to do that, but we can actually do this using a log statement and
‫it's a little bit complicated, unreal, which is why I've shied away from doing it until this point.
‫So we're a bit more familiar with the C++ syntax before introducing it to you.
‫Now, the way it works is if you've got the extension here, installed the unreal four snippets by caps
‫and caps, this actually gets a little bit easier because you don't have to remember all of the complexity
‫of the syntax.
‫You just type, you log, and it will pop up with an IntelliSense dialog like this saying you log to
‫help you write out the whole piece of code and you can hit, enter or select it with your mouse.
‫And then what you're going to see is it's written out something that looks a bit like a function.
‫It's not actually a function under the hood.
‫It's got some a little bit of unreal magic going on here, but it's essentially a bit like a function.
‫So what we're going to do here is we're passing in a couple of arguments to this function.
‫The first one tells us what type of log category we are doing.
‫The second one tells us the level of the log.
‫So how urgent it is essentially.
‫And then finally we have got this thing here, which is basically a function within a function which
‫tells us the text that we need to print out.
‫So how does this work?
‫If we just leave this in our code, go over into unreal hit compile, we want to see where does it show
‫up?
‫Well, once the compilation has finished, we're going to go ahead and hit play.
‫And what you're actually looking for is it's not going to be up on the screen here.
‫It's going to be in the output.
‫Log down here and you can see it just about here.
‫It's saying, display your message and we've got how many messages?
‫One, two, three, four, five.
‫The reason for this is it's displaying the message for every single actor in our scene that is a moving
‫platform.
‫CP So that is why we're getting so many messages.
‫And you can see we can change out the message to be whatever we wanted it to be here.
‫And we could also put some more information into this message, such as the start location or the name
‫of the actor, things like that.
‫So here we go.
‫So we've got an output log, we've got our message, and you can start to see what the categories I
‫was talking about are.
‫You can see we've got log temp here, but up above we've got log audio log audio mixer Log World.
‫So there are different categories of logging that help us distinguish where is this message coming from?
‫Which part of Unreal is this coming from?
‫But log temp is mostly what we're going to use because it's just a temporary place for logging out debug
‫messages and then there's display.
‫This is the default level, log level, but we can have messages, be a bunch of different things.
‫It can be display or any value from this table down here.
‫So you see display here prints a message to the console and the log file.
‫We've got the log down here which prints a message only to the log file and doesn't display it in the
‫console.
‫So this is for things where you don't really want it to show up so much, but might want to use it for
‫debugging.
‫If you're debugging someone's game and it ran on their machine, they're sending you a log file.
‫Then you've got warning.
‫This is for when things seem to be going wrong and it prints it to the log console and it is also in
‫yellow.
‫Then we've got error that prints it in red and then fatal, basically prints to the console and crashes
‫the engine.
‫So you kind of want to use this one as a last resort, but it does allow you to have different levels
‫in the log file.
‫And I'll just show you what it looks like if we put in different messages here.
‫So I'm going to copy our line for logging and I'm going to make three of them.
‫We're going to have a display, going to have a warning, and we're going to have an error.
‫Now, these are typically the levels that we will use.
‫So let's go ahead and have a look and see what they look like.
‫Let's go into Unreal, hit the compile button and let's stop and restart play.
‫And by the way, you can bring up the output log by hitting the tilde key twice.
‫It brings up there you go, we've got our output log and you can see we've got three sets of message,
‫the display which is just in the plain white warning, which is in yellow.
‫So it stands out a little bit more, an error, obviously, which stands out a lot more.
‫Now we can add data to our log messages.
‫I'm just going to delete the warning and error lines of the log and we can add in some.
‫Later into this string that's going to be printed out.
‫The way we would do this is, for example, if I wanted to print out the move distance configured for
‫a particular platform that's fairly straightforward to do, we can say configured move distance, and
‫that's the text that's going to show up.
‫I'm going to put a colon in here.
‫And remember, this is all within the double quotes to make sure that we're including in the text.
‫And then we can use a percent sign followed by the F character.
‫And you see that gets highlighted differently because this is what is known as a format specified here.
‫It's something that comes from C all the way back in history.
‫And essentially usually you do a percentage sign and then some letter you can see the different letters
‫down here.
‫The F corresponds to a decimal floating point and you can see an example of what the output looks like
‫over in the example column.
‫And there's lots of other things down here we can represent with D or I a decimal integer like so.
‫So we're using F because we've got a floating point number.
‫And then what we have to do is add in another argument to the E log function.
‫So make sure that you are doing this after you've closed the brackets for this text function and instead
‫we're putting a comma after here.
‫So between these two brackets, it should be inside of the outer set, but outside of the inner set,
‫if that makes sense.
‫And then what we can do is we can pass in the value of our variable so we can do the move distance variable
‫like so let's go over into Unreal and see this in action.
‫I hit the compile button, close down the compile window and hit play and then hit Tilde twice to bring
‫up the log.
‫And you can see our configured move distances showing up for the different platforms, 505 hundred for
‫the two moving platforms and 1500 for the three pushes that we've got in the scene.
‫So one piece of information that might be a little bit interesting or instructive is that we've previously
‫talked about how we overshoot or we did overshoot the distance moved, and it would be interesting to
‫know by what kind of levels that is happening.
‫So what we could do is we could create a float variable inside the if statement.
‫So we know that we've currently overshot.
‫We can say overshoot a float variable called overshoot.
‫We're going to set it equal to the distance moved minus the move distance.
‫So that tells us how much the overshoot was.
‫So I'd like you to have a go at logging the overshoot variable that we just created here.
‫So use the Yule log function.
‫Here it is for reference at least the kind of structure that you will probably be using.
‫You can also practice using that you log autocomplete to put it in instead and then compile and test
‫your code and see if it works.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to use the autocomplete option here.
‫I'm just going to close down my sidebar to have a little bit more space.
‫And then inside this, if after we've created the variable, we are going to type you log and hit enter
‫and then we're going to leave it at display.
‫We're going to make the text platform overshot by and then we're going to use percent F and then between
‫these two brackets, we're going to add a new argument for the overshoot variable like so now let's
‫head back into Unreal.
‫I hit the compile button, close down the live coding log, hit play and double tap that tilde key.
‫And you can see that every time the platform is turning around, it is printing out a log message and
‫saying how many units it overshot by.
‫You can actually see the net.
‫A lot of cases it's overshooting by quite a few, quite a noticeable amount of units, three units or
‫0.5 units.
‫That's basically 0.5% to three centimeters.
‫It's enough that over the course of your game these platforms could have been drifting.
‫And that's great that we've implemented a fix for that already.
‫Great stuff.
‫So hopefully now you've got another tool in your arsenal for trying to figure out what's going on inside
‫your code.
‫And later on when you are feeling more confident in making your own code, trying to figure out what's
‫not working so well.
‫Okay, I will see you in the next lecture.