﻿1
00:00:04,510 --> 00:00:05,320
‫Hello and welcome.

2
00:00:05,320 --> 00:00:10,870
‫In this lecture, we're going to be refactoring our pull trigger so that it's ready for us to add more

3
00:00:10,870 --> 00:00:11,410
‫code into.

4
00:00:11,410 --> 00:00:15,220
‫At the moment, it was getting really out of hand, so we're going to refactor it to a small function

5
00:00:15,220 --> 00:00:18,760
‫like this that's really much more straightforward to read.

6
00:00:18,760 --> 00:00:21,280
‫Let's dive in and see how to do this refactor.

7
00:00:22,220 --> 00:00:22,460
‫Okay.

8
00:00:22,460 --> 00:00:27,860
‫So we want to put some sound into our game and the place we're going to be doing this is in the gun

9
00:00:27,860 --> 00:00:28,640
‫when we're shooting.

10
00:00:28,640 --> 00:00:30,110
‫That's the first port of call.

11
00:00:30,110 --> 00:00:33,530
‫I think a nice kind of shot sound and impact sound would be great.

12
00:00:33,890 --> 00:00:39,290
‫The only problem is when I came back to our pull trigger function, I noticed that it has got outlandishly

13
00:00:39,290 --> 00:00:39,620
‫large.

14
00:00:39,620 --> 00:00:41,180
‫It doesn't fit on one of my screens.

15
00:00:41,180 --> 00:00:45,550
‫It's also a little bit hard to read and understand where we'd be putting this functionality.

16
00:00:45,560 --> 00:00:50,930
‫So while we take the effort to actually understand what the functionality is here, maybe it's worth

17
00:00:51,020 --> 00:00:56,840
‫doing a quick refactor or pulling out some of this stuff into other methods so that it's not so hard

18
00:00:56,840 --> 00:00:57,500
‫to read.

19
00:00:58,040 --> 00:01:03,200
‫Now, if you go and have a look at this code and try and figure out what the basic things that it's

20
00:01:03,200 --> 00:01:08,570
‫doing, then you'll see that a large chunk of this is actually just set up for our line.

21
00:01:08,570 --> 00:01:12,230
‫Trace this whole chunk from line 28 down to 43.

22
00:01:12,500 --> 00:01:13,910
‫And what are we doing?

23
00:01:13,910 --> 00:01:18,980
‫Well, we're getting the owner pawn, then we're checking the getting the owner's controller, then

24
00:01:18,980 --> 00:01:23,780
‫we're getting the viewport from that, then we're getting the end location based on the max range.

25
00:01:23,780 --> 00:01:26,810
‫Then we're passing in some hit parameters to ignore stuff.

26
00:01:26,810 --> 00:01:33,320
‫All of this is just set up for our line trace, so why don't we extract that out into its own function

27
00:01:33,320 --> 00:01:33,730
‫here?

28
00:01:34,030 --> 00:01:37,610
‫I'm going to create a new private function down right at the bottom.

29
00:01:37,610 --> 00:01:46,370
‫After our variables it is going to be bool return because we want to essentially replace this call here

30
00:01:46,370 --> 00:01:52,430
‫that returns success with one that just gives us the hit result out as well.

31
00:01:52,670 --> 00:02:00,020
‫So we're going to do this as something I'm going to call a gun trace function and it's going to have

32
00:02:00,020 --> 00:02:01,550
‫some out parameters here.

33
00:02:01,550 --> 00:02:03,920
‫Namely, one of them is going to be the hit result.

34
00:02:03,920 --> 00:02:07,400
‫So hit result, ampersand to make it a reference.

35
00:02:07,400 --> 00:02:10,160
‫And because it's not a constant reference, we know it's an out parameter.

36
00:02:10,160 --> 00:02:11,150
‫We call this the hit.

37
00:02:11,150 --> 00:02:14,540
‫And actually there may be another one that we need here.

38
00:02:14,570 --> 00:02:21,980
‫Namely, I can see that we've got the rotation is coming out of this chunk of code and in actual fact

39
00:02:21,980 --> 00:02:23,300
‫we're negating that rotation.

40
00:02:23,300 --> 00:02:26,630
‫So what we're really interested in is the shot direction.

41
00:02:26,630 --> 00:02:28,550
‫So maybe that's what we need to go for.

42
00:02:28,550 --> 00:02:36,680
‫So I'm going to create an F vector out parameter, so F vector ampersand and it's going to be the shot

