I can't set default structure function parameters

How can I set the default parameters of a function? int, float, FName, FVector no problem. But with my own structures, I don’t know how to do it.

UFUNCTION(BlueprintCallable, Category = “Container”)
void FindContainer (bool& Return, int& ID_ContainerData, int FindByID = -1, const FItem FindByItem);

const FItem FindByItem = ?

I know that you can simply move the FItem structure to the left, and leave the set parameters at the end, then everything will be compiled. But this is just a matter of aesthetics, since further on I have more complex functions.

I don’t believe there is any way to explicitly create a struct like that (someone correct me if i’m wrong) … and i’m running on little sleep right now, i know there’s some words i’m not thinking of here to describe that type of creation… just can’t think of it. :expressionless:

this is more of a C++ question than anything specific to Unreal, and I think that this is just probably not a good way to design function parameters in C++…

also you should probably pass in FindByItem by reference, so that you’re not making copies of structs every time you call that function. And typical Unreal convention is that if you’re making a function that can succeed or fail, you’ll have the function type return bool, and use by reference parameters for the results.

I don’t know what your exact task here is, but my guess is that you’re wanting either the int or the FItem as an input, and if you were to define both of them with a default parameter, then the user could pass in no int or FItem, and what would it do then?

If I’m correct, I’d go with overloaded functions, one with the int param and one with the FItem param, and no defaults.

1 Like

Thanks for your feedback. That’s right, the data will be passed by reference, I’m just experimenting and forgot to return the sign.
These functions will be called in BP, something like this. They have the same logic, they just store things in different databases, that’s all.
I would just like to learn how to initialize functions like this. But the compiler swears, asks to put FItem at the very top, or set the default values. This is not a problem, of course, but not everywhere it will be intuitively read to other people.

There is nothing special about default parameters in C++, you can set them like you would anywhere else in code. E.g. if your type has a default constructor:

void FindContainer (bool& Return, int& ID_ContainerData, int FindByID = -1, const FItem FindByItem = {});

While this is legal C++ the UHT only supports a small subset of the language and the last time I checked this did not work with BlueprintCallable functions. If that’s still the case you have no choice but to put the FItem before the default parameters.

However, as previously stated passing structs by reference-to-const (const FItem& FindByItem) is very common and almost always better which is probably the reason why they didn’t bother implementing default parameters for custom structs.

1 Like