﻿1
00:00:04,490 --> 00:00:05,690
‫Hello and welcome.

2
00:00:05,690 --> 00:00:11,630
‫In this lecture, we're going to be learning about functions that live not in the level blueprint,

3
00:00:11,630 --> 00:00:14,300
‫but on a particular blueprint class.

4
00:00:14,300 --> 00:00:20,120
‫These are called member functions and are absolutely crucial to object oriented programming.

5
00:00:20,120 --> 00:00:22,700
‫Let's dive in and see how they work.

6
00:00:23,150 --> 00:00:31,430
‫So in our spawn projectile function, in the level blueprint, we have got a load of nodes here relating

7
00:00:31,430 --> 00:00:36,830
‫to adding the impulse, getting the direction of the impulse, and getting the static mesh component.

8
00:00:36,920 --> 00:00:43,610
‫All of these nodes use the BP projectile instance in depth.

9
00:00:43,610 --> 00:00:50,690
‫Wouldn't it be great if we could take all of this code and move it in to the BP projectile so that the

10
00:00:50,690 --> 00:00:55,130
‫code that deals with that data lives with the data itself?

11
00:00:55,280 --> 00:00:59,300
‫Well, that's the entire point of object oriented programming.

12
00:00:59,300 --> 00:01:07,040
‫So we can define object oriented programming as functions living with the data that they manipulate.

13
00:01:07,400 --> 00:01:09,080
‫So how does this work?

14
00:01:09,080 --> 00:01:13,520
‫How can we make functions on particular instances of things?

15
00:01:13,940 --> 00:01:20,420
‫Well, one way we can do this will go to our be open up our BP projectile class and make sure we click

16
00:01:20,420 --> 00:01:27,020
‫open full blueprint editor is you can see that in this full blueprint editor there is a my blueprint

17
00:01:27,020 --> 00:01:35,180
‫pane and a function section so we can hit the plus arrow here to create a new function on the BP project

18
00:01:35,330 --> 00:01:35,810
‫class.

19
00:01:35,810 --> 00:01:41,960
‫And I'm going to name this function, print your name and this print your name.

20
00:01:41,960 --> 00:01:44,150
‫Function should do just that.

21
00:01:44,570 --> 00:01:49,910
‫You can see we've got a node that starts the execution of this function so we can pull off its execution

22
00:01:49,910 --> 00:01:52,460
‫pin and type print string.

23
00:01:52,460 --> 00:01:53,810
‫So that's going to print out.

24
00:01:53,810 --> 00:01:54,380
‫Hello.

25
00:01:54,380 --> 00:01:57,200
‫Not exactly the name of this function, is it?

26
00:01:57,200 --> 00:01:59,390
‫But we'll sort that out in just a second.

27
00:01:59,690 --> 00:02:04,130
‫So what we've got here is known as a member function.

28
00:02:04,130 --> 00:02:12,020
‫A member function is a function on a class that always gets called on a particular instance of a class.

29
00:02:12,020 --> 00:02:18,320
‫So you don't say, I'm going to call print your name on all BP projectiles.

30
00:02:18,320 --> 00:02:25,010
‫You have to say, I want to call, print your name on BP projectile number one, or BP projectile that

31
00:02:25,010 --> 00:02:26,720
‫has just been spawned.

32
00:02:26,720 --> 00:02:36,650
‫So if we go to our level blueprint, what you would have to do is take the reference that came out of

33
00:02:36,650 --> 00:02:38,780
‫spawn BP projectile.

34
00:02:39,020 --> 00:02:48,050
‫And from that reference you could call print your name and you can see it automatically connects to

35
00:02:48,050 --> 00:02:51,920
‫a special input node called Target.

36
00:02:51,920 --> 00:02:54,410
‫And obviously you've got to hook up your execution pins as well.

37
00:02:54,410 --> 00:02:58,580
‫I'm going to hook it up to happen after ad impulse.

38
00:02:58,580 --> 00:03:07,370
‫But what's interesting about this special target input node is that if you go over to the BP projectile,

39
00:03:07,370 --> 00:03:08,750
‫you'll see that print.

40
00:03:08,750 --> 00:03:12,830
‫Your name has no such input node.

41
00:03:12,830 --> 00:03:14,750
‫So what's going on here?

42
00:03:14,990 --> 00:03:24,230
‫Well, implicitly, any member function has a parameter which is its current target or the current instance.

43
00:03:24,560 --> 00:03:32,030
‫And you can get hold of this by right clicking in the blueprint event graph and typing self.

44
00:03:32,390 --> 00:03:37,910
‫And you can see there is a node called get a reference to self and it just creates a self node like

45
00:03:37,910 --> 00:03:38,540
‫this.

46
00:03:38,540 --> 00:03:45,980
‫And then you can use this self object reference to get hold of any of the properties on this particular

47
00:03:45,980 --> 00:03:46,370
‫object.

48
00:03:46,370 --> 00:03:53,270
‫So if we wanted to get hold of the name of our BP projectile, you can pull off of the self reference

49
00:03:53,270 --> 00:03:57,110
‫and type in get display name, which we've done before.

50
00:03:57,110 --> 00:04:01,250
‫And then you can hook up the display name into our print string.

51
00:04:01,250 --> 00:04:09,890
‫So now when we play and we fire off a new projectile, you can see it's printing the name of the projectile

52
00:04:10,310 --> 00:04:17,210
‫and you can see for every new projectile I create, the name is a different one because the self that's

53
00:04:17,210 --> 00:04:23,750
‫being referred to by the member function is a different self is a different projectile.

54
00:04:23,750 --> 00:04:32,390
‫So we've learnt that self is a node available inside of member functions that always points to the specific

