﻿1
00:00:04,500 --> 00:00:05,580
‫Hello and welcome.

2
00:00:05,580 --> 00:00:12,360
‫In this lecture, we're going to be adding variables to our moving platform actor so that we can see

3
00:00:12,360 --> 00:00:16,560
‫them in the editor and we're going to be doing this entirely in C++.

4
00:00:16,560 --> 00:00:17,790
‫Let's dive in.

5
00:00:18,360 --> 00:00:23,640
‫So the first concept that we're going to be learning in C++ is how to create variables.

6
00:00:23,640 --> 00:00:25,890
‫We've seen them before in Blueprint.

7
00:00:25,890 --> 00:00:30,090
‫We know that they're like boxes that we can put values into and that they have types.

8
00:00:30,240 --> 00:00:34,890
‫Well, all of that still applies over in C++, except how we create them.

9
00:00:34,890 --> 00:00:39,810
‫So previously, when we did this in Blueprint, if I go and open up a level blueprint just to show you

10
00:00:39,810 --> 00:00:45,150
‫and refresh your memory, then we had this, my blueprint pane, and that was where we added things

11
00:00:45,150 --> 00:00:48,300
‫like functions and variables and so on.

12
00:00:48,300 --> 00:00:51,510
‫And then we had the event graph where we actually program stuff in.

13
00:00:51,510 --> 00:00:53,970
‫Now C++ is very similar.

14
00:00:53,970 --> 00:01:00,270
‫We have two different locations for doing the configuration of our variables and functions and then

15
00:01:00,270 --> 00:01:02,190
‫when we actually do the implementation.

16
00:01:02,190 --> 00:01:07,500
‫So let's go over to Visual Studio code and open up the Visual Studio Code Project and have a look.

17
00:01:07,500 --> 00:01:10,650
‫In the left hand bar there is an Explorer tab.

18
00:01:10,650 --> 00:01:16,710
‫Go and open that up and you can see basically a hierarchical map of your files.

19
00:01:16,710 --> 00:01:22,800
‫So we've got at the top level five and Obstacle Assault or our own project here, you can open that

20
00:01:22,800 --> 00:01:24,300
‫up and you can see some of the folders here.

21
00:01:24,300 --> 00:01:28,410
‫The one we're interested in is the source folder, which is where we can find all of our C++.

22
00:01:28,410 --> 00:01:34,800
‫Then in there there is the obstacle assault folder and then there's two files with the name of moving

23
00:01:34,800 --> 00:01:35,340
‫platform.

24
00:01:35,340 --> 00:01:44,250
‫One is a dot file and one is at DOT CP, the DOT stands for Header and CP it stands for C++.

25
00:01:44,250 --> 00:01:52,170
‫So this is essentially a bit like having a my blueprints where you can add in the functions and variables

26
00:01:52,170 --> 00:01:58,710
‫and stuff that is resides in the header file, the FF file, and then we have the dot CP where we can

27
00:01:58,710 --> 00:02:03,060
‫actually essentially like the event graph, we can actually do the coding itself.

28
00:02:03,330 --> 00:02:06,600
‫So we're going to open up the moving platform.

29
00:02:06,840 --> 00:02:12,180
‫H Because we want to add in a new variable and you'll notice that there's a whole bunch of stuff in

30
00:02:12,180 --> 00:02:12,300
‫here.

31
00:02:12,300 --> 00:02:18,180
‫We're not going to go into the details of this, but what is most interesting is that it is defining

32
00:02:18,180 --> 00:02:20,910
‫what it means to be a moving platform class.

33
00:02:20,910 --> 00:02:23,370
‫You can see a couple of keywords here that are interesting.

34
00:02:23,370 --> 00:02:25,290
‫There's a class keyword on line ten.

35
00:02:25,290 --> 00:02:32,160
‫There is the moving platforms which is prefixed with an A, meaning that it's an actor, and then there

36
00:02:32,160 --> 00:02:36,270
‫are some curly braces on line 11 and 26.

37
00:02:36,270 --> 00:02:42,540
‫And between these matched curly braces, you can see there is the declaration of a few things.

