﻿1
00:00:04,270 --> 00:00:05,530
‫Hello and welcome.

2
00:00:05,530 --> 00:00:11,830
‫In this lecture, we're going to be learning how we can remove execution pins from our own functions

3
00:00:11,830 --> 00:00:15,760
‫and how and when it's a good idea to do this.

4
00:00:15,760 --> 00:00:16,690
‫Let's dive in.

5
00:00:16,960 --> 00:00:23,560
‫So it's a little bit weird that our has ammo function now has execution pins when previously we didn't

6
00:00:23,560 --> 00:00:27,610
‫need that and that's kind of made our code a little bit worse.

7
00:00:27,610 --> 00:00:29,110
‫So what is that?

8
00:00:29,110 --> 00:00:35,710
‫Why has that happened and why do some function nodes require execution pins and other ones don't?

9
00:00:35,710 --> 00:00:39,850
‫Well, it all comes down to something called a side effect.

10
00:00:39,850 --> 00:00:42,250
‫So what is a side effect?

11
00:00:42,250 --> 00:00:49,570
‫Essentially, a side effect is when a function has some sort of observable effect after it has been

12
00:00:49,570 --> 00:00:50,260
‫run.

13
00:00:50,350 --> 00:00:52,090
‫Now, what do I mean by this?

14
00:00:52,090 --> 00:00:57,060
‫Let's have a look at some examples of functions with side effects so that you can understand prints,

15
00:00:57,400 --> 00:00:58,420
‫print, string.

16
00:00:58,870 --> 00:01:01,090
‫After you have run print string.

17
00:01:01,090 --> 00:01:05,860
‫It is quite clear that something has happened because there is now a string on your screen.

18
00:01:05,860 --> 00:01:12,490
‫You can see that there is a string add impulse that clearly has an observable effect because suddenly

19
00:01:12,490 --> 00:01:16,960
‫the thing that you've added impulse to goes flying off into the distance.

20
00:01:17,440 --> 00:01:25,210
‫Set ammo has an observable effect because after you've set ammo, the value in that variable has changed.

21
00:01:25,210 --> 00:01:29,320
‫Now at this point, you might be wondering, well, surely every function has an observable effect.

22
00:01:29,320 --> 00:01:31,540
‫Otherwise, what is the point of it?

23
00:01:31,600 --> 00:01:32,740
‫Well, not quite.

24
00:01:32,740 --> 00:01:35,590
‫There are such things as pure functions.

25
00:01:35,590 --> 00:01:37,600
‫What is a pure function?

26
00:01:38,200 --> 00:01:41,170
‫It's basically a function with no side effects.

27
00:01:41,170 --> 00:01:45,040
‫However, it does have return values.

28
00:01:45,040 --> 00:01:47,380
‫So there are values coming out of that function.

29
00:01:47,380 --> 00:01:53,830
‫And that is the entire purpose of the function is to calculate and give us those values.

30
00:01:53,830 --> 00:01:57,250
‫So again, let's have a look at some examples because this will make it a lot clearer.

31
00:01:57,520 --> 00:01:58,570
‫Get ammo.

32
00:01:58,570 --> 00:02:01,090
‫That is a pure function.

33
00:02:01,090 --> 00:02:06,640
‫It has no side effects because you can get ammo as many times as you like.

34
00:02:06,640 --> 00:02:12,940
‫And unless you do something with the value that you got out of it, your program, your game is not

35
00:02:12,940 --> 00:02:14,380
‫going to be any different.

36
00:02:14,380 --> 00:02:20,320
‫You can just keep getting ammo all the time, all all the way through your game and nothing is going

37
00:02:20,320 --> 00:02:22,780
‫to be observable different about your game.

38
00:02:22,810 --> 00:02:24,910
‫Get active forward vector.

39
00:02:24,910 --> 00:02:27,730
‫That's another one where you could get it as many times as you like.

40
00:02:27,730 --> 00:02:34,330
‫It's not going to change anything until you do something with the value returned and all these mathematical

41
00:02:34,330 --> 00:02:37,900
‫functions like multiply minus greater.

42
00:02:37,900 --> 00:02:46,480
‫All of these nodes don't have an effect on your project until their results are used for some greater

43
00:02:46,480 --> 00:02:47,260
‫purpose.

44
00:02:47,260 --> 00:02:53,290
‫So these are called pure functions, and you can probably see that these are exactly the nodes that

45
00:02:53,290 --> 00:02:56,920
‫didn't have any execution pins because they don't need them.

