‫Hello and welcome.
‫In this lecture, we're going to be looking at the C++ equivalent of pure blueprint functions, which
‫is concept functions.
‫We're going to see what this consort keyword is, how we use it, and how it restricts what we can do
‫in our code to make us safer.
‫Let's dive in and see how it works.
‫Over in blue print, we were able to make a function pure, and that meant that we could take away the
‫need for the execution pins.
‫Now, in C++, we can do something a little bit stronger, which is to make a function constant.
‫Now, what does it mean to make a function const?
‫It means that that function cannot modify the state of your class, and it is actually enforced by the
‫rules of the compiler.
‫So let me give you an example.
‫If I were to take our should platform return function, this is not a function that should modify anything
‫in the class and it would be an error really if we forgot that because it would be weird if calling
‫this and using in an if statement actually change the location of the platform or if it updated the
‫move distance or platform velocity.
‫So to keep ourselves honest to that, we can make it convert.
‫And the way we do this is by putting the contact keyword after the parentheses of the function.
‫We often leave a space here as well.
‫Now, in order for it to correctly match up to the function in the C++ file, we actually have to put
‫that const keyword over there as well.
‫Now, like I said, this now makes it an error to do anything that would modify the class.
‫So if in should platform return, I were to try and modify a class variable such as move distance.
‫I might try and set move distance to 200.
‫Let's see what happens when we go over to unreal and hit the compile button.
‫Okay.
‫You can see that there's now an error.
‫It does fail to build and if you look at the error, you can see that there's something being said here
‫about not being able to modify something because it's being accessed through a constant pointer.
‫Now don't worry about that language.
‫It's a little bit hard to interpret, but the point is the keyword const was there and it's saying it's
‫not able to modify something and that is crucial.
‫Also, if you called any functions that aren't marked as being const, that's also a problem.
‫So while it's fine to call get actor location that is a cost function doesn't change anything just gets
‫the location.
‫It's not okay to call set actor location that hasn't been marked as const and so it's not valid.
‫It could be modifying the class.
‫So any function that's not marked as const is not allowed to be called from within a cost function.
‫And so you can see a problem emerging.
‫We can't actually call get distance moved from within our should platform return because should platform
‫return is const but get distance moved hasn't been marked as such.
‫So a quick charge to you get the distance moved const make get distance move cost so in the header and
‫see p file you're going to add that const keyword to let C++ know that it shouldn't modify the class
‫and does it compile now pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to go over to the header file first.
‫And after the closing parentheses, we're going to add a space concept and then we're going to go back
‫to the C++ file.
‫And after a distance moved parentheses, we're going to put space concept.
‫Now the squiggly lines under get distance moved should update in a little bit of time.
‫But if we go over to Unreal and hit the compile button, that will be the ultimate arbiter as to whether
‫the compiler likes what we've done or not.
‫And this time the compilation has succeeded successfully, which means that we have correctly updated
‫our code.
‫And that makes sense because there is nothing being called from get distance moved that is not concept,
‫it's not trying to change anything.
‫And where we are calling a function in should platform return, we are only using const functions and
‫this is a way for us to increase the safety of our code because functions won't be changing stuff without
‫us knowing about it.
‫Now as a little exercise to your self, you might want to go through and look at all the functions that
‫we've been using up to now and see which one of them are marked as constant, which ones aren't.
‫You can do this by simply hovering over a function such as get actor location and seeing whether the
‫pop up that is included, whether after the parentheses it has contact or not.
‫You can see with get active location hovering over it gives me const hovering over get name also gives
‫me that this is const and it makes sense.
‫Neither of those two should be changing the state of our class.
‫Now if I hovered over tick you can see this one isn't marked as const and that makes sense because Tick
‫is usually used for updating how a class is positioned in the world or any of the state of the class.
‫And we've got our vector.
‫If we hover over, get safe, normal, you can see that it's marked as const that's because get safe
‫normal doesn't actually modify the vector, it just returns a value with a new vector in it.
‫So that's it.
‫For this lecture we've learnt about adding const to our functions to increase the safety of our code.
‫I'll see you in the next one.