Blueprint node input short-circuiting

One major thing that is confusing for programmers coming to blueprint scripting is that boolean nodes and select nodes evaluate all inputs rather than only evaluating inputs that are necessary for the result (ie. short-circuiting). I didn’t see a similar post and I’m curious if other people feel the same way.

For example, I can write a valid line of C++ code like this:

if (pTarget != nullptr && pTarget->NetPriority == 5) {}

But the same thing in blueprint will crash if the target is not valid because the lower logic is evaluated by the “AND”:

51837-shortcircuit.png

The workaround involves adding a branch and a temporary variable but that doesn’t seem as clean and can’t be as optimal. Anyone agree?

But if it isn’t valid then you are trying to get a non existing value. Makes sense to me.

With short-circuiting if the first input is false to an AND node the second input should not be evaluated. So if Target is not valid it should not query the NetPriority and crash.

Similarly if the first input is true to an OR, there is no need to evaluate the following inputs. And SELECT should only evaluate the input that was selected.

Definitely. Came across some weird behaviour/errors caused by assuming it worked how you described, and died a little when I found this post.

I can’t fathom why it doesn’t work like that, and I would really like to hear from Epic on this.

makes sense what you’re saying I think however

The workaround involves adding a branch and a temporary variable but that doesn’t seem as clean and can’t be as optimal. Anyone agree?

I don’t know if a temp variable is needed.

target is valid? |Branch|> true > target NetPriority ==5? |Branch|>true >next step

if false on either branch it just dies

While I do agree with you that if one branch of an “And” or an “Or” satisfies (or fails) the check, the second check should not be evaluated. However, for the example you show above, there is an easy solution since you are evaluating “Is Valid”. Rather than use the function “IsValid” use this node:

52878-isvalid.png

This will solve the need to run a branch as it is essentially a branch and is valid function rolled into one.

I was originally thinking of a situation where I wanted to get a boolean value either to be used in a later branch or as the return value to a function. Here is what I want to do:

and here is what I am required to do:

Along with creating additional variables, now I’m threading exec lines through what could be a completely pure region of the graph.