﻿1
00:00:04,340 --> 00:00:05,240
‫Hello and welcome.

2
00:00:05,240 --> 00:00:12,140
‫In this lecture, we're going to be refactoring our code to make our grab function smaller and more

3
00:00:12,140 --> 00:00:12,830
‫readable.

4
00:00:12,830 --> 00:00:15,290
‫And we're going to be using our parameters again.

5
00:00:15,290 --> 00:00:18,860
‫This time, we're actually going to create a function that takes it as a parameter.

6
00:00:18,860 --> 00:00:21,380
‫Let's dive in and see how to do this.

7
00:00:22,130 --> 00:00:29,090
‫So it's generally best practice for functions to try to fit on a single screen.

8
00:00:29,090 --> 00:00:34,070
‫Now, in my case, my screen is very big, but we are still learning, so it actually makes sense to

9
00:00:34,070 --> 00:00:37,490
‫have even smaller functions to make things understandable.

10
00:00:38,030 --> 00:00:45,740
‫And I think this grab function has got too long, so I think we can split it out in two fairly functional

11
00:00:45,740 --> 00:00:46,070
‫units.

12
00:00:46,070 --> 00:00:52,250
‫The first one is going to be the bit that does the sweep and does all the calculation of the start and

13
00:00:52,250 --> 00:00:58,880
‫end vectors and outputs the hit result and whether we have hit or not, the boolean that says whether

14
00:00:58,880 --> 00:00:59,480
‫we've hit.

15
00:00:59,480 --> 00:01:05,900
‫And then the second bit is inside the if statement which we can probably leave as the job of grabbing

16
00:01:05,900 --> 00:01:08,930
‫because that's actually to do with grabbing, right?

17
00:01:08,930 --> 00:01:10,790
‫We have to wake the rigid body.

18
00:01:10,790 --> 00:01:16,430
‫We have to grab a component that makes sense to be in a method called in a function called grab.

19
00:01:16,430 --> 00:01:20,210
‫But this other stuff is a little bit more tangential.

20
00:01:20,210 --> 00:01:23,720
‫It's more of a utility that we need to pull in.

21
00:01:23,720 --> 00:01:28,580
‫So I'd like to challenge you to create that utility, get grab able in reach.

22
00:01:28,580 --> 00:01:29,870
‫That's what I think it should be called.

23
00:01:29,870 --> 00:01:33,290
‫I think that describes what that lump of code does.

24
00:01:33,290 --> 00:01:37,220
‫So extract all of that sweep code into a new private function.

25
00:01:37,220 --> 00:01:41,600
‫Have a think about what its parameters and its return values should be.

26
00:01:41,600 --> 00:01:43,670
‫Remember, you've got two outputs.

27
00:01:43,670 --> 00:01:46,760
‫So we've got has hit and hit result.

28
00:01:46,760 --> 00:01:51,890
‫So you need to think about trying to use an out parameter to make this work.

29
00:01:52,040 --> 00:01:57,710
‫Now, if you want a little bit of a hint, I'm going to show you what I'm going to put in the header

30
00:01:57,710 --> 00:01:58,970
‫file in just a second.

31
00:01:58,970 --> 00:02:01,520
‫If you don't, you can go and pause a video right now.

32
00:02:01,820 --> 00:02:04,340
‫Otherwise, I'm going to give you the hint.

33
00:02:04,730 --> 00:02:12,740
‫So we're going to go into grab H and underneath where we've got get physics handle, we are going to

34
00:02:12,740 --> 00:02:18,860
‫have a Boolean return type to tell us whether something has been we can get something, grab a ball,

35
00:02:18,910 --> 00:02:23,180
‫let's get grab a ball in reach.

36
00:02:23,180 --> 00:02:27,650
‫Then we need to give it a parameter which is going to be an out parameter.

37
00:02:27,650 --> 00:02:34,040
‫So it's going to be a non constant reference to the F hit result.

38
00:02:34,040 --> 00:02:35,630
‫Put a reference.

39
00:02:36,590 --> 00:02:41,450
‫Ampersand and then we'll call that the alt hit result like so.

40
00:02:41,450 --> 00:02:49,340
‫And then I think this isn't going to change anything in our function so we can make it cost as well.

41
00:02:49,340 --> 00:02:52,280
‫It's a little bonus points if you had already thought of that.

42
00:02:52,490 --> 00:02:57,860
‫So go ahead, pause the video and try and fill out that function in the C++ file.

43
00:03:01,090 --> 00:03:01,420
‫Okay.

44
00:03:01,420 --> 00:03:02,190
‫Welcome back.

