﻿1
00:00:03,980 --> 00:00:05,210
‫Hello and welcome.

2
00:00:05,210 --> 00:00:11,840
‫In this lecture, we're going to be taking our mess of blueprints and organizing it neatly into functions

3
00:00:11,840 --> 00:00:17,870
‫so that we can easily read and understand what's going on in our Blueprint Event graph.

4
00:00:17,870 --> 00:00:19,850
‫Let's dive in and see how to do this.

5
00:00:20,600 --> 00:00:26,030
‫Now, if we zoom out on our event graph, we can see that it's starting to get very noisy.

6
00:00:26,030 --> 00:00:28,610
‫I can also see a couple of nodes that aren't connected up to anything.

7
00:00:28,610 --> 00:00:33,920
‫There's a get node which I'm going to delete, an asset, M0 node, but you can see it's getting very

8
00:00:33,920 --> 00:00:34,340
‫noisy.

9
00:00:34,340 --> 00:00:36,860
‫It's getting hard to understand what's going on here.

10
00:00:36,860 --> 00:00:45,380
‫Now, a typical solution that people do is to say, Well, this block of nodes here is all about spawning

11
00:00:45,380 --> 00:00:46,010
‫the projectile.

12
00:00:46,010 --> 00:00:47,300
‫So I can select these.

13
00:00:47,300 --> 00:00:54,080
‫I can hit the C key and it is going to put a block around this called a comment block.

14
00:00:54,080 --> 00:01:02,360
‫And then I could type in spawn projectiles and that lets me know what that block of code is doing.

15
00:01:02,360 --> 00:01:05,410
‫That is certainly one way to organize your code.

16
00:01:05,420 --> 00:01:12,680
‫However, in my opinion, it's not the best way and we have got better tools for doing this called functions.

17
00:01:12,680 --> 00:01:14,420
‫So what are functions?

18
00:01:14,780 --> 00:01:20,510
‫Functions execute blocks of blueprint and make our games do things.

19
00:01:20,510 --> 00:01:25,310
‫Many nodes that are provided by Unreal Real are actually functions already.

20
00:01:25,310 --> 00:01:27,380
‫So we've encountered these.

21
00:01:27,380 --> 00:01:33,170
‫When you go and zoom in say on our spawn actor projectile node, that is a function.

22
00:01:33,170 --> 00:01:39,410
‫When we look at add impulse, that's a function to in fact most every one of these nodes, even things

23
00:01:39,410 --> 00:01:44,090
‫like get forward active vector that doesn't have an execution pin and isn't blue is also a function

24
00:01:44,090 --> 00:01:50,300
‫you can tell because it has a little f symbol in the top left that stands for function, but we aren't

25
00:01:50,300 --> 00:01:52,370
‫restricted to the functions Unreal provides.

26
00:01:52,370 --> 00:01:56,150
‫We can create our own functions too, and it's really easy to do.

27
00:01:56,150 --> 00:01:57,050
‫In Blueprint.

28
00:01:57,050 --> 00:02:02,630
‫They allow us to stay organized and they allow us to reuse blocks of code.

29
00:02:02,630 --> 00:02:06,500
‫So that's better than a comment because a comment you have to copy and paste.

30
00:02:06,500 --> 00:02:12,620
‫Whereas a function allows us to share one block of code between multiple places of use.

31
00:02:12,620 --> 00:02:19,280
‫So let's take a look at how we can turn something like this in to a block of code.

32
00:02:19,610 --> 00:02:24,620
‫Well, we can delete the comment block, first of all, because I don't think that's necessary.

33
00:02:24,620 --> 00:02:31,340
‫Instead, I'm going to select all of the nodes from get player -- all the way to add impulse, right

34
00:02:31,340 --> 00:02:35,180
‫click on them and select collapse to function.

35
00:02:35,360 --> 00:02:42,920
‫And that creates a new block and replaces all of those previous nodes that were there before.

36
00:02:42,920 --> 00:02:46,520
‫Now I'm going to give this function a name you can see over in the left hand pane.

37
00:02:46,520 --> 00:02:53,870
‫It's created a new function under the function section in the my blueprint pane and it's got it selected

38
00:02:53,870 --> 00:02:59,480
‫for me to automatically rename because we should definitely, definitely rename these functions and

39
00:02:59,480 --> 00:03:02,120
‫I'm going to call it spawn projectile.

40
00:03:02,120 --> 00:03:09,530
‫And good naming is absolutely vital in program because code is essentially all about communication.

