‫Hello and welcome.
‫In this lecture, we're going to be taking our mess of blueprints and organizing it neatly into functions
‫so that we can easily read and understand what's going on in our Blueprint Event graph.
‫Let's dive in and see how to do this.
‫Now, if we zoom out on our event graph, we can see that it's starting to get very noisy.
‫I can also see a couple of nodes that aren't connected up to anything.
‫There's a get node which I'm going to delete, an asset, M0 node, but you can see it's getting very
‫noisy.
‫It's getting hard to understand what's going on here.
‫Now, a typical solution that people do is to say, Well, this block of nodes here is all about spawning
‫the projectile.
‫So I can select these.
‫I can hit the C key and it is going to put a block around this called a comment block.
‫And then I could type in spawn projectiles and that lets me know what that block of code is doing.
‫That is certainly one way to organize your code.
‫However, in my opinion, it's not the best way and we have got better tools for doing this called functions.
‫So what are functions?
‫Functions execute blocks of blueprint and make our games do things.
‫Many nodes that are provided by Unreal Real are actually functions already.
‫So we've encountered these.
‫When you go and zoom in say on our spawn actor projectile node, that is a function.
‫When we look at add impulse, that's a function to in fact most every one of these nodes, even things
‫like get forward active vector that doesn't have an execution pin and isn't blue is also a function
‫you can tell because it has a little f symbol in the top left that stands for function, but we aren't
‫restricted to the functions Unreal provides.
‫We can create our own functions too, and it's really easy to do.
‫In Blueprint.
‫They allow us to stay organized and they allow us to reuse blocks of code.
‫So that's better than a comment because a comment you have to copy and paste.
‫Whereas a function allows us to share one block of code between multiple places of use.
‫So let's take a look at how we can turn something like this in to a block of code.
‫Well, we can delete the comment block, first of all, because I don't think that's necessary.
‫Instead, I'm going to select all of the nodes from get player -- all the way to add impulse, right
‫click on them and select collapse to function.
‫And that creates a new block and replaces all of those previous nodes that were there before.
‫Now I'm going to give this function a name you can see over in the left hand pane.
‫It's created a new function under the function section in the my blueprint pane and it's got it selected
‫for me to automatically rename because we should definitely, definitely rename these functions and
‫I'm going to call it spawn projectile.
‫And good naming is absolutely vital in program because code is essentially all about communication.
‫We're trying to communicate with ourselves so that we can understand what we've written.
‫We're trying to communicate with ourselves in the future.
‫When we come back to this project, have forgotten why we've written it a certain way.
‫And when you work in teams, you're trying to communicate with the rest of your team too.
‫Some say that a good metric for code quality is the what the heck's per minute.
‫So if you're reading a piece of code and you're going, What the heck, 50 times a minute, that's pretty
‫bad code if you're reading it and only going, What the heck, once or twice a minute, then that's
‫probably pretty good code and you're always going to get somewhat the hex.
‫So we're just trying to reduce that number their functions.
‫Crucially, we tend to name functions as verbs.
‫There are some exceptions to this rule that we'll talk about later on, but mostly functions are things
‫that are doing something.
‫So we use doing words and if you're unsure about how to name this function, then if you're working
‫in a team, you could just talk to somebody else and ask them what they think you should name it and
‫what kind of things make sense to them.
‫You often find that you're so ingrained into a problem that you can't think of how to talk about it.
‫So you may be asking yourself, Well, where did all my code go when I created this spawn projectile
‫function?
‫Well, you can get back to that detail of how this function is actually implemented by either double
‫clicking it in the event graph or clicking on it in the My Blueprints tab or double clicking it on the
‫My Blueprints tab.
‫And you can see that we've got all of our nodes still resident here just now they're in a separate tab
‫so you can go back to your event graph tab or you can dive in to the Spawn projectile tab which shows
‫just that one function.
‫And you can see there's a new node here, this little purple node, which is the entry point to our
‫function.
‫This says this is what we should do when the function starts.
‫It's just an entry point.
‫And whenever it's called, that node is going to trigger and trigger all the rest of the nodes inside
‫of our function.
‫So going back to the event graph and.
‫Zooming out, maybe bringing our nodes together a bit.
‫Now that we've made our node graph so much smaller and easier to read.
‫I think there are a couple more candidates here for us to pull out functions and replace things.
‫One of them would be where we are printing out ammo.
‫So in this case, printing ammo is something that we do in two places.
‫We do it in begin play and we do it down in spawn projectile.
‫And then the other place, the other thing, piece of functionality that we could extract into a function
‫would be where we are decreasing the ammo that could be put into a decrease ammo function instead.
‫So just before I set you the challenge, I would like to delete the conversion node and print string
‫node at the after the spawn projectile because this is going to complicate matters.
‫I would like to copy the functionality from up above so that they both look exactly the same.
‫So if we copy the three nodes from Begin Play and paste them down below and connect up the execution
‫pins, then you can see we've got exactly the same set of nodes here, the same functionality between
‫the two, and there's no confusion when we come to extract this into a function.
‫So I'd like to challenge you to make some more functions.
‫The first one I would recommend doing is the decrease ammo.
‫So taking the three nodes involved in decreasing ammo, extracting those into a function and giving
‫them a name.
‫And then the second one is print ammo.
‫So there you're going to be extracting out the functionality of getting, converting and printing the
‫ammo into a single function.
‫And because we've got two sites where we're printing ammo, you might want to delete one of them and
‫bring in the function instead, which you can do by dragging from the my blueprint pane.
‫And then finally we've got a couple of welcome messages, output of welcome message nodes.
‫You can extract those into a print.
‫Welcome messages function as well.
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So we're going to extract our decrease ammo first.
‫So there's three nodes involved in that.
‫We'll right click collapse the function, then we're going to call it Decrease Ammo and that's fairly
‫straightforward.
‫So spawn projectile, decrease ammo and then we are printing the ammo.
‫So let's take the three nodes, the get convert and print string and we're going to right click collapse
‫to function and call this one print ammo.
‫Now as I said, we've got this functionality repeated in two places, so we'll delete where it does
‫this in begin play and just drag in print ammo from our functions list and connect up the execution
‫pins so that it runs after our print string.
‫Which brings me on to the last function I want to create, which we said we'd call print welcome messages.
‫So I'm going to select the to print strings after being in play collapse to function and call it print
‫welcome messages.
‫And this node, we can just now rearrange all of our nodes to make it far easier to read all of this
‫in one screen full.
‫You can see it's much easier to understand the functionality of our game now because it's all fits on
‫one screen and is nicely brought to a similar level of abstraction and understanding.
‫And this is what we would call self documenting code, because it is code that doesn't need comments
‫or documentation to explain what it does.
‫It just reads like English.
‫Great stuff.
‫I'll see you in the next lecture.