﻿1
00:00:04,540 --> 00:00:05,560
‫Hello and welcome.

2
00:00:05,560 --> 00:00:10,840
‫In this lecture, we are going to be making some rudimentary calculations here using some variables.

3
00:00:10,840 --> 00:00:18,190
‫I've got input A and input B and A, plus B when we hit play, then the result will be calculated on

4
00:00:18,190 --> 00:00:20,830
‫this actor to give us a result of nine.

5
00:00:20,830 --> 00:00:24,940
‫Let's dive in and see how we can perform these calculations in C++.

6
00:00:25,090 --> 00:00:31,420
‫So in the last lecture we saw how we could expose some C++ variables into our editor.

7
00:00:31,420 --> 00:00:37,990
‫We could see the default values that we'd set, but we haven't really explored how to code in C++.

8
00:00:37,990 --> 00:00:45,490
‫We've really only declared some variables, so how can we go ahead and actually execute some functionality?

9
00:00:45,490 --> 00:00:48,730
‫Well, this is where the C++ file comes in.

10
00:00:48,730 --> 00:00:53,710
‫As and as I've said, there's a bit of an analogy here between the C++ file and event graph, because

11
00:00:53,710 --> 00:00:55,970
‫this is where we put our functionality out.

12
00:00:55,990 --> 00:00:58,870
‫What should the program do in Blueprint?

13
00:00:58,870 --> 00:01:04,120
‫Whereas the event graph is where we define where to find functions and variables and what types they

14
00:01:04,120 --> 00:01:04,270
‫are.

15
00:01:04,270 --> 00:01:07,240
‫So let's more configuring the class.

16
00:01:07,240 --> 00:01:14,320
‫So at the same is true in C++, the header file is for configuring and declaring what things are in

17
00:01:14,320 --> 00:01:18,730
‫the class, and a C++ file is where the actual code lives.

18
00:01:18,970 --> 00:01:23,980
‫So in this C++ file we can see there are a few things here.

19
00:01:23,980 --> 00:01:28,300
‫These are the different functions in our C++ class.

20
00:01:28,300 --> 00:01:30,490
‫They were defined over in the header file.

21
00:01:30,490 --> 00:01:37,630
‫So you can see that we mention this big in play and tick over here and in the C++ file we show what

22
00:01:37,630 --> 00:01:39,670
‫code is actually inside them.

23
00:01:39,670 --> 00:01:42,100
‫So we've got big in play, we've got tick.

24
00:01:42,100 --> 00:01:49,360
‫And again, we're seeing these curly braces which surround the definition of the code for these functions.

25
00:01:49,750 --> 00:01:51,910
‫So the one we're interested in is Begin Play.

26
00:01:51,910 --> 00:01:57,790
‫It's the same as that Begin Play event that we've seen over in Blueprint and the code that we write

27
00:01:57,790 --> 00:02:04,060
‫within these curly braces after the begin play, that is code that's going to be executed as soon as

28
00:02:04,060 --> 00:02:05,740
‫the game starts playing.

29
00:02:06,040 --> 00:02:12,190
‫So one thing that we can do in here is we can access the variables that we've created in our header

30
00:02:12,190 --> 00:02:14,860
‫file and potentially update them.

31
00:02:14,860 --> 00:02:21,310
‫So for example, I could take the my INT and I could say I want to update your value.

32
00:02:21,340 --> 00:02:27,670
‫We do this with the equals sign, so we say my int equals and then we could set it to something like

33
00:02:27,670 --> 00:02:28,990
‫nine instead.

34
00:02:28,990 --> 00:02:35,590
‫And then with any instructions in C++, we also end them with a semicolon like.

35
00:02:35,590 --> 00:02:38,110
‫So notice again my indentation.

36
00:02:38,110 --> 00:02:44,530
‫Everything within these curly braces is indented one level to show that it is part of the begin play

37
00:02:44,530 --> 00:02:46,600
‫function definition.

38
00:02:46,750 --> 00:02:53,080
‫And it's important that you also add your lines of code after the line that says Super Begin Play.

39
00:02:53,350 --> 00:02:56,380
‫Now you can see that is on line 18 for me.

40
00:02:56,380 --> 00:03:00,910
‫And again, we can use as many new lines as we want to separate things out and give us a bit of breathing

41
00:03:00,910 --> 00:03:01,420
‫room.

42
00:03:01,630 --> 00:03:10,210
‫So what happens if I compile this with live coding and then with that compilation successful, we go

43
00:03:10,210 --> 00:03:11,830
‫and play well.

44
00:03:11,830 --> 00:03:16,780
‫We need to select the moving platform actor in the outline as we're playing.

45
00:03:16,780 --> 00:03:24,760
‫And you can see that the my INT has been updated from its default value to nine and that is because

46
00:03:24,760 --> 00:03:30,880
‫begin play that begin play function was run and the command that we'd written there that we should set

47
00:03:30,880 --> 00:03:34,960
‫my int equal to nine was executed.

48
00:03:34,960 --> 00:03:39,040
‫Now we could use this to create a little bit of a calculator.

49
00:03:39,040 --> 00:03:47,140
‫So if I were to have two integers in here that we want to add together, I'm going to create a new INT

50
00:03:47,140 --> 00:03:58,120
‫32 called input A I'm going to set that default to zero and I'm going to make it a new property edit

51
00:03:58,120 --> 00:03:58,930
‫anywhere.

52
00:03:59,050 --> 00:04:02,140
‫Then I'm going to have a second one, which is also a new property.