45
00:03:02,200 --> 00:03:03,010
‫So I'm going to copy it.

46
00:03:03,010 --> 00:03:04,960
‫Go over to the C++.

47
00:03:04,960 --> 00:03:10,840
‫I'm going to put it underneath our Get Physics Handle component to match the order we've got over in

48
00:03:10,840 --> 00:03:11,800
‫the header file.

49
00:03:11,980 --> 00:03:16,930
‫I'm going to add the U grabber in front.

50
00:03:17,110 --> 00:03:18,520
‫I'm going to indent it correctly.

51
00:03:18,520 --> 00:03:25,330
‫I'm going to add instead of semicolon the braces and then I'm going to move the code that we've got

52
00:03:25,330 --> 00:03:26,890
‫over in grab.

53
00:03:26,890 --> 00:03:34,930
‫So starting at line 53 for me down to 65, I'm going to cut all of that out and we'll replace it with

54
00:03:34,930 --> 00:03:36,220
‫something in just a second.

55
00:03:36,370 --> 00:03:44,710
‫I'm going to paste it in to get grab a ball in reach and we've got this hit result that we've created.

56
00:03:44,710 --> 00:03:53,530
‫What we can do is we can set that out hit result equal to our hit result that we've got here.

57
00:03:53,620 --> 00:03:56,740
‫We might only want to do that if we've hit.

58
00:03:56,740 --> 00:04:05,440
‫So we can put that if statement has hit like so and wrap this assignment, the out hit to the hit result

59
00:04:05,530 --> 00:04:11,500
‫inside the if statement and then we can go ahead and at the end of the function just return has hit

60
00:04:11,500 --> 00:04:18,280
‫like so now there is an optimization or an improvement that can be made here because we are passing

61
00:04:18,280 --> 00:04:23,260
‫by reference into the sweep channel and into grab ball in reach.

62
00:04:23,260 --> 00:04:28,960
‫We don't actually need to create a new hit result in here and then copy it into the hit result.

63
00:04:28,960 --> 00:04:30,250
‫That's just wasting memory.

64
00:04:30,820 --> 00:04:36,490
‫What we can do is we can remove the hit result that we're creating inside, get grab a ball in reach

65
00:04:36,490 --> 00:04:44,110
‫and instead we can just use the reference that we've had passed in to pass directly in to the sweep

66
00:04:44,110 --> 00:04:44,980
‫single by channel.

67
00:04:44,980 --> 00:04:50,440
‫So it's actually passing this reference to functions down into the sweep single by channel where it

68
00:04:50,440 --> 00:04:55,840
‫will get populated and that removes the need for the if statement making this function a little bit

69
00:04:55,840 --> 00:04:57,880
‫quicker and easier to read.

70
00:04:57,880 --> 00:05:01,510
‫And in fact, I don't think we even need a variable to store has hit.

71
00:05:01,510 --> 00:05:03,790
‫We can just go ahead and return.

72
00:05:03,790 --> 00:05:09,730
‫It's going to be much cleaner to return the get world sweep channel results.

73
00:05:09,730 --> 00:05:16,090
‫So we're just kind of passing the results from that through back up into get grab able in reach or whoever

74
00:05:16,090 --> 00:05:17,050
‫is calling it.

75
00:05:17,050 --> 00:05:21,970
‫So talking of whoever is calling it, we now need to go up into our grab.

76
00:05:21,970 --> 00:05:24,670
‫We need to create our hit results somewhere.

77
00:05:24,670 --> 00:05:25,690
‫We have to create it here.

78
00:05:25,690 --> 00:05:28,150
‫So hit result, this is the one we're going to populate.

79
00:05:28,330 --> 00:05:35,320
‫We're going to then create a boolean variable which is going to be has hit kind of set that equal to

80
00:05:35,350 --> 00:05:38,320
‫get grab able in reach.

81
00:05:38,320 --> 00:05:43,240
‫And we're going to pass in the hit result by reference that we want populated.

82
00:05:43,240 --> 00:05:49,270
‫And then if we have hit, we're going to go ahead and execute the grabbing code.

83
00:05:49,270 --> 00:05:50,320
‫Fantastic stuff.

84
00:05:50,320 --> 00:05:56,590
‫We have moved something in to a function to make this grab function fit on a single screen.

85
00:05:56,590 --> 00:06:04,120
‫And we've again made use of out parameters, this time creating a function for ourselves that use them

86
00:06:04,120 --> 00:06:05,200
‫in earnest.

87
00:06:05,200 --> 00:06:07,270
‫I'll see you in the next video.

