# Callback function

**URL:** <https://forums.unrealengine.com/t/callback-function/309626>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [March 6, 2015, 4:29pm UTC](https://forums.unrealengine.com/t/callback-function/309626 "2015-03-06T16:29:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LukasK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LukasK](https://forums.unrealengine.com/u/LukasK)\
**Post date:** [March 6, 2015, 4:29pm UTC](https://forums.unrealengine.com/t/callback-function/309626/1 "2015-03-06T16:29:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MickD777](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MickD777](https://forums.unrealengine.com/u/MickD777)\
**Post date:** [March 6, 2015, 4:38pm UTC](https://forums.unrealengine.com/t/callback-function/309626/2 "2015-03-06T16:38:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LukasK](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@LukasK](https://forums.unrealengine.com/u/LukasK)\
**Post date:** [March 7, 2015, 8:25am UTC](https://forums.unrealengine.com/t/callback-function/309626/3 "2015-03-07T08:25:30Z")

</div>

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 ☹  
As I said everything works only if everything is in one class.

Thanks for answer

---

<div class="post-metadata">

**Author:** ![MickD777](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@MickD777](https://forums.unrealengine.com/u/MickD777)\
**Post date:** [March 9, 2015, 2:00pm UTC](https://forums.unrealengine.com/t/callback-function/309626/4 "2015-03-09T14:00:21Z")

</div>

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.
