‫Hello and welcome.
‫In this lecture, we're going to be learning how we can remove execution pins from our own functions
‫and how and when it's a good idea to do this.
‫Let's dive in.
‫So it's a little bit weird that our has ammo function now has execution pins when previously we didn't
‫need that and that's kind of made our code a little bit worse.
‫So what is that?
‫Why has that happened and why do some function nodes require execution pins and other ones don't?
‫Well, it all comes down to something called a side effect.
‫So what is a side effect?
‫Essentially, a side effect is when a function has some sort of observable effect after it has been
‫run.
‫Now, what do I mean by this?
‫Let's have a look at some examples of functions with side effects so that you can understand prints,
‫print, string.
‫After you have run print string.
‫It is quite clear that something has happened because there is now a string on your screen.
‫You can see that there is a string add impulse that clearly has an observable effect because suddenly
‫the thing that you've added impulse to goes flying off into the distance.
‫Set ammo has an observable effect because after you've set ammo, the value in that variable has changed.
‫Now at this point, you might be wondering, well, surely every function has an observable effect.
‫Otherwise, what is the point of it?
‫Well, not quite.
‫There are such things as pure functions.
‫What is a pure function?
‫It's basically a function with no side effects.
‫However, it does have return values.
‫So there are values coming out of that function.
‫And that is the entire purpose of the function is to calculate and give us those values.
‫So again, let's have a look at some examples because this will make it a lot clearer.
‫Get ammo.
‫That is a pure function.
‫It has no side effects because you can get ammo as many times as you like.
‫And unless you do something with the value that you got out of it, your program, your game is not
‫going to be any different.
‫You can just keep getting ammo all the time, all all the way through your game and nothing is going
‫to be observable different about your game.
‫Get active forward vector.
‫That's another one where you could get it as many times as you like.
‫It's not going to change anything until you do something with the value returned and all these mathematical
‫functions like multiply minus greater.
‫All of these nodes don't have an effect on your project until their results are used for some greater
‫purpose.
‫So these are called pure functions, and you can probably see that these are exactly the nodes that
‫didn't have any execution pins because they don't need them.
‫It doesn't matter when I run get ammo or whether I run it 50 times or one time, it's not going to matter
‫because it's got no observable side effects.
‫So how can we indicate to blueprint that a function is pure?
‫Well, let's have a look at my test function here.
‫So I'm going to select it in the event graph or over in the my blueprint pane.
‫And you can see that in the details pane there is a checkbox labeled pure.
‫So if you check that checkbox, notice the execution pins just disappeared.
‫And all we've got now are the input and outputs of this function.
‫Now this function will get run whenever we need the output.
‫So if I plug this output into a print string, then it's going to run this function before the print
‫string so that it has the result in time to execute the print string.
‫We can do the same with any function where there are no side effects, where there are no observable
‫effects such as our has ammo function.
‫So I'd like to challenge you to make has ammo into a pure function.
‫So select it and set that checkbox to make it pure.
‫And what happens to the execution flow now.
‫What currently it was hooked up through the function.
‫Where does it go?
‫Pause the video and have a go.
‫Okay.
‫Welcome back.
‫So let's select has ammo.
‫We'll check the pure check box and instantly you see that the execution pins disappear and the execution
‫flow that previously flowed through has ammo is now going directly on to our branch statement, which
‫is exactly what we want now has ammo is just going to be run as soon as the branch statement is run
‫to get hold of the return value.
‫Now a word of caution only use this pure checkbox when you are sure that a function has no side effects.
‫Common cases and use cases for this are when your function is simply trying to calculate a value such
‫as has ammo.
‫And that is why this function doesn't follow the convention of having a verb naming because it isn't
‫doing something, it is getting something or it is answering a question.
‫In this case, the question is has ammo.
‫And in fact, we could even put a little question mark in our function, naming something that you can
‫do in Blueprint that you can't do in C++, which allows us to see that, yes, this function is answering
‫a question.
‫It isn't trying to do something.
‫So often the distinction between pure and non pure functions are whether it fits with a verb or whether
‫it fits with a question.
‫So to review, let's have a look at our glossary.
‫A side effect is an observable effect of a function we've seen and we've seen examples of those functions.
‫And a pure function is a function with no side effects, such as these getting functions or question
‫answering functions.
‫That's it for this lecture.
‫I'll see you in the next one.