‫Hello and welcome.
‫In this lecture, we're going to be refactoring our code to make our grab function smaller and more
‫readable.
‫And we're going to be using our parameters again.
‫This time, we're actually going to create a function that takes it as a parameter.
‫Let's dive in and see how to do this.
‫So it's generally best practice for functions to try to fit on a single screen.
‫Now, in my case, my screen is very big, but we are still learning, so it actually makes sense to
‫have even smaller functions to make things understandable.
‫And I think this grab function has got too long, so I think we can split it out in two fairly functional
‫units.
‫The first one is going to be the bit that does the sweep and does all the calculation of the start and
‫end vectors and outputs the hit result and whether we have hit or not, the boolean that says whether
‫we've hit.
‫And then the second bit is inside the if statement which we can probably leave as the job of grabbing
‫because that's actually to do with grabbing, right?
‫We have to wake the rigid body.
‫We have to grab a component that makes sense to be in a method called in a function called grab.
‫But this other stuff is a little bit more tangential.
‫It's more of a utility that we need to pull in.
‫So I'd like to challenge you to create that utility, get grab able in reach.
‫That's what I think it should be called.
‫I think that describes what that lump of code does.
‫So extract all of that sweep code into a new private function.
‫Have a think about what its parameters and its return values should be.
‫Remember, you've got two outputs.
‫So we've got has hit and hit result.
‫So you need to think about trying to use an out parameter to make this work.
‫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
‫file in just a second.
‫If you don't, you can go and pause a video right now.
‫Otherwise, I'm going to give you the hint.
‫So we're going to go into grab H and underneath where we've got get physics handle, we are going to
‫have a Boolean return type to tell us whether something has been we can get something, grab a ball,
‫let's get grab a ball in reach.
‫Then we need to give it a parameter which is going to be an out parameter.
‫So it's going to be a non constant reference to the F hit result.
‫Put a reference.
‫Ampersand and then we'll call that the alt hit result like so.
‫And then I think this isn't going to change anything in our function so we can make it cost as well.
‫It's a little bonus points if you had already thought of that.
‫So go ahead, pause the video and try and fill out that function in the C++ file.
‫Okay.
‫Welcome back.
‫So I'm going to copy it.
‫Go over to the C++.
‫I'm going to put it underneath our Get Physics Handle component to match the order we've got over in
‫the header file.
‫I'm going to add the U grabber in front.
‫I'm going to indent it correctly.
‫I'm going to add instead of semicolon the braces and then I'm going to move the code that we've got
‫over in grab.
‫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
‫something in just a second.
‫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.
‫What we can do is we can set that out hit result equal to our hit result that we've got here.
‫We might only want to do that if we've hit.
‫So we can put that if statement has hit like so and wrap this assignment, the out hit to the hit result
‫inside the if statement and then we can go ahead and at the end of the function just return has hit
‫like so now there is an optimization or an improvement that can be made here because we are passing
‫by reference into the sweep channel and into grab ball in reach.
‫We don't actually need to create a new hit result in here and then copy it into the hit result.
‫That's just wasting memory.
‫What we can do is we can remove the hit result that we're creating inside, get grab a ball in reach
‫and instead we can just use the reference that we've had passed in to pass directly in to the sweep
‫single by channel.
‫So it's actually passing this reference to functions down into the sweep single by channel where it
‫will get populated and that removes the need for the if statement making this function a little bit
‫quicker and easier to read.
‫And in fact, I don't think we even need a variable to store has hit.
‫We can just go ahead and return.
‫It's going to be much cleaner to return the get world sweep channel results.
‫So we're just kind of passing the results from that through back up into get grab able in reach or whoever
‫is calling it.
‫So talking of whoever is calling it, we now need to go up into our grab.
‫We need to create our hit results somewhere.
‫We have to create it here.
‫So hit result, this is the one we're going to populate.
‫We're going to then create a boolean variable which is going to be has hit kind of set that equal to
‫get grab able in reach.
‫And we're going to pass in the hit result by reference that we want populated.
‫And then if we have hit, we're going to go ahead and execute the grabbing code.
‫Fantastic stuff.
‫We have moved something in to a function to make this grab function fit on a single screen.
‫And we've again made use of out parameters, this time creating a function for ourselves that use them
‫in earnest.
‫I'll see you in the next video.