‫Hello and welcome.
‫In this lecture, we're going to be looking at how we can add dynamic text into our log messages using
‫strings in C++.
‫So let's dive in and learn about strings and C++.
‫So we've seen how to print out some simple things from our UI log, but what if we wanted to print out
‫some more text, something like the actor name, for example, or even something more complicated,
‫such as a textual representation of a vector?
‫Well, that does not typically have format specifiers because format specifiers have come to us from
‫the age of C, which is a very basic programming language in comparison to C++.
‫And so certain things just weren't thought up at the time.
‫So things like vectors would not be included in the way that we format specify them.
‫So we tend to use strings to do this.
‫And as we've said in the past, strings are just text variables.
‫They are variables that contain text so we can declare strings.
‫In C++ we've used F vectors where we used vectors previously.
‫Here we're going to use F string as the type for a string so we can have a string.
‫And I'm just going to put in my string as the variable name for now.
‫And how do we set a string value?
‫Well, we've seen it a little bit around here already.
‫These double quotes are what denote a string value in C++.
‫So all of these expressions here, that one here where we've got the double quotes also over in the
‫header file where we've got the double quotes for the category that is a string expression.
‫So what we can do is set the my string variable from this and I'm going to say my string value just
‫to give us an idea of that and put a semicolon at the end of the line and then how can we show this
‫in a log statement?
‫So I'm just going to remove the move distance from here as an argument to the log, and I'm going to
‫change the text in the log message to say, here's my string colon.
‫And then instead of the F specify, we use percent SSE this time to denote the string that we want to
‫print out.
‫And then the argument we pass into the E log function again between these two brackets is going to be
‫my string.
‫Now unfortunately that would be great if that just worked by itself.
‫But unfortunately there's a little bit of a complication here in Unreal that they won't accept a straightforward
‫string as an argument to the YUI log.
‫It needs a little bit of a converting operator here, and the way we do this is by putting an asterisk
‫in front of the my string and it just converts it from an F string into the format that it's looking
‫for internally within the U log.
‫We don't have to go into too much detail as to why it's doing this.
‫It's it's beyond the level that we need to know at this point in time.
‫I think it's just important to learn it as it is that when we do a percent se, we always need a star
‫in front of our f strings.
‫So let's see if that's worked by going over into Unreal and hitting the compile button.
‫And then we're going to close down the live coding, hit play, open up the output log and here you
‫go, here's my string, my string value.
‫So it's printing out the value in the my string.
‫But obviously this piece of code that I've just written is not particularly useful because well, I
‫could have just written this string into here.
‫Why bother with this whole substitution business?
‫Well, where this gets more interesting is where we put a dynamic value into this variable instead.
‫So we could do get name and call that.
‫That is a function here available in the moving platform actor that gets the name of the actor that
‫we see in the level out liner, we can put it into the variable.
‫I'm going to call that variable name instead of my string and change its name down here in the US log
‫as well.
‫So then we can change the text of the log message to say begin play colon and then it's going to be
‫the name of the actor.
‫So let's go back into unreal, stop play, hit, compile, go ahead and hit play and open up the output
‫log and you can see we get the messages begin play moving platform one begin play moving.
‫Platform three pushing.
‫Kub three pushing, keep five, pushing, keep seven.
‫So we've got big in play and all the names of the different actors.
‫Now this can be very useful because later on down the line where we're printing out our overshoot distance,
‫it's not always clear who is overshooting in this case because they're all getting messed up together.
‫So being able to print out the name of the actor that is producing the log message can be really helpful
‫in pinpointing where a problem is.
‫Happening.
‫So I'd like to challenge you to log the name with that overshoot log so that we are going to see who
‫has overshot and by how much.
‫So you're going to add a variable for the name of the actor to tick like we did in Begin Play.
‫You're going to get name into it and you're going to add a percent as specified somewhere anywhere you
‫like into your log message where we're printing that overshoot and you're going to add an argument for
‫that name.
‫We still want the overshoot variable in the list of arguments, but after all, before that one, you're
‫going to have to put the argument for the name as well.
‫And it's important to hear that the order of the argument matches the order of the specifiers in your
‫message.
‫So if you put percents before percent f, then that's the order.
‫The name should go before the overshoot or you should do it the other way round if you've done the other.
‫And don't forget to add the star before your variable name to convert the string into something that
‫the log you e log can use.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to start off by getting a variable and this isn't strictly necessary, but we are going
‫to use the variable just for simplicity's sake and keeping that line a bit shorter.
‫So we're going to have an F string called name using uppercase letters, usually to start our variable
‫names.
‫We're going to do a get name in to that variable and then we're going to use it as an argument.
‫But where am I going to put my format, specify it in the log message?
‫Well, I'm going to put it as the first thing in my log message to kind of make it clear that this is
‫a log message coming from this thing.
‫So I'm going to put percent s first, and that means that my argument needs to go before the overshoot
‫argument.
‫So we're going to get their name, put it in here, and we need to put an asterisk in front of the name
‫in order to convert it appropriately.
‫Now, let's go back into the editor.
‫Stop playing hit recompile, close the terminal, hit play and open the output log.
‫And you can see that we're starting to get our tick messages coming through.
‫Got push and cube overshot by one push and q11.
‫So you can see here you can make a conclusion that we couldn't make before, which is that the faster
‫moving platforms, the pushing cubes are the ones overshooting by more, whereas the moving platforms
‫seem to be overshooting by less, which is what I would expect.
‫But it's good to see it confirmed here in the logs.
‫Great stuff.
‫So that's it for our little logging diversion.
‫Hopefully you found this interesting and I will see you in the next lecture.