46
00:02:56,920 --> 00:03:04,330
‫It doesn't matter when I run get ammo or whether I run it 50 times or one time, it's not going to matter

47
00:03:04,330 --> 00:03:08,320
‫because it's got no observable side effects.

48
00:03:08,560 --> 00:03:13,540
‫So how can we indicate to blueprint that a function is pure?

49
00:03:13,660 --> 00:03:16,150
‫Well, let's have a look at my test function here.

50
00:03:16,150 --> 00:03:20,800
‫So I'm going to select it in the event graph or over in the my blueprint pane.

51
00:03:21,010 --> 00:03:25,870
‫And you can see that in the details pane there is a checkbox labeled pure.

52
00:03:25,870 --> 00:03:31,150
‫So if you check that checkbox, notice the execution pins just disappeared.

53
00:03:31,150 --> 00:03:35,410
‫And all we've got now are the input and outputs of this function.

54
00:03:35,410 --> 00:03:40,120
‫Now this function will get run whenever we need the output.

55
00:03:40,120 --> 00:03:46,750
‫So if I plug this output into a print string, then it's going to run this function before the print

56
00:03:46,750 --> 00:03:51,910
‫string so that it has the result in time to execute the print string.

57
00:03:51,910 --> 00:03:57,700
‫We can do the same with any function where there are no side effects, where there are no observable

58
00:03:57,700 --> 00:04:01,450
‫effects such as our has ammo function.

59
00:04:01,450 --> 00:04:06,280
‫So I'd like to challenge you to make has ammo into a pure function.

60
00:04:06,280 --> 00:04:10,150
‫So select it and set that checkbox to make it pure.

61
00:04:10,360 --> 00:04:14,380
‫And what happens to the execution flow now.

62
00:04:14,380 --> 00:04:17,380
‫What currently it was hooked up through the function.

63
00:04:17,380 --> 00:04:18,600
‫Where does it go?

64
00:04:18,610 --> 00:04:20,770
‫Pause the video and have a go.

65
00:04:23,870 --> 00:04:24,410
‫Okay.

66
00:04:24,410 --> 00:04:25,360
‫Welcome back.

67
00:04:25,370 --> 00:04:27,460
‫So let's select has ammo.

68
00:04:27,470 --> 00:04:34,760
‫We'll check the pure check box and instantly you see that the execution pins disappear and the execution

69
00:04:34,760 --> 00:04:41,360
‫flow that previously flowed through has ammo is now going directly on to our branch statement, which

70
00:04:41,360 --> 00:04:48,770
‫is exactly what we want now has ammo is just going to be run as soon as the branch statement is run

71
00:04:48,770 --> 00:04:51,820
‫to get hold of the return value.

72
00:04:51,830 --> 00:05:00,500
‫Now a word of caution only use this pure checkbox when you are sure that a function has no side effects.

73
00:05:00,500 --> 00:05:08,180
‫Common cases and use cases for this are when your function is simply trying to calculate a value such

74
00:05:08,180 --> 00:05:09,290
‫as has ammo.

75
00:05:09,290 --> 00:05:17,540
‫And that is why this function doesn't follow the convention of having a verb naming because it isn't

76
00:05:17,540 --> 00:05:21,980
‫doing something, it is getting something or it is answering a question.

77
00:05:21,980 --> 00:05:24,680
‫In this case, the question is has ammo.

78
00:05:24,680 --> 00:05:29,330
‫And in fact, we could even put a little question mark in our function, naming something that you can

79
00:05:29,330 --> 00:05:35,390
‫do in Blueprint that you can't do in C++, which allows us to see that, yes, this function is answering

80
00:05:35,390 --> 00:05:36,080
‫a question.

81
00:05:36,080 --> 00:05:37,850
‫It isn't trying to do something.

82
00:05:37,850 --> 00:05:44,270
‫So often the distinction between pure and non pure functions are whether it fits with a verb or whether

83
00:05:44,270 --> 00:05:46,490
‫it fits with a question.

84
00:05:46,490 --> 00:05:49,160
‫So to review, let's have a look at our glossary.

85
00:05:49,430 --> 00:05:56,660
‫A side effect is an observable effect of a function we've seen and we've seen examples of those functions.

86
00:05:56,660 --> 00:06:04,010
‫And a pure function is a function with no side effects, such as these getting functions or question

87
00:06:04,010 --> 00:06:05,270
‫answering functions.

88
00:06:05,390 --> 00:06:06,680
‫That's it for this lecture.

89
00:06:06,680 --> 00:06:08,120
‫I'll see you in the next one.