38
00:02:42,540 --> 00:02:46,200
‫There are functions in here and this is where we can also put our variables.

39
00:02:46,440 --> 00:02:49,680
‫So we're going to add our variables to the end of this declaration.

40
00:02:49,680 --> 00:02:57,030
‫So just before we close that curly brace, we can add some new lines and start writing our new variables

41
00:02:57,030 --> 00:02:57,810
‫in here.

42
00:02:58,140 --> 00:03:02,970
‫And just like in a variable in blueprint, we have a type and we have a variable name and we can also

43
00:03:02,970 --> 00:03:04,440
‫give it a default value.

44
00:03:04,620 --> 00:03:13,530
‫So the first thing is we want to maybe create an integer and in C++, in Unreal C++, an integer is

45
00:03:13,530 --> 00:03:20,550
‫denoted with the keyword INT 32 basically telling us how big a value we can store in here.

46
00:03:20,550 --> 00:03:22,500
‫And no, it's not 32.

47
00:03:22,530 --> 00:03:23,820
‫It's much, much larger than that.

48
00:03:23,820 --> 00:03:28,620
‫32 is the number of bits allocated to the integer.

49
00:03:28,650 --> 00:03:33,750
‫Now we've got to go for a variable name and just like the classes, we cannot have spaces in our variable

50
00:03:33,750 --> 00:03:34,200
‫names.

51
00:03:34,200 --> 00:03:38,670
‫So instead we use capital letters to denote our words.

52
00:03:38,670 --> 00:03:41,520
‫So I'm going to call it my int notice.

53
00:03:41,520 --> 00:03:44,520
‫I'm capitalizing both of the beginnings of those words.

54
00:03:44,520 --> 00:03:50,730
‫Then I can give it a default value and we do this with an equal sign so we have equals, and then we

55
00:03:50,730 --> 00:03:53,970
‫can give it a value such as 99, for example.

56
00:03:54,150 --> 00:03:59,880
‫Now notice that I'm putting spaces after my and before my equals sign.

57
00:03:59,880 --> 00:04:04,140
‫That's not actually strictly necessary, but it does make it easier and nicer to read.

58
00:04:04,140 --> 00:04:05,760
‫So I include that.

59
00:04:05,760 --> 00:04:11,070
‫Now you do obviously need a space between the 32 and the actual name of the integer.

60
00:04:11,070 --> 00:04:12,690
‫Without that, things don't work.

61
00:04:12,960 --> 00:04:18,210
‫And then to finish off this declaration, you need to put a semicolon on the end of the line to tell

62
00:04:18,240 --> 00:04:20,520
‫C++ that you are done.

63
00:04:20,520 --> 00:04:26,010
‫The fact that there is new lines and that there are spaces and tabs, all of this doesn't actually matter

64
00:04:26,010 --> 00:04:26,790
‫to C++.

65
00:04:26,790 --> 00:04:30,360
‫You could run all of this stuff together and this would still be the same.

66
00:04:30,360 --> 00:04:34,230
‫I could take out the spaces here that would still work just fine.

67
00:04:34,230 --> 00:04:38,040
‫As far as C++ is concerned, this is all perfectly valid C++.

68
00:04:38,040 --> 00:04:40,590
‫It's just really awful, awful to read.

69
00:04:40,800 --> 00:04:46,350
‫So what we do is we keep this level of indentation with a tab and we keep this level of spacing as well

70
00:04:46,350 --> 00:04:48,000
‫just for our sanity.

71
00:04:48,450 --> 00:04:52,380
‫Now this isn't currently visible over in the Blueprint Editor.

72
00:04:52,380 --> 00:04:57,540
‫To make it visible, we need to give a little bit of a heads up to Unreal that there is a variable it

73
00:04:57,540 --> 00:04:58,410
‫should display.

74
00:04:58,410 --> 00:05:03,770
‫The way we do this is on the preceding line we type you property all caps.

75
00:05:04,330 --> 00:05:11,170
‫Then open parentheses and then using our what's called camel case, basically where we capitalize the

