How to create pass-by-ref parameter with default value (something like “nullptr”) in Unreal's BlueprintCallable UFUNCTION?

Hello,

What I want to achieve:
A Blueprint Callable function that takes ‘UPARAM(ref) bool&’ as a parameter which can but don’t have to be passed inside of Blueprint Graph (variable don’t have to be pluged into input pin to this node for the graph to compile). And by basing on if the parameter has been passed or not, the function will behave in a slightly different way.

For example I was able to create something ugly like this

void Func(bool& param = *(bool*)0)
{
    if (&param == nullptr)
        // do something
}

Above C++ code (sadly, it’s UB), allows to call ‘Func()’ without passing any parameters in it and execute code basing on if the parameter has been passed which is the exact behavior that I’m looking for.

But when I try to declare something similiar as BlueprintCallable UFUNCTION in Unreal, this of course will not compile:

UFUNCTION(BlueprintCallable)
static void Func(UPARAM(ref) bool& param = *(bool*)nullptr);

due to error:

C++ Default parameter not parsed: param “(bool)nullptr”

I’ve also tried to declare global constant variable that would be set as default parameter but the result is the same - compiler throwing “Default parameter not parsed…” Error.

So my question is:

  • Is the behaviour that I want to achieve even possible in BlueprintCallable functions?
  • Is there any workaround to what I’ve described above?

Thanks in advance! I appreciate any help.
Let me know if something is unclear and I will edit the post.

EDIT - Possible but not exact sollutions:

  • meta = AutoCreateRefTerm( "param" ) can be specified in the UFUNCTION declaration. This allows Blueprint Node to have default value in pass-by-ref pin. Hovewer with this approach another condition (bool pin) is needed because It is not possible check if actual variable gets passed or not.
  • TOptional is a data container that exacly fits here BUT TOptional cannot be used as parameter in BlueprintCallable UFUNCTION due to Error: Unrecognized type 'TOptional' - type must be a UCLASS, USTRUCT, UENUM, or global delegate. OR at least I don’t know how to use it.
1 Like

No, this isn’t something that you can do the way that you way. At best you can have the bool& parameter and include the metadata meta = AutoCreateRefTerm( "param" ) in the UFUNCTION declaration. This will let you use have the parameter as a reference without making it a required input. However you still wouldn’t be able to distinguish being that default and something actually connected to the pin. If you really need that you’d have to have a second boolean to trigger the if-branch instead of the param being nullptr.

On a related note, bool& param = *(bool*)0 is a very, very bad idea. The whole point of a reference is that it being null is an error. If you’re already writing pure C++ code, you would be better off doing bool *param = nullptr.

1 Like

Thanks, I think this actually might do the job in my specific case.
Just want to ask about one more thing related to this question:
Can I somehow specify the default value when using meta = AutoCreateRefTerm( "param" ) ?
The boolean pin with AutoCreateRefTerm will always show up ‘False’ state as the default one. And for example I want it to be ‘True’. I’m not able to find if this is possible. Documentation about meta specifiers doesn’t rly help Metadata Specifiers | Unreal Engine Documentation

Not when you have a non-const reference like you’re using. If you had a const reference you could (and without any of the other meta/UPARAM stuff) but it would no long be an out parameter.

In fact it might be better to just have a regular non-reference input (which you would be able to do as bool param = true) and then just return a new value. If the caller wants to assign that value back to the input, they can or they could connect it directly to a condition or anything else. Non-const input references are sort of fraught and problematic (as you’re finding) and many times it can be better to just develop an alternate approach.

1 Like