‫Hello and welcome.
‫In this lecture, we are going to be making some rudimentary calculations here using some variables.
‫I've got input A and input B and A, plus B when we hit play, then the result will be calculated on
‫this actor to give us a result of nine.
‫Let's dive in and see how we can perform these calculations in C++.
‫So in the last lecture we saw how we could expose some C++ variables into our editor.
‫We could see the default values that we'd set, but we haven't really explored how to code in C++.
‫We've really only declared some variables, so how can we go ahead and actually execute some functionality?
‫Well, this is where the C++ file comes in.
‫As and as I've said, there's a bit of an analogy here between the C++ file and event graph, because
‫this is where we put our functionality out.
‫What should the program do in Blueprint?
‫Whereas the event graph is where we define where to find functions and variables and what types they
‫are.
‫So let's more configuring the class.
‫So at the same is true in C++, the header file is for configuring and declaring what things are in
‫the class, and a C++ file is where the actual code lives.
‫So in this C++ file we can see there are a few things here.
‫These are the different functions in our C++ class.
‫They were defined over in the header file.
‫So you can see that we mention this big in play and tick over here and in the C++ file we show what
‫code is actually inside them.
‫So we've got big in play, we've got tick.
‫And again, we're seeing these curly braces which surround the definition of the code for these functions.
‫So the one we're interested in is Begin Play.
‫It's the same as that Begin Play event that we've seen over in Blueprint and the code that we write
‫within these curly braces after the begin play, that is code that's going to be executed as soon as
‫the game starts playing.
‫So one thing that we can do in here is we can access the variables that we've created in our header
‫file and potentially update them.
‫So for example, I could take the my INT and I could say I want to update your value.
‫We do this with the equals sign, so we say my int equals and then we could set it to something like
‫nine instead.
‫And then with any instructions in C++, we also end them with a semicolon like.
‫So notice again my indentation.
‫Everything within these curly braces is indented one level to show that it is part of the begin play
‫function definition.
‫And it's important that you also add your lines of code after the line that says Super Begin Play.
‫Now you can see that is on line 18 for me.
‫And again, we can use as many new lines as we want to separate things out and give us a bit of breathing
‫room.
‫So what happens if I compile this with live coding and then with that compilation successful, we go
‫and play well.
‫We need to select the moving platform actor in the outline as we're playing.
‫And you can see that the my INT has been updated from its default value to nine and that is because
‫begin play that begin play function was run and the command that we'd written there that we should set
‫my int equal to nine was executed.
‫Now we could use this to create a little bit of a calculator.
‫So if I were to have two integers in here that we want to add together, I'm going to create a new INT
‫32 called input A I'm going to set that default to zero and I'm going to make it a new property edit
‫anywhere.
‫Then I'm going to have a second one, which is also a new property.
‫And just to make it easier, I'm going to copy the whole lot.
‫I'm going to have a second one, which I'm going to call Input B, and then I'm going to have a third
‫one, which again, I'm just going to copy called a plus B.
‫Now, what we want to do here is take the value that's in A, the value that's in B and add them together
‫and put them in a plus B well, we can do this by going back to the C++ file.
‫I'm going to add a new line underneath where I set my INT equal to nine.
‫And I'm going to say that a plus B is equal to input A plus input B and again, notice the variable
‫names are all without spaces.
‫I've put spaces around my plus symbol to make it easier to read, and then I need to finish off the
‫line with a semicolon.
‫So if I go back into the editor and I recompile using live coding, I should see in my moving platform
‫active.
‫Charles Payne that we've got an input A and input B and an A plus B, so I can put some numbers into
‫A and B, and that will actually update the values inside my class so I can put four and I can put five
‫in here.
‫And then when I hit play, my instruction to calculate the sum of those two will be executed.
‫And if I go and look at the details pane for the moving platform, you can see that the value in A plus
‫B has been set to nine, the sum of four and five.
‫So I'd like to challenge you to add two floats together.
‫We've just done it with 30 twos.
‫Now let's try with the float.
‫So add three new float variables to your header file.
‫Call them something like input, float a, input float B and a plus B float.
‫Again, we're having to use different names because you can't have multiple variables with the same
‫name, even if there are different types and then add them together in big play so that you get the
‫result in a plus b float pause video and have a go.
‫Okay.
‫So this is going to be fairly straightforward.
‫Almost copying and pasting here.
‫I'm going to take the three variables that I've got that were in 32.
‫I'm going to paste a copy of these.
‫I'm going to have input, a float, and I'm going to change its type to float as well.
‫The input B I'm going to change the float and have the output be a float as well.
‫And then I'm going to have the type for a plus B, be a float and have it called a plus B float, which
‫is just going to make it easy to see the difference between all of these.
‫Let's go back into the editor.
‫Stop playing, hit the live coding button and that's recompiled it.
‫But I haven't actually put the code in Begin Play yet.
‫So let's go back into Visual Studio code, go to the C++ file and this is where we need to have a new
‫line at the end of the begin playback before the curly brace where we've got a plus B float is equal
‫to input A float plus input B float and then a semicolon at the end of the line.
‫And that will be executed after the other two lines are executed at the beginning of play.
‫So we'll hit the live coding button, close the terminal down and hit play, then select the actor in
‫the outline.
‫I have a look in the details pane and well, we haven't added anything into the inputs, so let's have
‫something in there.
‫For starters, maybe I will have six and I'll put 7.3 in the A and B floats.
‫Then I'm going to go ahead and hit play, select the actor, have a look in the details pane.
‫And sure enough, we've got the result of 13.3 in the result variable.
‫Great stuff.
‫So in this lecture we have used the C++ file and begin play function to execute some of our own C++
‫code.
‫I'll see you in the next one.