Default Parameters with TArrays

Here is my function declaration:

virtual void SetCurrentScreen(TSubclassOf<UWavesUIScreen> newScreenParent, bool CloseAllOtherScreens, bool RemoveFromStack, const TArray<UWavesUIScreen*> ScreensToClose = TArray<UWavesUIScreen*>(), int32 id = 0);

Intelisense says it’s fine but on compile I get: C++ Default parameter not parsed: ScreensToClose “TArray()”

I’ve tried using the parameter as a reference but with the same result. I’ve seen some code that passes a TArray of AActor* with a default parameter just fine.

Do you use UFUNCTION here? Because this looks like UHT, not compiler error, it don’t know how to translate this to reflection system which is more limited then of what C++ can do. You would need to remove UFUNCTION() to do this.

1 Like

You can use the AutoCreateRefTerm macro to do this. In the example below, MyOptionalParam will be given an empty array if non is provided via Blueprints.

UFUNCTION(BlueprintCallable, meta = (AutoCreateRefTerm = "MyOptionalParam"))
void MyFunction(TArray<AActor*> MyOptionalParam);

You can make multiple parameters have default values by putting multiple parameter names in the AutoCreateRefTerm string, separated by commas:

UFUNCTION(BlueprintCallable, meta = (AutoCreateRefTerm = "MyOptionalParam1,MyOptionalParam2"))
void MyOtherFunction(TArray<AActor*> MyOptionalParam1, TArray<AActor*> MyOptionalParam2);
1 Like

But it still require passing argument, if is called from code. (however in BPs it apparently works)

Standard procedure Epic uses in the UE4 itself is to separate the C++ version from the Blueprint version by creating the function twice. Basically like so:

`void Foo( FString MyString = FString() );

UFUNCTION(BlueprintCallable, meta = (DisplayName = “Foo”, AutoCreateRefTerm = “MyString”))
void K2Foo( FString MyString );`