‫Hello and welcome.
‫In this lecture, we're going to be looking at vectors and in general more complicated things like structs
‫in C++ and how we can access the individual parts to both set and get those from a struct.
‫Let's dive in.
‫So we've learned how to deal with basic data types in C++ and create variables and assign to them and
‫even do some basic arithmetic with them.
‫But what about if we wanted to deal with more complicated types such as vectors and transforms?
‫Well, the process is actually very similar.
‫Let's start off by clearing out some of the test code that we've already got.
‫I'm going to delete all of my properties.
‫For me, that's line 26 down to 51.
‫And I'm also going to go over into the C++ file and delete lines 19 to 23, which is where I'm doing
‫all of the work with those variables that I just deleted.
‫So now Begin Play is completely empty and we're back to essentially square one.
‫So now what I want to do is add in a new you property.
‫So all caps again edit anywhere and I'm going to give this the type of F vector notice the capitalisation
‫capital F, capital V and this is how we deal with the vectors that we've dealt with in Blueprint.
‫They're the same vectors under the surface, but this is how we type it in C++.
‫The F that precedes it just lets us know that this is a struct in the same way as the A that proceeds
‫moving platform lets us know that that is an actor.
‫That's not a C++ thing.
‫That is an unreal naming convention for naming the different classes, structs and types.
‫So then what we're going to do is give this variable a name and this works exactly the same way as if
‫it were a float or an int.
‫We can do call it my float, for example.
‫It needs to have no spaces.
‫And if we were to put semicolon at the end of the line here, then that's it.
‫We've declared a new variable that will be visible in our details pane over in the editor, but that's
‫not enough for me.
‫I would like to actually give this a default value that's different from the default 000.
‫To do that, we need to update our glossary with a new term.
‫We need a constructor, which is essentially something in C++ that makes a new struct or class value.
‫So if we were to create a constructor for the vector, the way this would work is we want to assign
‫a value so we hit equals.
‫And typically when we were dealing with integers or floats, we could just use something like a one
‫or a true.
‫There were built in values that allowed us to set the value of those basic types.
‫But when we have more elaborate types like a struct, we need to use the constructor and the constructor
‫will always have the same name as the data type.
‫So it will be f vector spelled exactly the same, and then you open some parentheses and inside the
‫parentheses you give the values that you want.
‫So X, Y and Z is what makes up a vector.
‫So we could have one, two and three all separated by commas, and then we close the parentheses and
‫put semicolon at the end of the line.
‫And that will give a default value of one, two and three in the X, Y and Z for this vector.
‫Let's try it out.
‫Go back into Unreal and hit the compile button.
‫And once that's completed successfully, we can make sure we've got the moving platform actor selected
‫in our outlier and have a look at the details pane and see that indeed we've got a new property called
‫My Float.
‫The other ones have been taken away and the values in its different positions X, Y and Z is one, two
‫and three, just as we expect it.
‫So that's great.
‫But what if I wanted to get hold of these values and stick them perhaps in another property?
‫So if I were to have another property which is marked as edit anywhere so we can see it in the inspector
‫of type float called my x and we'll default this to zero just so that we can see that we're changing
‫it.
‫And what I want to do is grab the x value from the my float in begin play and I want to set it in the
‫my x variable.
‫So let's go over to the C++ file and in begin play after our the line that says super big in play,
‫we're going to set my x equal to what we want to get something from my vector, but we don't want the
‫whole of my vector.
‫That would be an error.
‫We can't set a vector into a float, we just want the x part of that vector.
‫Well, to understand that, we need to understand the concept of an operator.
‫Operators are symbols in C++ that do something.
‫We've seen a few already.
‫We've seen the plus operator, which adds two things together.
‫There's also a minus operator.
‫There's the equality operator, the equals operator, which assign.
‫Things.
‫So we're using this right here on line 19.
‫That's an operator that is assigning the value of my vector to my X.
‫And then we have one more operator, which we need to get to know which is the DOT operator.
‫It is an operator that gets something from within a struct or class.
‫So we want to get the X from within my vector.
‫So we use a dot operator, we do dot x now we don't use a space here between the my vector and the x.
‫We just leave these run together like so that's how we typically leave things even though it isn't necessarily
‫an error to put spaces in.
‫So we've got my vector x should get the x value of the vector and put it into the my x float.
‫So we put semicolon at the end of the line there and as usual I'm ignoring the red squiggles because
‫sometimes they take a long time to update and sometimes they aren't correct.
‫And then we're going to go into live coding and hit the button that re compiles and just check whether
‫we get a successful outcome.
‫And no, you see, we've got a build failure.
‫And if you look at the error, it's saying my vector is an undeclared identifier and that's because
‫I've made a typo.
‫I've called the F vector a my float and you're probably screaming at your screens, but it's good to
‫show you that even experienced programmers make mistakes.
‫We wanted to call that my vector, so I'm going to go back to the header file and rename it to my vector,
‫which should remove the the problem with in my brain.
‫Let's go ahead and click the compile button again.
‫And that just gives you a taste of what it's like when you make errors, you get a compilation error.
‫It happens all the time.
‫In fact, it's very rare to to not make mistakes.
‫The first time you do something, then we're going to go ahead and hit play and we'll see what value
‫we get in the details pane.
‫You can see now my x has been set to one because that was the value in the x component of the my vector
‫variable.
‫So I'd like to challenge you to set a vector value.
‫We've been getting values from the vector.
‫Now let's try setting them.
‫Basically try using the dot operator to set a value.
‫Try flipping the two sides of that equals so that we have the my X on the right and the my vector over
‫on the left instead.
‫And what if you change it over to Y or Z?
‫Is there any difference there?
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So I'm going to have a go at flipping these two around.
‫Let's go over to the C++ and in Begin Play instead what I'm going to say is my vector dot y is equal
‫to my x and then put semicolon at the end of the line.
‫And this is how we can set values within a struct as well.
‫So let's go back over into Unreal and recompile and then we can go ahead and set some value in my X.
‫So I'm going to put the value of five in here.
‫Then I'm going to go ahead and play and have a look at the details pane for the moving platform.
‫And sure enough, the y value of my vector has been updated in begin play by the value in my x.
‫So congratulations.
‫We have seen how to use more complicated types that are compound types like structs in C++.
‫I will see you in the next lecture.