55
00:04:32,390 --> 00:04:35,270
‫current instance that we are using.

56
00:04:35,270 --> 00:04:40,190
‫So the whole point of looking at member functions in the first place, the reason we started was because

57
00:04:40,190 --> 00:04:45,680
‫of these nodes in the level blueprint, the where we get the static mesh, we get the forward vector

58
00:04:45,680 --> 00:04:48,260
‫and then we add an impulse to these.

59
00:04:48,560 --> 00:04:51,860
‫These feel like they should live on the BP projectile.

60
00:04:51,860 --> 00:04:55,310
‫And so that's what I would like to challenge you to do.

61
00:04:55,310 --> 00:04:58,730
‫So this is a bit of an evolved challenge, but we're getting towards the end of the section.

62
00:04:58,730 --> 00:05:01,730
‫So I will take you through step by step.

63
00:05:02,120 --> 00:05:03,950
‫So we're going to make a launch function.

64
00:05:04,060 --> 00:05:05,800
‫Shin on the BP projectile.

65
00:05:05,800 --> 00:05:07,810
‫So create it on the BP projectile.

66
00:05:07,810 --> 00:05:08,890
‫Go to that plus button.

67
00:05:08,890 --> 00:05:11,080
‫Add a new one, name it, launch.

68
00:05:11,440 --> 00:05:18,700
‫Then cut and paste the nodes over from the level blueprint into this newly created function of yours

69
00:05:18,700 --> 00:05:22,090
‫and make sure you hook up execution pins and etc..

70
00:05:22,510 --> 00:05:28,000
‫Now you're going to try and compile this that we haven't done this very often in blueprints, but compiling

71
00:05:28,000 --> 00:05:34,390
‫just allows you to essentially spell check your code and make sure that there aren't any errors that

72
00:05:34,390 --> 00:05:36,850
‫the Blueprint compiler can foresee.

73
00:05:36,850 --> 00:05:43,660
‫So the way you do this is you go to the button in the top left and you hit compile and if there are

74
00:05:43,660 --> 00:05:48,580
‫any errors, they will show up at the bottom in the little pane called compiler results.

75
00:05:48,580 --> 00:05:53,530
‫You can see I've got compile successful, but any errors will show up here and sometimes you'll get

76
00:05:53,530 --> 00:05:57,250
‫a little bit of red on a node to indicate where the error is.

77
00:05:57,790 --> 00:06:03,730
‫And I'd like to see if you can try and understand what that error is saying and try and fix the error

78
00:06:03,730 --> 00:06:07,480
‫using the new self node that we have learnt about.

79
00:06:07,930 --> 00:06:15,850
‫And then you need to go ahead and call that function from the level blueprint by dragging off of the

80
00:06:15,850 --> 00:06:18,130
‫instance like we did for our print.

81
00:06:18,130 --> 00:06:24,760
‫Your self node drag off of the instance of the new projectile created from the spawn node and call the

82
00:06:24,760 --> 00:06:25,960
‫launch function.

83
00:06:26,200 --> 00:06:28,360
‫Pause the video and have a go.

84
00:06:31,400 --> 00:06:32,000
‫Okay.

85
00:06:32,000 --> 00:06:32,890
‫Welcome back.

86
00:06:32,900 --> 00:06:39,320
‫So I'm going to go over to our BP projectile, hit the plus button next to functions in the my blueprint

87
00:06:39,320 --> 00:06:47,090
‫pane and type in launch to name the function that let's go back to our level blueprint, cut out the

88
00:06:47,090 --> 00:06:51,740
‫nodes and go into the BP projectile and paste them in.

89
00:06:52,010 --> 00:06:58,550
‫Now I've got to hook up the execution from launch to add impulse and now I'm going to hit, compile

90
00:06:58,550 --> 00:07:04,850
‫and you see I get an error on the static mesh or get static mesh node.

91
00:07:05,060 --> 00:07:10,550
‫And if you look in the compiler results, it says it uses an invalid target.

92
00:07:10,700 --> 00:07:15,290
‫So target this input here is not correct.

93
00:07:15,290 --> 00:07:25,040
‫Now as I recommended you do is you can right click and type in self to get a reference to self and then

94
00:07:25,520 --> 00:07:28,460
‫bring that self into the target instead.

95
00:07:28,730 --> 00:07:34,490
‫Now if you hit compile again you can see the error has gone away and it's compiled successfully because

96
00:07:34,490 --> 00:07:43,550
‫what's going on is that it's now able to get the static mesh component from the current projectile that

97
00:07:43,550 --> 00:07:46,010
‫this launch function is being called on.

98
00:07:46,490 --> 00:07:56,060
‫So if we go back to our level blueprint now, we can take the output pin from our spawn actor and we

99
00:07:56,060 --> 00:07:59,930
‫can type in launch to call our launch function.

100
00:07:59,930 --> 00:08:06,080
‫You can see the execution pin was automatically hooked up for me and then out after the launch function

101
00:08:06,080 --> 00:08:12,500
‫we can hook up our print your name function if we want to, although that was just for debugging and

102
00:08:12,500 --> 00:08:18,560
‫demonstration purposes and if you test it out, you can see that it's still launching successfully.

103
00:08:18,590 --> 00:08:24,620
‫Except now the code that deals with launching lives on the projectile itself.

104
00:08:24,620 --> 00:08:25,250
‫Great stuff.

105
00:08:25,250 --> 00:08:30,980
‫We've learnt about one of the core concepts of object oriented programming in this lecture member functions.

106
00:08:30,980 --> 00:08:32,780
‫I will see you in the next one.

