Pure Functions

I noticed a couple questions here about pure functions and what their point is. I wanted to give an answer for anyone wondering the same question. For context, my day job is programming, but I’m not a gavedev, so this answer is based on my experience writing code for other stuff.

Take the blueprint at 5:30 in the video, there’s a pure function “Has Ammo?” and a non-pure function “Print String”. The non-pure function “Print String” can only execute after the “Branch” is done executing, in other words, the non-pure functions execute in order, one after the other, and the developer has to line up all the white lines to make that happen.

The pure function is the opposite, it executes when it’s needed and does not have to be ordered with the white event line. The output of “Has Ammo?” is used by the “Branch” when the branch executes. If, hypothetically, “Has Ammo?” was non-pure; the white event line would have to feed from “Space Bar” into “Has Ammo?”, then into “Branch”, then into “Print String”; making the blue print slightly more difficult to edit and understand (for a small example like this, it doesn’t make a big difference, but for large blue prints, reaching over a hundred functions, its value really starts to shine. Imagine the complexity of a single white event line feeding through a hundred function blocks).

Using pure functions simplifies the blue print, because you don’t need extra white event lines all over the place to trigger the execution of pure functions. It makes the blue print easier to read and easier to develop.

I hope this makes sense! I don’t have much experience teaching so sorry if it just makes things more confusing.
There’s probably a lot more nuance I don’t know about, but in the context of this warehouse wreckage game that’s what’s going on!

3 Likes

Thank you for taking the time explain that, it makes a lot more sense to me now why you would choose to make a pure function. So is the concept of a “pure” function a blueprint only concept?

Yeah, I can’t really thing of a C++ concept that’s similar. Glad this helped you though!

So later on in the course Sam covers “Const Member Functions” which he describes as the C++ equivalent. While not exactly equal as he explains, it seems that the goal in both cases is to make your code safer/cleaner. For example, in BP the pure function can’t be used to set anything and similarly making a function constant in C++ prevents the function from modifying the class (or even calling other functions that modify the class).

I’ve always struggled with the idea of why we’d use constants, and I probably would have still struggled with the explanation in the lecture had it not been for your example within blueprints, so thanks again!

I didn’t notice that, const functions are a pretty good comparison! Glad this helped!

Privacy & Terms