43
00:02:36,680 --> 00:02:37,700
‫direction.

44
00:02:38,330 --> 00:02:41,990
‫These are the two outputs that are going to come from our gun trace.

45
00:02:42,200 --> 00:02:45,620
‫Now let's go ahead and create the implementation here.

46
00:02:46,280 --> 00:02:53,510
‫And what we want to do is essentially just copy all of this code into there.

47
00:02:54,590 --> 00:02:55,760
‫See how it goes.

48
00:02:56,240 --> 00:03:00,120
‫And we also want to be checking this shot.

49
00:03:00,140 --> 00:03:00,980
‫Direction.

50
00:03:01,520 --> 00:03:05,870
‫Take that out there and stick it in just before the end.

51
00:03:05,870 --> 00:03:11,360
‫Or maybe just here after we've got our player viewport, we can set that the shot direction needs to

52
00:03:11,360 --> 00:03:13,310
‫be equal to the negative, the rotation.

53
00:03:13,520 --> 00:03:19,190
‫But it's important to note here that actually we want to be setting not a local variable.

54
00:03:19,190 --> 00:03:22,520
‫So if you put the type here, it's going to be setting a local shot direction.

55
00:03:22,520 --> 00:03:27,980
‫We want to delete that so that it is setting the shot direction in the parameters, which actually means

56
00:03:27,980 --> 00:03:32,600
‫that it's setting the shot direction that we're going to be passing in in the function that calls it.

57
00:03:33,050 --> 00:03:34,730
‫Same goes for the hit.

58
00:03:34,730 --> 00:03:42,260
‫We need to remove this local hit so that the hit being passed into line trace single by channel is the

59
00:03:42,260 --> 00:03:44,300
‫hit that was passed into gun trace.

60
00:03:45,140 --> 00:03:50,810
‫And then finally, is there anything else is this to do line which is now not needed and also this be

61
00:03:50,810 --> 00:03:51,290
‫success?

62
00:03:51,290 --> 00:03:56,240
‫Well, we just need to be returning the result of the line trace signal by channel.

63
00:03:56,240 --> 00:03:59,780
‫That is going to be whether or not our gun trace has succeeded.

64
00:03:59,780 --> 00:04:02,450
‫So we've succeeded in making this indentation a little bit less.

65
00:04:02,450 --> 00:04:06,650
‫We can fit it all on one screen now, and it's a little bit easier to understand that, yes, all of

66
00:04:06,650 --> 00:04:09,440
‫this is related to getting a gun trace.

67
00:04:09,710 --> 00:04:14,990
‫We can go up to our gun CP And we can now set our B success up here.

68
00:04:14,990 --> 00:04:23,780
‫So bool B success, we're going to need to set as the gun trace and we need to pass in some out parameters

69
00:04:23,780 --> 00:04:23,930
‫here.

70
00:04:23,930 --> 00:04:27,020
‫So we still need to declare those somewhere if hit result.

71
00:04:28,220 --> 00:04:29,420
‫It's going to be one of them.

72
00:04:29,660 --> 00:04:30,690
‫I'm just going to call hit.

73
00:04:30,740 --> 00:04:36,380
‫And the other one was going to be an F vector called shot direction.

74
00:04:37,430 --> 00:04:39,250
‫And those are going to be passed in.

75
00:04:39,260 --> 00:04:42,410
‫So we're going to have the hit first, then the shot direction.

76
00:04:44,150 --> 00:04:46,710
‫And then semi-colon at the end there.

77
00:04:46,730 --> 00:04:49,580
‫So that's going to make that just a little bit easier.

78
00:04:49,610 --> 00:04:56,000
‫However, I'm noticing we also need the owner controller and I don't want the gun trace to be responsible

79
00:04:56,000 --> 00:04:59,720
‫for the owner controller or forgetting the owner controller.

80
00:04:59,720 --> 00:05:03,450
‫That doesn't sound like it's the responsibility of a gun trace to do that.

81
00:05:03,470 --> 00:05:05,560
‫It sounds okay for it to get a hit.

82
00:05:05,570 --> 00:05:07,580
‫It sounds okay for it to get a shot direction.

83
00:05:07,580 --> 00:05:10,550
‫But I'm going to draw the line at getting the owner controller.

84
00:05:10,550 --> 00:05:17,660
‫So for that, I'm going to put it in, put all of this code into another function which both of them

