Invoking UFunction by name via ProcessEvent in Unreal Engine 5 C++

Written on: 27 July 2024


Example Class:

UCLASS()
class TESTING_API UMyObject : public UObject {
    GENERATED_BODY()

   public:
    UFUNCTION()
    void MyMethod(){ UE_LOG(LogTemp, Warning, TEXT("MyMethod Invoked")); }

    UFUNCTION()
    void MyMethodArgs(int32 Value){ UE_LOG(LogTemp, Warning, TEXT("MyMethodArgs called with = %d"), Value); }
};

Invoking UFunction:

// Creating Object
UMyObject* MyObj = NewObject<UMyObject>(this, TEXT("MyObject"));



// Invoking function without arguments
FName MethodName = FName(TEXT("MyMethod"));
UFunction* Func = MyObj->FindFunction(MethodName);
if (Func) {
    MyObj->ProcessEvent(Func, nullptr);
}



// Invoking function with arguments
FName MethodArgsName = FName(TEXT("MyMethodArgs"));
UFunction* FuncArgs = MyObj->FindFunction(MethodArgsName);
if (FuncArgs) {
    struct
    {
        int32 Value;
    } ParamStruct;
    ParamStruct.Value = 56;

    MyObj->ProcessEvent(FuncArgs, &ParamStruct);
}