﻿1
00:00:04,420 --> 00:00:05,260
‫Hello and welcome.

2
00:00:05,260 --> 00:00:10,900
‫In this lecture, we're going to be looking at the C++ equivalent of pure blueprint functions, which

3
00:00:10,900 --> 00:00:12,400
‫is concept functions.

4
00:00:12,400 --> 00:00:17,950
‫We're going to see what this consort keyword is, how we use it, and how it restricts what we can do

5
00:00:17,950 --> 00:00:20,200
‫in our code to make us safer.

6
00:00:20,200 --> 00:00:22,000
‫Let's dive in and see how it works.

7
00:00:22,850 --> 00:00:29,600
‫Over in blue print, we were able to make a function pure, and that meant that we could take away the

8
00:00:29,600 --> 00:00:32,570
‫need for the execution pins.

9
00:00:32,600 --> 00:00:38,840
‫Now, in C++, we can do something a little bit stronger, which is to make a function constant.

10
00:00:39,230 --> 00:00:41,540
‫Now, what does it mean to make a function const?

11
00:00:41,570 --> 00:00:50,330
‫It means that that function cannot modify the state of your class, and it is actually enforced by the

12
00:00:50,330 --> 00:00:51,950
‫rules of the compiler.

13
00:00:51,950 --> 00:00:54,500
‫So let me give you an example.

14
00:00:54,500 --> 00:01:01,220
‫If I were to take our should platform return function, this is not a function that should modify anything

15
00:01:01,220 --> 00:01:07,790
‫in the class and it would be an error really if we forgot that because it would be weird if calling

16
00:01:07,790 --> 00:01:14,570
‫this and using in an if statement actually change the location of the platform or if it updated the

17
00:01:14,570 --> 00:01:16,460
‫move distance or platform velocity.

18
00:01:16,640 --> 00:01:20,540
‫So to keep ourselves honest to that, we can make it convert.

19
00:01:20,540 --> 00:01:28,610
‫And the way we do this is by putting the contact keyword after the parentheses of the function.

20
00:01:28,610 --> 00:01:30,920
‫We often leave a space here as well.

21
00:01:31,220 --> 00:01:38,390
‫Now, in order for it to correctly match up to the function in the C++ file, we actually have to put

22
00:01:38,390 --> 00:01:41,700
‫that const keyword over there as well.

23
00:01:41,720 --> 00:01:46,730
‫Now, like I said, this now makes it an error to do anything that would modify the class.

24
00:01:46,730 --> 00:01:53,480
‫So if in should platform return, I were to try and modify a class variable such as move distance.

25
00:01:53,480 --> 00:01:57,050
‫I might try and set move distance to 200.

26
00:01:57,320 --> 00:02:01,700
‫Let's see what happens when we go over to unreal and hit the compile button.

27
00:02:01,700 --> 00:02:02,020
‫Okay.

28
00:02:02,030 --> 00:02:03,830
‫You can see that there's now an error.

29
00:02:03,830 --> 00:02:09,230
‫It does fail to build and if you look at the error, you can see that there's something being said here

30
00:02:09,230 --> 00:02:15,110
‫about not being able to modify something because it's being accessed through a constant pointer.

31
00:02:15,110 --> 00:02:17,330
‫Now don't worry about that language.

32
00:02:17,330 --> 00:02:22,490
‫It's a little bit hard to interpret, but the point is the keyword const was there and it's saying it's

33
00:02:22,490 --> 00:02:25,760
‫not able to modify something and that is crucial.

34
00:02:25,760 --> 00:02:31,820
‫Also, if you called any functions that aren't marked as being const, that's also a problem.

35
00:02:31,820 --> 00:02:38,510
‫So while it's fine to call get actor location that is a cost function doesn't change anything just gets

36
00:02:38,510 --> 00:02:39,410
‫the location.

37
00:02:39,410 --> 00:02:48,140
‫It's not okay to call set actor location that hasn't been marked as const and so it's not valid.

38
00:02:48,140 --> 00:02:50,270
‫It could be modifying the class.

39
00:02:50,270 --> 00:02:55,760
‫So any function that's not marked as const is not allowed to be called from within a cost function.

