Download

Function Pointer with UFUNCTION() Macro ? C++

Hello!

I have two functions in my class and one enum. The enum is called FireType and contains “LineTrace” and “BulletSpawn”. In my blueprint i want to select the desired enum type to call the specific funktion in the tick function of the class. Normaly i would simply call an if statement every frame but i would like to learn new things and it hurts when i see an if statement that gets called every frame even if the condition is only set up once at the start of the game.

So i thought about a function pointer. I just set up the function pointer at the start of the game to the right function, based on the selected enum type and call the ptr in the tick function.

I never did a function pointer before but it worked well. But i dont know which marco i should use on the pointer type. I tried UFUNCTION() because its a function pointer ? - doesn´t work. I tried UPROPERTY() because its also just a variable holding a memory adress. - doesn´t work. So what can i use in this situation?

why i even want to use a macro? Because of the garbage collection and, as far as i know, the gb only works when the variable/function has a macro.

btw, is a function pointer call as performant as a normal function call?

The defenition in the header file:



protected:

typedef void (AWeaponGun::*FireTypeFunctionPtr)(void);

FireTypeFunctionPtr PtrFireType;

UFUNCTION()
void FireLineTrace();


The call in the .cpp file


void AWeaponGun::BeginPlay()
{
   //...other code...
PtrFireType = &AWeaponGun::FireLineTrace;
}

void AWeaponGun::Tick(float DeltaTime)
{
   //...other code...
    (this->*PtrFireType)();
}