76
00:05:11,170 --> 00:05:17,460
‫first letter of each word we type edit anywhere like so, and then we close the parentheses.

77
00:05:17,470 --> 00:05:19,420
‫No need for a semicolon at the end of this line.

78
00:05:19,420 --> 00:05:26,050
‫That's not correct, because it's actually part of the same declaration as the integer down on the line

79
00:05:26,050 --> 00:05:26,770
‫below it.

80
00:05:27,160 --> 00:05:32,470
‫But we put them on separate lines just to make our life easier for reading this.

81
00:05:32,470 --> 00:05:39,100
‫So we've got you property edit anywhere in 30 to my int and then equals sign and then 99.

82
00:05:39,100 --> 00:05:42,790
‫And it's important that you save your files here by default.

83
00:05:42,790 --> 00:05:44,980
‫You need to hit control as to do this.

84
00:05:45,100 --> 00:05:51,160
‫If you go to the file menu, you can hit autosave here as an option so that you don't have to hit save

85
00:05:51,160 --> 00:05:52,660
‫every time you change things.

86
00:05:52,750 --> 00:05:53,890
‫Now that's what I do.

87
00:05:53,890 --> 00:06:00,190
‫So with that done, we could go ahead and close the editor, recompile and re-open it and we would have

88
00:06:00,190 --> 00:06:03,100
‫a new variable on our moving platform.

89
00:06:03,100 --> 00:06:08,560
‫Actor But that's a bit of a pain if we had to close the editor every time we made a code change.

90
00:06:08,560 --> 00:06:12,160
‫So there is a feature in Unreal called Live Coding.

91
00:06:12,160 --> 00:06:18,130
‫I'm just going to go and close down the level blueprint and show you that there is a button down in

92
00:06:18,130 --> 00:06:21,970
‫the bottom right which looks like a set of cubes dissolving off into the distance.

93
00:06:21,970 --> 00:06:27,790
‫If you hover over it, it says recompile and reload C++ code for games on the fly, click that button

94
00:06:27,790 --> 00:06:36,340
‫and a terminal will show up with a log of messages that are the results of compiling your code.

95
00:06:36,730 --> 00:06:41,380
‫Now, this will run a lot faster than it previously did because it's not the first time it's running,

96
00:06:41,470 --> 00:06:46,210
‫and when it's complete, it's going to give you a notification saying that live coding succeeded.

97
00:06:46,450 --> 00:06:53,650
‫Now it's important that if live coding didn't succeed, that you copy the results of the log that come

98
00:06:53,650 --> 00:06:59,530
‫before the error and post those in the Q&A if you need to ask for help, but also just double check

99
00:06:59,530 --> 00:07:03,340
‫and make sure that your syntax matches what I've typed on screen.

100
00:07:03,520 --> 00:07:09,910
‫Oftentimes we will find that it's simply a typo that people have made and it doesn't compile because

101
00:07:09,910 --> 00:07:10,630
‫of that.

102
00:07:10,630 --> 00:07:18,550
‫Pay close attention to your capitalization as capitalization is often very important and also your semicolons.

103
00:07:18,550 --> 00:07:20,950
‫People often forget to include those.

104
00:07:21,100 --> 00:07:26,200
‫So now I'd like to challenge you to add your own variable on a new line in that header file.

105
00:07:26,200 --> 00:07:30,190
‫So make it a float or bull type and give it a different name.

106
00:07:30,190 --> 00:07:32,020
‫Variables cannot share the same name.

107
00:07:32,020 --> 00:07:33,100
‫They have to be different.

108
00:07:33,340 --> 00:07:34,990
‫Give it a default value.

109
00:07:34,990 --> 00:07:38,050
‫So if it's a float, the default value needs to be of the format.

110
00:07:38,050 --> 00:07:39,970
‫Something point, something, something.

111
00:07:39,970 --> 00:07:47,560
‫If it's a boolean, it needs to be either true or false all lowercase recompile using live coding and

112
00:07:47,560 --> 00:07:53,860
‫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

113
00:07:53,860 --> 00:07:55,210
‫written it the right way.