53
00:04:02,140 --> 00:04:05,560
‫And just to make it easier, I'm going to copy the whole lot.

54
00:04:05,770 --> 00:04:11,620
‫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

55
00:04:11,620 --> 00:04:17,020
‫one, which again, I'm just going to copy called a plus B.

56
00:04:17,020 --> 00:04:23,800
‫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

57
00:04:23,800 --> 00:04:28,720
‫and put them in a plus B well, we can do this by going back to the C++ file.

58
00:04:28,750 --> 00:04:33,850
‫I'm going to add a new line underneath where I set my INT equal to nine.

59
00:04:34,030 --> 00:04:46,270
‫And I'm going to say that a plus B is equal to input A plus input B and again, notice the variable

60
00:04:46,270 --> 00:04:48,400
‫names are all without spaces.

61
00:04:48,400 --> 00:04:53,350
‫I've put spaces around my plus symbol to make it easier to read, and then I need to finish off the

62
00:04:53,350 --> 00:04:55,360
‫line with a semicolon.

63
00:04:55,360 --> 00:05:03,340
‫So if I go back into the editor and I recompile using live coding, I should see in my moving platform

64
00:05:03,340 --> 00:05:03,670
‫active.

65
00:05:03,850 --> 00:05:10,540
‫Charles Payne that we've got an input A and input B and an A plus B, so I can put some numbers into

66
00:05:10,540 --> 00:05:17,470
‫A and B, and that will actually update the values inside my class so I can put four and I can put five

67
00:05:17,470 --> 00:05:18,130
‫in here.

68
00:05:18,250 --> 00:05:25,120
‫And then when I hit play, my instruction to calculate the sum of those two will be executed.

69
00:05:25,120 --> 00:05:30,190
‫And if I go and look at the details pane for the moving platform, you can see that the value in A plus

70
00:05:30,220 --> 00:05:33,850
‫B has been set to nine, the sum of four and five.

71
00:05:33,940 --> 00:05:36,700
‫So I'd like to challenge you to add two floats together.

72
00:05:36,700 --> 00:05:38,680
‫We've just done it with 30 twos.

73
00:05:38,710 --> 00:05:39,910
‫Now let's try with the float.

74
00:05:39,910 --> 00:05:43,240
‫So add three new float variables to your header file.

75
00:05:43,240 --> 00:05:48,940
‫Call them something like input, float a, input float B and a plus B float.

76
00:05:48,940 --> 00:05:53,110
‫Again, we're having to use different names because you can't have multiple variables with the same

77
00:05:53,110 --> 00:05:58,330
‫name, even if there are different types and then add them together in big play so that you get the

78
00:05:58,330 --> 00:06:02,920
‫result in a plus b float pause video and have a go.

79
00:06:07,320 --> 00:06:07,680
‫Okay.

80
00:06:07,680 --> 00:06:10,290
‫So this is going to be fairly straightforward.

81
00:06:10,320 --> 00:06:11,910
‫Almost copying and pasting here.

82
00:06:11,910 --> 00:06:16,530
‫I'm going to take the three variables that I've got that were in 32.

83
00:06:16,620 --> 00:06:19,680
‫I'm going to paste a copy of these.

84
00:06:19,710 --> 00:06:24,600
‫I'm going to have input, a float, and I'm going to change its type to float as well.

85
00:06:24,630 --> 00:06:29,280
‫The input B I'm going to change the float and have the output be a float as well.

86
00:06:29,670 --> 00:06:36,970
‫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

87
00:06:36,970 --> 00:06:40,200
‫is just going to make it easy to see the difference between all of these.

88
00:06:40,200 --> 00:06:41,790
‫Let's go back into the editor.

89
00:06:41,790 --> 00:06:47,490
‫Stop playing, hit the live coding button and that's recompiled it.

90
00:06:47,490 --> 00:06:49,590
‫But I haven't actually put the code in Begin Play yet.

91
00:06:49,590 --> 00:06:56,850
‫So let's go back into Visual Studio code, go to the C++ file and this is where we need to have a new

92
00:06:56,850 --> 00:07:03,990
‫line at the end of the begin playback before the curly brace where we've got a plus B float is equal

93
00:07:03,990 --> 00:07:11,550
‫to input A float plus input B float and then a semicolon at the end of the line.

94
00:07:11,550 --> 00:07:17,190
‫And that will be executed after the other two lines are executed at the beginning of play.

95
00:07:17,190 --> 00:07:24,270
‫So we'll hit the live coding button, close the terminal down and hit play, then select the actor in

96
00:07:24,270 --> 00:07:24,780
‫the outline.

97
00:07:24,780 --> 00:07:30,450
‫I have a look in the details pane and well, we haven't added anything into the inputs, so let's have

98
00:07:30,450 --> 00:07:31,320
‫something in there.

99
00:07:31,320 --> 00:07:39,420
‫For starters, maybe I will have six and I'll put 7.3 in the A and B floats.

100
00:07:39,420 --> 00:07:44,910
‫Then I'm going to go ahead and hit play, select the actor, have a look in the details pane.

101
00:07:44,910 --> 00:07:50,280
‫And sure enough, we've got the result of 13.3 in the result variable.

102
00:07:50,370 --> 00:07:50,970
‫Great stuff.

103
00:07:50,970 --> 00:07:58,860
‫So in this lecture we have used the C++ file and begin play function to execute some of our own C++

104
00:07:58,860 --> 00:07:59,310
‫code.

105
00:07:59,310 --> 00:08:00,810
‫I'll see you in the next one.

