2

I have a method in my controller class, I want to store the method in a TFunction object. I've tried to use: TFunction<void ()> FuncPtr{&PlayerController::ClassMethod}; It doesn't work. My function signature is: void ClassMethod();

GigaKox69
  • 41
  • 2
  • If `ClassMethod()` is not `static`, then `TFunction` will need a `PlayerController` object instance to call `ClassMethod()` on. But you are not assigning such an object to the `TFunction`. `TFunction` is designed to work much like C++'s `std::function`, and in C++ you would use `std::bind()` or a lambda to bind an object instance to a `std::function`. Not sure the Unreal equivalent of that. – Remy Lebeau Nov 01 '21 at 22:00
  • As Remy is pointing out, you would bind with an instance of the class. [This](https://stackoverflow.com/questions/17131768/how-to-directly-bind-a-member-function-to-an-stdfunction-in-visual-studio-11) covers a lot of the territory. – lakeweb Jan 10 '22 at 20:25

1 Answers1

-1
//Let's say so:

.h
UFunction* ExecuteFunction;

UFUNCTION()
    void ChangeState(FName NextState)
UFUNCTION()
    void GlobalState_One()
UFUNCTION()
    void GlobalState_Two()
.cpp

void ChangeState(FName NextState)
{
    FName NameFunction = FName(FString("GlobalState_") + NextState.ToString());
    ExecuteFunction = FindFunction(NameFunction);
}

//Place of call
if (ExecuteFunction != nullptr)
            ProcessEvent(ExecuteFunction, nullptr);
//

//...
ChangeState("One");
//...

void GlobalState_One()
{

}
void GlobalState_Two()
{
    
}
  • 3
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 10 '22 at 22:01