114
00:07:55,300 --> 00:07:57,250
‫Pause video and have a go.

115
00:08:00,500 --> 00:08:00,890
‫Okay.

116
00:08:00,890 --> 00:08:01,790
‫Welcome back.

117
00:08:02,060 --> 00:08:06,440
‫So let's go back into Visual Studio code and we're going to add a variable.

118
00:08:06,440 --> 00:08:09,470
‫So first of all, we're going to be making it a new property.

119
00:08:09,470 --> 00:08:13,400
‫It's open parentheses and edit anywhere and close parentheses.

120
00:08:13,400 --> 00:08:19,670
‫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

121
00:08:19,670 --> 00:08:21,530
‫up the tradition here.

122
00:08:21,620 --> 00:08:27,320
‫And we're going to give it a default value of 5.99 and then put a semicolon at the end of the line,

123
00:08:27,320 --> 00:08:31,420
‫make sure that you have all of the components of what I've just typed in here.

124
00:08:32,060 --> 00:08:34,130
‫Then I'm going to add some more spaces.

125
00:08:34,130 --> 00:08:36,980
‫Notice that I'm adding multiple new lines.

126
00:08:36,980 --> 00:08:39,110
‫It doesn't really matter how many new lines you have.

127
00:08:39,110 --> 00:08:40,670
‫Whitespace doesn't matter.

128
00:08:40,670 --> 00:08:44,750
‫As I've said, then we're going to have a new property again.

129
00:08:44,930 --> 00:08:51,920
‫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

130
00:08:51,920 --> 00:08:55,760
‫default value of true and put a semicolon on the end there.

131
00:08:55,760 --> 00:08:58,340
‫Now you don't have to save if you've got autosave, but you do.

132
00:08:58,340 --> 00:09:01,220
‫If you don't, then we go back into the editor.

133
00:09:01,220 --> 00:09:09,080
‫We hit the live coding button in the bottom right and the log will come up and hopefully we'll succeed,

134
00:09:09,200 --> 00:09:10,700
‫which it has done for me.

135
00:09:10,700 --> 00:09:16,490
‫But I just want to show you what would happen if I'd made a mistake, such as leaving out the semicolon

136
00:09:16,490 --> 00:09:20,300
‫and the end of this line instead of a nice, reassuring green.

137
00:09:20,300 --> 00:09:25,550
‫At the end of the log you will have a red build failed showing up instead.

138
00:09:25,550 --> 00:09:31,520
‫So this is important that if you get a build, fail, first things first, go and check your code,

139
00:09:31,520 --> 00:09:38,030
‫make sure it matches mine, because as I've said, it's often just a typo and if not, then copy this

140
00:09:38,030 --> 00:09:42,290
‫and put it in the Q&A and ask for some help.

141
00:09:42,350 --> 00:09:49,220
‫That said, I'm going to correct my code now and recompile it, and when that has succeeded, I can

142
00:09:49,220 --> 00:09:56,180
‫close down the log and I can look in the outline for the moving platform, and I can select the moving

143
00:09:56,180 --> 00:10:00,590
‫platform to look in the details pane and see all of the variables that I've created.

144
00:10:00,590 --> 00:10:07,040
‫They my int the my float and the my bool and the default values that we have given them there.

145
00:10:07,040 --> 00:10:10,040
‫Now, it's worth noting that these are indeed just defaults.

146
00:10:10,040 --> 00:10:16,730
‫So if you were to override those values in the editor, in Blueprint or in the level, and then you

147
00:10:16,730 --> 00:10:21,110
‫go and change them in C++, the changes in C++ will no longer have an effect.

148
00:10:21,110 --> 00:10:22,580
‫They were only default values.

149
00:10:22,580 --> 00:10:28,580
‫So if you've made changes in Blueprint, that takes precedence and overrides anything you did in C++.

150
00:10:28,610 --> 00:10:29,360
‫Fantastic stuff.

151
00:10:29,360 --> 00:10:35,420
‫We've created our first variables, our first code in C++ and brought it in using live coding.

152
00:10:35,420 --> 00:10:37,250
‫I will see you in the next lecture.

