‫Hello and welcome.
‫In this lecture we are going to be implementing our own class member functions, move platform and rotate
‫platform.
‫We're going to see how we can set them up so that they can take in arguments.
‫And it's going to help us tidy up our code a little bit.
‫Let's dive in and see how this works.
‫So up until now, we've been using C++ functions that have already been created for us.
‫We've been using TIC V using big in play in terms of writing their functionality.
‫And we've also been using functions that other people have written for us.
‫We've been calling them such as get active location and get name.
‫Now how can we go about creating new own functions to help organize our code and keep these functions
‫small and everything much more legible?
‫Well, we've got the basic building blocks already in place by getting towards the end of the section.
‫We understand a lot more about C++ syntax.
‫So what I want to do is essentially create a new function on the moving platform that allows us to have
‫just intake, say call move platform.
‫Why do I want to do that?
‫Well, because being able to make a function call like this called MOVE Platform, it really speaks
‫to what we're trying to do at the moment.
‫We've got a lot of stuff going on in tech and I'm not entirely clear why we are doing all this stuff
‫if I just come and take a glance at it, if I went into tech and saw a function that says Move platform,
‫well then suddenly I have the context for what all this stuff should be doing, and I don't need a single
‫comment to tell me what's going on.
‫And this is going to be particularly useful when we add into the mix the possibility of rotating the
‫platform as well.
‫So how do we define declare functions?
‫Well, the declaration aspect is done over in the header file.
‫That's where we basically say a heads up to C++, we're going to have a function that's going to look
‫kind of like this.
‫And then over in the C++ is where we actually define the function.
‫We give it the body, we tell it exactly to C++.
‫This is how the function works.
‫And let's have a look at how we can do this.
‫We've got a few examples of functions and they got big in play.
‫We've got tick and it's a good point in time for us to look at the different sections that we've got
‫in our header file.
‫We've got public and protected being mentioned here and public being mentioned again.
‫Now what does this mean?
‫These key words with the colon at the end basically define a section of accessibility.
‫These functions, where can they be accessed from?
‫We've seen over in the get safe normal.
‫This is a function that we're calling on an F vector, but we're calling on it from another class,
‫not from within an F vector.
‫So this means that the accessibility of this function needs to be public.
‫Now we'll leave aside protected just for the minute because it gets a little bit more complicated there.
‫But there is another type of key word that we can use, which is private.
‫So I'm going to add after tick, I'm going to add the private key word.
‫Notice that I don't indent these sections and we're going to put a code on at the end.
‫And that means that everything that comes afterwards until the next section, the next key word that
‫I put in here is going to be private.
‫So that means we've taken all of these variables and said, hey, no other class is allowed to touch
‫these.
‫And that's usually the best practice.
‫We haven't done it up to this point because I didn't want to get things a little bit too confusing for
‫you.
‫But variables in classes should generally be their own business.
‫It's one of the core tenants of object oriented programming that we leave classes to manage their own
‫variables, and we only access them using functions that the class denotes as being public.
‫So what we're going to do here is we're going to add some more private functions in here because we
‫don't want something like Move Platform to be called from outside of the class.
‫We want it to be managed by our own tick function.
‫So how do we define a private function?
‫Well, we put it in a private section.
‫So we've got our private key word up here.
‫I'm going to put it after our variables, however, and we start off with the return type.
‫And if our function doesn't return anything, we just type void to indicate that there's no return coming
‫out of this function.
‫Then we give it a function name.
‫So we had move platform was what we wanted and then we give it some parentheses to indicate that this
‫is a function open and close parentheses and then a semicolon to finish off the line and say, okay,
‫that's it.
‫That is the function that I want to declare here in the header file and all that does.
‫It says this is the shape of the function, this is the return, this is what parameters it takes and
‫the name and which class it belongs to.
‫Now, to actually give it an implementation, we have to define it over in the C++ file.
‫So let's go over there.
‫And at the end of the file, just outside of any curly braces, this is where we do it.
‫We start again with the return type.
‫So that's going to be void then because we're inside a C++ file, we're not inside the curly braces
‫of a class.
‫We have to use the scope resolution operator to say that we're we're trying to define a function from
‫within the moving platform class.
‫So we type the name of the class, which is a moving platform, remembering that a prefix is there to
‫say that it's an actor.
‫Then we use colon, colon, the scope resolution operator, and then we use the name of the function
‫we're defining, which is MOVE platform, and then we use the same set of parameters as we did in the
‫header file.
‫In this case, we had an empty set of parameters, so we just have the parentheses.
‫And then instead of putting a semicolon at the end of this line on a new line, we put an opening curly
‫brace and a corresponding closing curly brace.
‫And between these curly braces where we put the code for our function, fantastic.
‫So what I want to do now is try and say, well, all of this functionality we've got here, and in fact,
‫I'm going to delete the comments because we no longer need them.
‫They were the pseudocode we used to implement this, but now it's kind of served its purpose.
‫We understand what the lines of code are doing.
‫I'm going to take everything but the MOVE platform function out of TTIC and copy it and paste it into
‫our MOVE platform function instead.
‫So this should work pretty much the same because when we go into tick, we then call move platform and
‫it just runs the same piece of code.
‫Except now we've bundled it into a function and it makes it easier for us to read and potentially reuse
‫in the future.
‫But if you do do this, you will notice there is a problem.
‫We are accessing Delta time and that is a parameter we no longer have access to because it was a parameter
‫passed into tick.
‫So if we want to get hold of that, we do need to have a parameter in our function.
‫So when we define parameters, we have to do it in two places.
‫Just like when we define the function, we go back over to the header file and between the parentheses
‫of our MOVE platform function.
‫This is where we say what parameters we want and it's exactly like defining a variable.
‫You need to give it a type and you need to give it a name.
‫So we're going to say it's going to be a type float and we can just call it Delta Time.
‫It doesn't have to be the same name.
‫We could call it D.T., we could call it time.
‫But the it's easier to give it the same name because then it's obvious to anyone reading it what should
‫be passed in here.
‫Then over in the C++ file, we need to match the list of parameters.
‫So again, you can just do this by copying and pasting or by typing it out.
‫If you type it out, make sure you don't make any typos because the compiler will complain.
‫So we're going to have a float called Delta Time passed in as a parameter or defined as a parameter
‫to move platform, but we haven't actually passed it in as an argument to move platform in tick.
‫And you'll see I've got a little red squiggly line here.
‫If I hover over it, it says there's too few arguments in the function call because I have defined it
‫in the header file and now in the C++ as well as requiring one parameter of type float.
‫So I must provide that parameter.
‫So I'm going to make sure that the parameter I'm providing, the value that I'm providing comes from
‫the delta time variable or the Delta time parameter from tick.
‫So with threading the value, the value gets passed into tick, then gets passed into MOVE platform
‫and ends up in our parameter in the MOVE platform function where it can be used to execute our MOVE
‫platform functionality.
‫So I'd like you to create your own rotate platform function that we're going to call from TIC.
‫So you're going to add it to the header file like we've done for our MOVE platform function.
‫We're going to add it to the C++ file in the same way as well.
‫So take your hints from the way we did it for those functions and then call it from the tick function.
‫Make sure you pass in Delta time and you define any parameters to allow you to pass in the Delta time
‫and implement the body of the Rotate platform so that it logs out a message.
‫It's not actually going to rotate the platform yet, but as a placeholder to make sure that that is
‫actually getting called print a log message using the UI log that we've learnt about.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to start off in the header file and we're mostly going to be doing the same as we did
‫for our MOVE platform.
‫We're going to put it in a private section so that it's only accessible within our class.
‫I'm going to give it a void return type, we're going to call it rotate platform.
‫We're going to open up the parameter list.
‫And in it we're going to put one parameter which is going to be a type float.
‫We're going to call it Delta time and close the parameters and finish off with a semicolon.
‫Now, most of what we do over in the C++ file is exactly the same as in the header file.
‫So I'm actually going to copy this line, go over to the C++ file and show you what is different.
‫So I'm going to go to a new empty line at the end of the file, paste it in, and then what's different
‫is over here we need the scope resolution operator in the class name.
‫So we're going to have a moving platform, colon, colon rotate platform.
‫Then we have the parameter list and then we don't need the semicolon on the end.
‫So remove the semicolon, add a new line and add our curly braces instead and a new line between those
‫two curly braces.
‫So what we want to do in here is print out a log message.
‫So I'm going to type you log hit enter.
‫It's just going to be a simple display message.
‫And in the text I'm going to put the name of our platform.
‫So it's going to be percents.
‫Then we're going to type rotating dot and then I'm going to pass in the name.
‫Now we could put this into a variable, but because it's quite a short line here, I'm just going to
‫type star, get name and then put the parentheses so that we're calling essentially here calling the
‫get name.
‫That is the expression that gets hold of the F string.
‫And then we are converting that result to put into our log message.
‫So from all of our platforms, I'm expecting to see that rotating is printed, but this will only happen
‫if I actually call it in tech.
‫So let's go up to the tick function after MOVE platform can add a new line and call the rotate platform.
‫And we need to you see, when I open up the parentheses, it gives me a hint that I need to pass in
‫an argument for Delta time.
‫So again, we're just going to pass that same Delta time variable from within here.
‫And like I said, the names here don't have to be the same.
‫So this delta time needs to match with the Delta time within tick.
‫But we could choose to call the variable within rotate platform date instead and anywhere we use it
‫within rotate platform, we'd have to use date the name of the parameter and the argument can basically
‫be different.
‫So let's hop over into unreal and hit compile because we've made some changes to the header file.
‫The compilation will be slightly longer than when we just change the C++ file and once that's completed
‫compiling, we're going to go ahead and hit play and open up the output log.
‫And you can see that we are indeed displaying log messages about each of the platforms rotating.
‫So it looks like our function is effectively getting called in tick.
‫Great stuff.
‫Now we understand how to create our own functions that are members of a class.
‫It's very useful tool for reusing a code, but also for organizing it just like it was in Blueprint.
‫I'll see you in the next lecture.