How do I pass pointer to function as parameter?

I have declared a delegate inside my function, and to bind it easier, I just want user to pass a pointer to function, like

gameEvent.AddEvent(this,UBasicExample::SimpleEvent)

And inside, bind SimpleEvent to dispatcher. How should AddEvent look like?

Normally in C++ pointer type is declered this way

ReturnType (*NameOfPointerVarable)(ArgType, ArgType);

UE4 has special template class providing better support and it’s the one delegates are using

TFunction<ReturnType(ArgType, ArgType)> NameOfPointerVarable;

If you function type don’t have any return then you put void same as you do in function declarations, if you have no arguments you simply leave empty ()

Note that you can bind delegate to delegate. Delegate works like storage function bindings and you can pass then one to another. It also let you bind functions with extra arguments. I see this more rethen then use function pointers to forward it

1 Like

I use TFunction<void()> as you describe but in some instances get a VS “no instance of function template matches the argument list” error with the claimed expected argument type being some random class that’s not even referenced.

Intellisense Hover:
template void Test::Private::DoWork(T scope, Chaos::FRigidBodyContactConstraintsPostComputeCallback method)

No instance of function template Test::Private::DoWork matches the argument list argument types are (Test::Private::ABC, void(Test::Private::ABC::*)())

Build Error:
Error C2672 ‘Work’: no matching overloaded function found

namespace Test::Private {
	template<typename T>
	void DoWork(T scope, TFunction<void()> method) {
		//Some Work
	}

	class ABC {
		static void Test() {
			ABC obj = ABC();

			DoWork(obj, &Result);
		}

		void Result() {
			//Some Work
		}
	};
}