TFunction assignment, cannot convert error

Hi

I want a function callback in parameter of an other function, but I have a error when I use TFunction.

This is my callback function declaration:


void MyClass::CallbackFunction(const MyParamClass& Param);

This is the line where I assign the TFunction (I have the same error when it’s a parameter):


TFunction<void(const MyParamClass&)> Callback = &MyClass::CallbackFunction;

I have this error when I compile it:


error C2440: 'initializing': cannot convert from 'void (__cdecl MyClass::* )(const MyParamClass &)' to 'TFunction<void (const MyParamClass &)>'

It seams to be an syntaxe error, but I haven’t enough experience with TFunction.

If somebody can help me…

Hi. I Had same problem and finally solved it this way.
I Have two functions, second one ReciveFunctionPointerAsArgTF takes TFunction as arg.


// This function will be passed as arg
int LogApple(int ApplesCount, FString Arg2) {
	ApplesCount = ApplesCount + 1;
	UE_LOG(LogTemp, Warning, TEXT("Apples Count = %d"), ApplesCount)

	return ApplesCount;
};

// TFunction based pointers

// This function recives pointer to other function as argument
void ReciveFunctionPointerAsArgTF(int Arg1, TFunction<int(int, FString)> pFunc) {
	int ApplesReturn = pFunc(5, "Test String");
	UE_LOG(LogTemp, Warning, TEXT("Apples Returned = %d"), ApplesReturn)
};

Here how i call them:

TFunction<int(int, FString)> pFuncRef = [this]( int Arg1, FString  Arg2){ return LogApple(Arg1, Arg2); };
ReciveFunctionPointerAsArgTF(5, pFuncRef);

And it works.
As you can see i wrapped my function refrence into lambda;

[this]( int Arg1, FString  Arg2){ return LogApple(Arg1, Arg2); };

I Don’t know why it dosen’t want to work this way instead:

TFunction<int(int, FString)> pFuncRef = &UPointersComponent::LogApple;

as it should in “classic” way.


If Anyone have better solution - please write it here, because wrapping refrenced function into lambda looks very odd and not “right”.