40
00:02:56,090 --> 00:02:58,730
‫And so you can see a problem emerging.

41
00:02:58,730 --> 00:03:05,900
‫We can't actually call get distance moved from within our should platform return because should platform

42
00:03:05,900 --> 00:03:09,620
‫return is const but get distance moved hasn't been marked as such.

43
00:03:09,620 --> 00:03:16,280
‫So a quick charge to you get the distance moved const make get distance move cost so in the header and

44
00:03:16,280 --> 00:03:22,040
‫see p file you're going to add that const keyword to let C++ know that it shouldn't modify the class

45
00:03:22,220 --> 00:03:26,120
‫and does it compile now pause the video and have a go.

46
00:03:29,320 --> 00:03:29,770
‫Okay.

47
00:03:29,770 --> 00:03:30,420
‫Welcome back.

48
00:03:30,430 --> 00:03:32,830
‫So we're going to go over to the header file first.

49
00:03:32,830 --> 00:03:37,120
‫And after the closing parentheses, we're going to add a space concept and then we're going to go back

50
00:03:37,120 --> 00:03:38,530
‫to the C++ file.

51
00:03:38,530 --> 00:03:42,670
‫And after a distance moved parentheses, we're going to put space concept.

52
00:03:42,670 --> 00:03:47,680
‫Now the squiggly lines under get distance moved should update in a little bit of time.

53
00:03:47,680 --> 00:03:53,290
‫But if we go over to Unreal and hit the compile button, that will be the ultimate arbiter as to whether

54
00:03:53,290 --> 00:03:55,780
‫the compiler likes what we've done or not.

55
00:03:55,780 --> 00:04:02,260
‫And this time the compilation has succeeded successfully, which means that we have correctly updated

56
00:04:02,260 --> 00:04:03,100
‫our code.

57
00:04:03,100 --> 00:04:08,860
‫And that makes sense because there is nothing being called from get distance moved that is not concept,

58
00:04:08,860 --> 00:04:10,780
‫it's not trying to change anything.

59
00:04:10,780 --> 00:04:18,250
‫And where we are calling a function in should platform return, we are only using const functions and

60
00:04:18,250 --> 00:04:25,060
‫this is a way for us to increase the safety of our code because functions won't be changing stuff without

61
00:04:25,060 --> 00:04:26,230
‫us knowing about it.

62
00:04:26,230 --> 00:04:30,790
‫Now as a little exercise to your self, you might want to go through and look at all the functions that

63
00:04:30,790 --> 00:04:35,380
‫we've been using up to now and see which one of them are marked as constant, which ones aren't.

64
00:04:35,410 --> 00:04:41,890
‫You can do this by simply hovering over a function such as get actor location and seeing whether the

65
00:04:41,890 --> 00:04:46,240
‫pop up that is included, whether after the parentheses it has contact or not.

66
00:04:46,240 --> 00:04:53,050
‫You can see with get active location hovering over it gives me const hovering over get name also gives

67
00:04:53,050 --> 00:04:55,360
‫me that this is const and it makes sense.

68
00:04:55,360 --> 00:04:59,560
‫Neither of those two should be changing the state of our class.

69
00:04:59,770 --> 00:05:05,110
‫Now if I hovered over tick you can see this one isn't marked as const and that makes sense because Tick

70
00:05:05,110 --> 00:05:11,350
‫is usually used for updating how a class is positioned in the world or any of the state of the class.

71
00:05:11,500 --> 00:05:13,360
‫And we've got our vector.

72
00:05:13,360 --> 00:05:18,880
‫If we hover over, get safe, normal, you can see that it's marked as const that's because get safe

73
00:05:18,880 --> 00:05:24,790
‫normal doesn't actually modify the vector, it just returns a value with a new vector in it.

74
00:05:24,790 --> 00:05:25,510
‫So that's it.

75
00:05:25,510 --> 00:05:32,620
‫For this lecture we've learnt about adding const to our functions to increase the safety of our code.

76
00:05:32,620 --> 00:05:34,060
‫I'll see you in the next one.