41
00:03:09,530 --> 00:03:14,750
‫We're trying to communicate with ourselves so that we can understand what we've written.

42
00:03:14,750 --> 00:03:17,210
‫We're trying to communicate with ourselves in the future.

43
00:03:17,210 --> 00:03:21,050
‫When we come back to this project, have forgotten why we've written it a certain way.

44
00:03:21,050 --> 00:03:25,310
‫And when you work in teams, you're trying to communicate with the rest of your team too.

45
00:03:25,340 --> 00:03:31,130
‫Some say that a good metric for code quality is the what the heck's per minute.

46
00:03:31,130 --> 00:03:36,500
‫So if you're reading a piece of code and you're going, What the heck, 50 times a minute, that's pretty

47
00:03:36,500 --> 00:03:41,060
‫bad code if you're reading it and only going, What the heck, once or twice a minute, then that's

48
00:03:41,060 --> 00:03:44,930
‫probably pretty good code and you're always going to get somewhat the hex.

49
00:03:44,930 --> 00:03:49,220
‫So we're just trying to reduce that number their functions.

50
00:03:49,220 --> 00:03:51,650
‫Crucially, we tend to name functions as verbs.

51
00:03:51,650 --> 00:03:58,070
‫There are some exceptions to this rule that we'll talk about later on, but mostly functions are things

52
00:03:58,070 --> 00:03:59,270
‫that are doing something.

53
00:03:59,270 --> 00:04:06,230
‫So we use doing words and if you're unsure about how to name this function, then if you're working

54
00:04:06,230 --> 00:04:11,420
‫in a team, you could just talk to somebody else and ask them what they think you should name it and

55
00:04:11,420 --> 00:04:14,060
‫what kind of things make sense to them.

56
00:04:14,060 --> 00:04:19,760
‫You often find that you're so ingrained into a problem that you can't think of how to talk about it.

57
00:04:19,760 --> 00:04:26,450
‫So you may be asking yourself, Well, where did all my code go when I created this spawn projectile

58
00:04:26,450 --> 00:04:27,170
‫function?

59
00:04:27,320 --> 00:04:34,610
‫Well, you can get back to that detail of how this function is actually implemented by either double

60
00:04:34,610 --> 00:04:40,700
‫clicking it in the event graph or clicking on it in the My Blueprints tab or double clicking it on the

61
00:04:40,700 --> 00:04:41,840
‫My Blueprints tab.

62
00:04:41,840 --> 00:04:48,950
‫And you can see that we've got all of our nodes still resident here just now they're in a separate tab

63
00:04:48,950 --> 00:04:56,240
‫so you can go back to your event graph tab or you can dive in to the Spawn projectile tab which shows

64
00:04:56,240 --> 00:04:57,980
‫just that one function.

65
00:04:58,610 --> 00:05:04,940
‫And you can see there's a new node here, this little purple node, which is the entry point to our

66
00:05:04,940 --> 00:05:05,480
‫function.

67
00:05:05,480 --> 00:05:09,800
‫This says this is what we should do when the function starts.

68
00:05:09,800 --> 00:05:11,180
‫It's just an entry point.

69
00:05:11,180 --> 00:05:16,880
‫And whenever it's called, that node is going to trigger and trigger all the rest of the nodes inside

70
00:05:16,880 --> 00:05:17,690
‫of our function.

71
00:05:17,690 --> 00:05:19,820
‫So going back to the event graph and.

72
00:05:19,960 --> 00:05:23,190
‫Zooming out, maybe bringing our nodes together a bit.

73
00:05:23,200 --> 00:05:26,850
‫Now that we've made our node graph so much smaller and easier to read.

74
00:05:26,860 --> 00:05:33,760
‫I think there are a couple more candidates here for us to pull out functions and replace things.

75
00:05:33,940 --> 00:05:38,380
‫One of them would be where we are printing out ammo.

76
00:05:38,380 --> 00:05:43,780
‫So in this case, printing ammo is something that we do in two places.

77
00:05:43,780 --> 00:05:47,800
‫We do it in begin play and we do it down in spawn projectile.

78
00:05:48,190 --> 00:05:54,190
‫And then the other place, the other thing, piece of functionality that we could extract into a function

79
00:05:54,190 --> 00:06:01,200
‫would be where we are decreasing the ammo that could be put into a decrease ammo function instead.

80
00:06:01,210 --> 00:06:09,640
‫So just before I set you the challenge, I would like to delete the conversion node and print string