85
00:05:17,660 --> 00:05:18,770
‫can call.

86
00:05:19,010 --> 00:05:23,960
‫So that's going to be a function that returns a controller.

87
00:05:24,880 --> 00:05:25,810
‫Star.

88
00:05:26,560 --> 00:05:32,830
‫And we're going to call it the get over controller and we can make that a concert.

89
00:05:32,830 --> 00:05:39,030
‫As we know, it's not meant to change any of the variables on our gun class.

90
00:05:39,040 --> 00:05:40,780
‫Let's create that implementation.

91
00:05:40,780 --> 00:05:48,430
‫Just bring in the implementation from gun trace, fix up any indentation if necessary, and we're going

92
00:05:48,490 --> 00:05:52,090
‫to go ahead and simply return the owner control.

93
00:05:52,090 --> 00:05:54,100
‫So there's no need to do a null check here.

94
00:05:54,220 --> 00:05:58,000
‫That null check we do need to do in the function that is using it.

95
00:05:58,180 --> 00:06:00,130
‫So we'll bring that up there.

96
00:06:00,130 --> 00:06:02,950
‫We also don't need to set a variable in here.

97
00:06:02,950 --> 00:06:04,870
‫We can just go ahead and return.

98
00:06:06,560 --> 00:06:08,660
‫So it's going to be fairly straightforward.

99
00:06:08,660 --> 00:06:11,030
‫I get on a controller, but not completely.

100
00:06:11,210 --> 00:06:14,840
‫And then we're going to set the variable in gun trace.

101
00:06:14,840 --> 00:06:19,550
‫So owner controller is going to be get owner controller like so.

102
00:06:20,380 --> 00:06:26,950
‫Which we then do a null return to null early return in gun trace, which by the way, it needs to return

103
00:06:26,950 --> 00:06:30,430
‫a boolean because gun trace is returning a boolean.

104
00:06:30,700 --> 00:06:36,160
‫So we can go ahead and return false here because if we can't get an owner controller, we can't do a

105
00:06:36,160 --> 00:06:36,610
‫gun trace.

106
00:06:36,610 --> 00:06:38,530
‫Therefore we can't possibly hit anything.

107
00:06:39,250 --> 00:06:42,880
‫And then we also need that owner controller further up.

108
00:06:43,780 --> 00:06:47,860
‫Where we have our hit actor, we're doing this take damage.

109
00:06:48,220 --> 00:06:54,310
‫And basically what we want to do here is get the owner controller right in here inside our hit actor.

110
00:06:54,310 --> 00:06:56,760
‫So if we've hit an actor, we get the owner controller again.

111
00:06:56,770 --> 00:07:00,250
‫Now, I know there's a little bit of gun tracers calling.

112
00:07:00,250 --> 00:07:03,400
‫Get a controller and then we're doing it again if we hit an actor.

113
00:07:03,520 --> 00:07:06,280
‫But that's only going to happen every time we hit an actor.

114
00:07:06,280 --> 00:07:12,670
‫So I don't mind having that recalculated twice because it's just going to make the code that little

115
00:07:12,670 --> 00:07:16,990
‫bit more readable without having to store variables and state that could get out of sync.

116
00:07:16,990 --> 00:07:17,920
‫So there you go.

117
00:07:17,920 --> 00:07:23,830
‫We're now got the owner controller that we need for take damage and our pull trigger is looking much

118
00:07:23,830 --> 00:07:24,280
‫better.

119
00:07:24,280 --> 00:07:25,240
‫So that's it for this lecture.

120
00:07:25,240 --> 00:07:25,780
‫A quick one.

121
00:07:25,780 --> 00:07:31,180
‫Just refactoring our pull trigger so that we've got a good base to work from in the next lecture, and

122
00:07:31,180 --> 00:07:31,870
‫I'll see you there.

123
00:07:32,800 --> 00:07:37,300
‫Oh, and it looks like I made a little bit of a booboo here in the get owner controller.

124
00:07:37,330 --> 00:07:42,430
‫If you try and compile it, it won't do anything because this return value is not actually returning

125
00:07:42,460 --> 00:07:43,210
‫a pointer.

126
00:07:43,210 --> 00:07:47,620
‫So we want to return null pointer in this case straight away.

127
00:07:47,620 --> 00:07:50,380
‫Basically we can't get a pawn, then we can't get a controller.

128
00:07:50,380 --> 00:07:52,030
‫So we return null pointer.

