‫Hello and welcome.
‫In this lecture, we're going to be adding variables to our moving platform actor so that we can see
‫them in the editor and we're going to be doing this entirely in C++.
‫Let's dive in.
‫So the first concept that we're going to be learning in C++ is how to create variables.
‫We've seen them before in Blueprint.
‫We know that they're like boxes that we can put values into and that they have types.
‫Well, all of that still applies over in C++, except how we create them.
‫So previously, when we did this in Blueprint, if I go and open up a level blueprint just to show you
‫and refresh your memory, then we had this, my blueprint pane, and that was where we added things
‫like functions and variables and so on.
‫And then we had the event graph where we actually program stuff in.
‫Now C++ is very similar.
‫We have two different locations for doing the configuration of our variables and functions and then
‫when we actually do the implementation.
‫So let's go over to Visual Studio code and open up the Visual Studio Code Project and have a look.
‫In the left hand bar there is an Explorer tab.
‫Go and open that up and you can see basically a hierarchical map of your files.
‫So we've got at the top level five and Obstacle Assault or our own project here, you can open that
‫up and you can see some of the folders here.
‫The one we're interested in is the source folder, which is where we can find all of our C++.
‫Then in there there is the obstacle assault folder and then there's two files with the name of moving
‫platform.
‫One is a dot file and one is at DOT CP, the DOT stands for Header and CP it stands for C++.
‫So this is essentially a bit like having a my blueprints where you can add in the functions and variables
‫and stuff that is resides in the header file, the FF file, and then we have the dot CP where we can
‫actually essentially like the event graph, we can actually do the coding itself.
‫So we're going to open up the moving platform.
‫H Because we want to add in a new variable and you'll notice that there's a whole bunch of stuff in
‫here.
‫We're not going to go into the details of this, but what is most interesting is that it is defining
‫what it means to be a moving platform class.
‫You can see a couple of keywords here that are interesting.
‫There's a class keyword on line ten.
‫There is the moving platforms which is prefixed with an A, meaning that it's an actor, and then there
‫are some curly braces on line 11 and 26.
‫And between these matched curly braces, you can see there is the declaration of a few things.
‫There are functions in here and this is where we can also put our variables.
‫So we're going to add our variables to the end of this declaration.
‫So just before we close that curly brace, we can add some new lines and start writing our new variables
‫in here.
‫And just like in a variable in blueprint, we have a type and we have a variable name and we can also
‫give it a default value.
‫So the first thing is we want to maybe create an integer and in C++, in Unreal C++, an integer is
‫denoted with the keyword INT 32 basically telling us how big a value we can store in here.
‫And no, it's not 32.
‫It's much, much larger than that.
‫32 is the number of bits allocated to the integer.
‫Now we've got to go for a variable name and just like the classes, we cannot have spaces in our variable
‫names.
‫So instead we use capital letters to denote our words.
‫So I'm going to call it my int notice.
‫I'm capitalizing both of the beginnings of those words.
‫Then I can give it a default value and we do this with an equal sign so we have equals, and then we
‫can give it a value such as 99, for example.
‫Now notice that I'm putting spaces after my and before my equals sign.
‫That's not actually strictly necessary, but it does make it easier and nicer to read.
‫So I include that.
‫Now you do obviously need a space between the 32 and the actual name of the integer.
‫Without that, things don't work.
‫And then to finish off this declaration, you need to put a semicolon on the end of the line to tell
‫C++ that you are done.
‫The fact that there is new lines and that there are spaces and tabs, all of this doesn't actually matter
‫to C++.
‫You could run all of this stuff together and this would still be the same.
‫I could take out the spaces here that would still work just fine.
‫As far as C++ is concerned, this is all perfectly valid C++.
‫It's just really awful, awful to read.
‫So what we do is we keep this level of indentation with a tab and we keep this level of spacing as well
‫just for our sanity.
‫Now this isn't currently visible over in the Blueprint Editor.
‫To make it visible, we need to give a little bit of a heads up to Unreal that there is a variable it
‫should display.
‫The way we do this is on the preceding line we type you property all caps.
‫Then open parentheses and then using our what's called camel case, basically where we capitalize the
‫first letter of each word we type edit anywhere like so, and then we close the parentheses.
‫No need for a semicolon at the end of this line.
‫That's not correct, because it's actually part of the same declaration as the integer down on the line
‫below it.
‫But we put them on separate lines just to make our life easier for reading this.
‫So we've got you property edit anywhere in 30 to my int and then equals sign and then 99.
‫And it's important that you save your files here by default.
‫You need to hit control as to do this.
‫If you go to the file menu, you can hit autosave here as an option so that you don't have to hit save
‫every time you change things.
‫Now that's what I do.
‫So with that done, we could go ahead and close the editor, recompile and re-open it and we would have
‫a new variable on our moving platform.
‫Actor But that's a bit of a pain if we had to close the editor every time we made a code change.
‫So there is a feature in Unreal called Live Coding.
‫I'm just going to go and close down the level blueprint and show you that there is a button down in
‫the bottom right which looks like a set of cubes dissolving off into the distance.
‫If you hover over it, it says recompile and reload C++ code for games on the fly, click that button
‫and a terminal will show up with a log of messages that are the results of compiling your code.
‫Now, this will run a lot faster than it previously did because it's not the first time it's running,
‫and when it's complete, it's going to give you a notification saying that live coding succeeded.
‫Now it's important that if live coding didn't succeed, that you copy the results of the log that come
‫before the error and post those in the Q&A if you need to ask for help, but also just double check
‫and make sure that your syntax matches what I've typed on screen.
‫Oftentimes we will find that it's simply a typo that people have made and it doesn't compile because
‫of that.
‫Pay close attention to your capitalization as capitalization is often very important and also your semicolons.
‫People often forget to include those.
‫So now I'd like to challenge you to add your own variable on a new line in that header file.
‫So make it a float or bull type and give it a different name.
‫Variables cannot share the same name.
‫They have to be different.
‫Give it a default value.
‫So if it's a float, the default value needs to be of the format.
‫Something point, something, something.
‫If it's a boolean, it needs to be either true or false all lowercase recompile using live coding and
‫then see if you get any errors and if you do come back and see how I do it to double check that you've
‫written it the right way.
‫Pause video and have a go.
‫Okay.
‫Welcome back.
‫So let's go back into Visual Studio code and we're going to add a variable.
‫So first of all, we're going to be making it a new property.
‫It's open parentheses and edit anywhere and close parentheses.
‫Then on a new line, I'm going to make it a float type and we're going to call it my float just to keep
‫up the tradition here.
‫And we're going to give it a default value of 5.99 and then put a semicolon at the end of the line,
‫make sure that you have all of the components of what I've just typed in here.
‫Then I'm going to add some more spaces.
‫Notice that I'm adding multiple new lines.
‫It doesn't really matter how many new lines you have.
‫Whitespace doesn't matter.
‫As I've said, then we're going to have a new property again.
‫Edit Anywhere that's going to be of type bool and I'll call that my bool and I'm going to give it the
‫default value of true and put a semicolon on the end there.
‫Now you don't have to save if you've got autosave, but you do.
‫If you don't, then we go back into the editor.
‫We hit the live coding button in the bottom right and the log will come up and hopefully we'll succeed,
‫which it has done for me.
‫But I just want to show you what would happen if I'd made a mistake, such as leaving out the semicolon
‫and the end of this line instead of a nice, reassuring green.
‫At the end of the log you will have a red build failed showing up instead.
‫So this is important that if you get a build, fail, first things first, go and check your code,
‫make sure it matches mine, because as I've said, it's often just a typo and if not, then copy this
‫and put it in the Q&A and ask for some help.
‫That said, I'm going to correct my code now and recompile it, and when that has succeeded, I can
‫close down the log and I can look in the outline for the moving platform, and I can select the moving
‫platform to look in the details pane and see all of the variables that I've created.
‫They my int the my float and the my bool and the default values that we have given them there.
‫Now, it's worth noting that these are indeed just defaults.
‫So if you were to override those values in the editor, in Blueprint or in the level, and then you
‫go and change them in C++, the changes in C++ will no longer have an effect.
‫They were only default values.
‫So if you've made changes in Blueprint, that takes precedence and overrides anything you did in C++.
‫Fantastic stuff.
‫We've created our first variables, our first code in C++ and brought it in using live coding.
‫I will see you in the next lecture.