Download

Function pointer inside a structure poitning to a function in a class

I have a function pointer inside a structure something like this.


USTRUCT()
struct FEventFloat
{
     void(*Event)(const float);

}

The function this function pointer is pointing to is supposed to be called by another function inside the structure. So it is important to keep this functionpointer inside the structure.

Now, the problem is I am assigning this pointer in the class of the function like this.


APlayerPawn::APlayerPawn()
{
     ....................
     ....................

     EventHealth.Event = &APlayerPawn::PlayerTakeDamage;
}

This thing throws an error(which it should)



1>C:\Users\Srikant\Documents\Unreal Projects\IW1\Source\IW1\Player\PlayerPawn.cpp(107): error C2440: '=' : cannot convert from 'void (__cdecl APlayerPawn::* )(const float)' to 'void (__cdecl *)(const float)'


So how can i covert this class function to a normal function? Is it possible to do so?

Could you elaborate a bit more on why you want function pointers in the first place? What goal are you trying to achieve? Perhaps you can just use delegates?

Do delegates work inside structures?

I am trying to make a data driven type of variable.
E.g. say a float.

My structure class is something like this


USTRUCT()
struct FEventFloat
{
	GENERATED_USTRUCT_BODY()
	
private:

	UPROPERTY()
	float EFloat;

public:

	void(*Event)(const float);

	float& operator=(const float& Other)
	{
		this->EFloat = Other;
		if (Event != 0)
		{
			(*Event)(this->EFloat);
		}
		return this->EFloat;
	}

	FEventFloat()
	{
		EFloat = 0;
	}
};

basically, when the value of the float is changed, it calls the function pointer with the new value. In future, I will replace the function pointer with an array of function pointers which all point to different functions.

Instead of using a Get-er and Set-er, I am trying to use something like this.

I tried declaring a delegate inside the structure but it didn’t work I trying this.