81
00:06:09,640 --> 00:06:15,280
‫node at the after the spawn projectile because this is going to complicate matters.

82
00:06:15,280 --> 00:06:21,370
‫I would like to copy the functionality from up above so that they both look exactly the same.

83
00:06:21,370 --> 00:06:29,200
‫So if we copy the three nodes from Begin Play and paste them down below and connect up the execution

84
00:06:29,200 --> 00:06:35,050
‫pins, then you can see we've got exactly the same set of nodes here, the same functionality between

85
00:06:35,050 --> 00:06:39,970
‫the two, and there's no confusion when we come to extract this into a function.

86
00:06:39,970 --> 00:06:43,240
‫So I'd like to challenge you to make some more functions.

87
00:06:43,240 --> 00:06:47,140
‫The first one I would recommend doing is the decrease ammo.

88
00:06:47,140 --> 00:06:51,850
‫So taking the three nodes involved in decreasing ammo, extracting those into a function and giving

89
00:06:51,850 --> 00:06:52,720
‫them a name.

90
00:06:52,960 --> 00:06:56,020
‫And then the second one is print ammo.

91
00:06:56,020 --> 00:07:03,310
‫So there you're going to be extracting out the functionality of getting, converting and printing the

92
00:07:03,310 --> 00:07:05,740
‫ammo into a single function.

93
00:07:05,740 --> 00:07:11,500
‫And because we've got two sites where we're printing ammo, you might want to delete one of them and

94
00:07:11,500 --> 00:07:17,020
‫bring in the function instead, which you can do by dragging from the my blueprint pane.

95
00:07:17,260 --> 00:07:22,750
‫And then finally we've got a couple of welcome messages, output of welcome message nodes.

96
00:07:22,750 --> 00:07:24,820
‫You can extract those into a print.

97
00:07:24,820 --> 00:07:27,070
‫Welcome messages function as well.

98
00:07:27,100 --> 00:07:29,170
‫Pause the video and have a go.

99
00:07:32,230 --> 00:07:32,740
‫Okay.

100
00:07:32,740 --> 00:07:33,570
‫Welcome back.

101
00:07:33,580 --> 00:07:37,450
‫So we're going to extract our decrease ammo first.

102
00:07:37,450 --> 00:07:39,640
‫So there's three nodes involved in that.

103
00:07:39,640 --> 00:07:47,260
‫We'll right click collapse the function, then we're going to call it Decrease Ammo and that's fairly

104
00:07:47,260 --> 00:07:47,740
‫straightforward.

105
00:07:47,740 --> 00:07:51,940
‫So spawn projectile, decrease ammo and then we are printing the ammo.

106
00:07:51,940 --> 00:07:58,210
‫So let's take the three nodes, the get convert and print string and we're going to right click collapse

107
00:07:58,210 --> 00:08:02,020
‫to function and call this one print ammo.

108
00:08:02,470 --> 00:08:08,590
‫Now as I said, we've got this functionality repeated in two places, so we'll delete where it does

109
00:08:08,590 --> 00:08:16,180
‫this in begin play and just drag in print ammo from our functions list and connect up the execution

110
00:08:16,180 --> 00:08:19,840
‫pins so that it runs after our print string.

111
00:08:20,200 --> 00:08:25,630
‫Which brings me on to the last function I want to create, which we said we'd call print welcome messages.

112
00:08:25,630 --> 00:08:32,050
‫So I'm going to select the to print strings after being in play collapse to function and call it print

113
00:08:32,050 --> 00:08:34,390
‫welcome messages.

114
00:08:34,390 --> 00:08:42,040
‫And this node, we can just now rearrange all of our nodes to make it far easier to read all of this

115
00:08:42,040 --> 00:08:43,200
‫in one screen full.

116
00:08:43,210 --> 00:08:49,960
‫You can see it's much easier to understand the functionality of our game now because it's all fits on

117
00:08:49,960 --> 00:08:56,380
‫one screen and is nicely brought to a similar level of abstraction and understanding.

118
00:08:56,380 --> 00:09:02,830
‫And this is what we would call self documenting code, because it is code that doesn't need comments

119
00:09:02,830 --> 00:09:04,900
‫or documentation to explain what it does.

120
00:09:04,900 --> 00:09:07,150
‫It just reads like English.

121
00:09:07,270 --> 00:09:08,110
‫Great stuff.

122
00:09:08,110 --> 00:09:10,120
‫I'll see you in the next lecture.

