Download

Passing member function ( FuncName from AddDynamic ) as parameter

Hi!
How could I pass a member function that will be used in .AddDynamic as parameter?
Example:


void ACustomActor::CreateMessage(**/* what type should I use here? */**)
{
MessageWidget = CreateWidget<UMessageUserWidget>(this, MessageWidgetClass);
MessageWidget->AddToViewport();

MessageWidget->OpenMessage();

// **So I could replace hard code here:**
//MessageWidget->OnCloseMessage.AddDynamic(this, **&ACustomActor::CloseMessage**);
}


So I could call it as:


CreateMessage(**&ACustomActor::CloseMessage**);

I couldn’t even create a variable from it, it didn’t compile:


TFunction<void(ACustomActor::*)(void)> Function = &ACustomActor::CloseMessage;

I’m trying to do it more Unreal than pure C++ as it need to be UFUNCTION() and I don’t know if it could have leaks or other problems.

Thank you!

Signature should be


void ACustomActor::CreateMessage(void(ACustomActor::*Func)(void))
{
  // ...
  MessageWidget->OnCloseMessage.AddDynamic(this, Func);
}


BTW I’m not sure that UFUNCTION support function pointer or TFunction as parameter.

@Emaer Thank you.
It doesn’t work, Unreal tries to cast the function to its name, but “Func” ( parameter name ) is used as the function name:


checkf(Result && Result[2] != (TCHAR)'0', TEXT("'%s' does not look like a member function"), InMacroFunctionName);

I’m not sure if what I’m trying to do is possible, but thanks!

Oh yeah, right. AddDynamic is a macro not only bounds function, but also “extracting” function name in preprocessor stage.
This can be solved by creating a simple wrapper class, but internet say UFUNCTION don’t allow function pointer of any types (raw,TFunction,etc.) as a param.
So, actually I don’t see any other option than wrapping it in UObject* like:


void ACustomActor::CreateMessage(UWrappedFunction* Wrapped)