Callback function

Hello. Can somebody please show me example how I can do this ?
Basically I am talking about callback functions.

Let’s say
Class A is moving some object.
Class B needs to know when the moving is finished.
I need to pass some method from class B to A. And that method will be called when Class A finish the movement.

Thanks for answer.

1 Like

There is a delegate macro for that.

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FMyDelegate);

Than in the class you want to hold that reference you add :

FMyDelegate OnMyDelegateCalled;

To add/remove listeners:

OnMyDelegateCalled.AddDynamic(this, &UTransitionManager::MapFullyReady);
OnMyDelegateCalled.RemoveDynamic(this, &UTransitionManager::MapFullyReady);

To call that delegate:

OnMyDelegateCalled.Broadcast();

There is other macros to declare delegates with parameters, here is an example:

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FMyActionDone, float, myFloatValue);

Hope this helps!

Mick

1 Like

It works only if everything is in class A.

I tried:

CLASS B .h

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnSetLocation);
...

FOnSetLocation GetLocationCallback();

...
FOnSetLocation		__onSetLocation;

CLASS B .cpp

FOnSetLocation AFreeCam::GetLocationCallback()
{
	return __onSetLocation;
}

// Then if object is set to location
if (__onSetLocation.IsBound())
{
     __onSetLocation.Broadcast();
}

======================================================================

CLASS A .h

UFUNCTION()
void MoveToMouseCursor();

CLASS A .cpp

__freeCam->GetLocationCallback.AddDynamic(this, &AStarcrooksPlayerController::MoveToMouseCursor);

So In this case __onSetLocation.IsBound() Returns always 0. So __onSetLocation.Broadcast(); Do nothing :frowning:
As I said everything works only if everything is in one class.

Thanks for answer

It does work in mutiple class, I do it myself. Teh way you do it with a getter, I believe you get a copy, not the reference. Lose the getter for your test.