‫Hello and welcome.
‫In this lecture, we're going to be refactoring our pull trigger so that it's ready for us to add more
‫code into.
‫At the moment, it was getting really out of hand, so we're going to refactor it to a small function
‫like this that's really much more straightforward to read.
‫Let's dive in and see how to do this refactor.
‫Okay.
‫So we want to put some sound into our game and the place we're going to be doing this is in the gun
‫when we're shooting.
‫That's the first port of call.
‫I think a nice kind of shot sound and impact sound would be great.
‫The only problem is when I came back to our pull trigger function, I noticed that it has got outlandishly
‫large.
‫It doesn't fit on one of my screens.
‫It's also a little bit hard to read and understand where we'd be putting this functionality.
‫So while we take the effort to actually understand what the functionality is here, maybe it's worth
‫doing a quick refactor or pulling out some of this stuff into other methods so that it's not so hard
‫to read.
‫Now, if you go and have a look at this code and try and figure out what the basic things that it's
‫doing, then you'll see that a large chunk of this is actually just set up for our line.
‫Trace this whole chunk from line 28 down to 43.
‫And what are we doing?
‫Well, we're getting the owner pawn, then we're checking the getting the owner's controller, then
‫we're getting the viewport from that, then we're getting the end location based on the max range.
‫Then we're passing in some hit parameters to ignore stuff.
‫All of this is just set up for our line trace, so why don't we extract that out into its own function
‫here?
‫I'm going to create a new private function down right at the bottom.
‫After our variables it is going to be bool return because we want to essentially replace this call here
‫that returns success with one that just gives us the hit result out as well.
‫So we're going to do this as something I'm going to call a gun trace function and it's going to have
‫some out parameters here.
‫Namely, one of them is going to be the hit result.
‫So hit result, ampersand to make it a reference.
‫And because it's not a constant reference, we know it's an out parameter.
‫We call this the hit.
‫And actually there may be another one that we need here.
‫Namely, I can see that we've got the rotation is coming out of this chunk of code and in actual fact
‫we're negating that rotation.
‫So what we're really interested in is the shot direction.
‫So maybe that's what we need to go for.
‫So I'm going to create an F vector out parameter, so F vector ampersand and it's going to be the shot
‫direction.
‫These are the two outputs that are going to come from our gun trace.
‫Now let's go ahead and create the implementation here.
‫And what we want to do is essentially just copy all of this code into there.
‫See how it goes.
‫And we also want to be checking this shot.
‫Direction.
‫Take that out there and stick it in just before the end.
‫Or maybe just here after we've got our player viewport, we can set that the shot direction needs to
‫be equal to the negative, the rotation.
‫But it's important to note here that actually we want to be setting not a local variable.
‫So if you put the type here, it's going to be setting a local shot direction.
‫We want to delete that so that it is setting the shot direction in the parameters, which actually means
‫that it's setting the shot direction that we're going to be passing in in the function that calls it.
‫Same goes for the hit.
‫We need to remove this local hit so that the hit being passed into line trace single by channel is the
‫hit that was passed into gun trace.
‫And then finally, is there anything else is this to do line which is now not needed and also this be
‫success?
‫Well, we just need to be returning the result of the line trace signal by channel.
‫That is going to be whether or not our gun trace has succeeded.
‫So we've succeeded in making this indentation a little bit less.
‫We can fit it all on one screen now, and it's a little bit easier to understand that, yes, all of
‫this is related to getting a gun trace.
‫We can go up to our gun CP And we can now set our B success up here.
‫So bool B success, we're going to need to set as the gun trace and we need to pass in some out parameters
‫here.
‫So we still need to declare those somewhere if hit result.
‫It's going to be one of them.
‫I'm just going to call hit.
‫And the other one was going to be an F vector called shot direction.
‫And those are going to be passed in.
‫So we're going to have the hit first, then the shot direction.
‫And then semi-colon at the end there.
‫So that's going to make that just a little bit easier.
‫However, I'm noticing we also need the owner controller and I don't want the gun trace to be responsible
‫for the owner controller or forgetting the owner controller.
‫That doesn't sound like it's the responsibility of a gun trace to do that.
‫It sounds okay for it to get a hit.
‫It sounds okay for it to get a shot direction.
‫But I'm going to draw the line at getting the owner controller.
‫So for that, I'm going to put it in, put all of this code into another function which both of them
‫can call.
‫So that's going to be a function that returns a controller.
‫Star.
‫And we're going to call it the get over controller and we can make that a concert.
‫As we know, it's not meant to change any of the variables on our gun class.
‫Let's create that implementation.
‫Just bring in the implementation from gun trace, fix up any indentation if necessary, and we're going
‫to go ahead and simply return the owner control.
‫So there's no need to do a null check here.
‫That null check we do need to do in the function that is using it.
‫So we'll bring that up there.
‫We also don't need to set a variable in here.
‫We can just go ahead and return.
‫So it's going to be fairly straightforward.
‫I get on a controller, but not completely.
‫And then we're going to set the variable in gun trace.
‫So owner controller is going to be get owner controller like so.
‫Which we then do a null return to null early return in gun trace, which by the way, it needs to return
‫a boolean because gun trace is returning a boolean.
‫So we can go ahead and return false here because if we can't get an owner controller, we can't do a
‫gun trace.
‫Therefore we can't possibly hit anything.
‫And then we also need that owner controller further up.
‫Where we have our hit actor, we're doing this take damage.
‫And basically what we want to do here is get the owner controller right in here inside our hit actor.
‫So if we've hit an actor, we get the owner controller again.
‫Now, I know there's a little bit of gun tracers calling.
‫Get a controller and then we're doing it again if we hit an actor.
‫But that's only going to happen every time we hit an actor.
‫So I don't mind having that recalculated twice because it's just going to make the code that little
‫bit more readable without having to store variables and state that could get out of sync.
‫So there you go.
‫We're now got the owner controller that we need for take damage and our pull trigger is looking much
‫better.
‫So that's it for this lecture.
‫A quick one.
‫Just refactoring our pull trigger so that we've got a good base to work from in the next lecture, and
‫I'll see you there.
‫Oh, and it looks like I made a little bit of a booboo here in the get owner controller.
‫If you try and compile it, it won't do anything because this return value is not actually returning
‫a pointer.
‫So we want to return null pointer in this case straight away.
‫Basically we can't get a pawn, then we can't get a controller.
‫So we return null pointer.