# Function Pointers or Delegates

**URL:** <https://forums.unrealengine.com/t/function-pointers-or-delegates/280111>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine\
**Created:** [April 9, 2014, 7:43pm UTC](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111 "2014-04-09T19:43:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [April 9, 2014, 7:43pm UTC](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111/1 "2014-04-09T19:43:08Z")

</div>

Hi All,

Another C++ noob question here which I would appreciate some input on.

I have the following situation:

I have 2 classes, let’s call them The Boss and The Worker.

The Boss creates a new instance of The Worker, and instructs The Worker to start doing its thing.  
Now when certain conditions are met in The Worker, it needs to post back to The Boss telling it that something’s happened.

Now, I’m looking to make The Worker generic, in the sense that when The Boss instructs The Worker to start doing its thing, it must also give it a reference to one of the Boss’ functions which must be called when The Worker wants to inform The Boss of something happening.

I’ve tried getting function pointers as well as delegates working but I’m going on 2 hours here without any results, so any help would be greatly appreciated.

I suppose my question officially is:  
What approach should I use to solve this, and example code would be greatly appreciated.

Thanks!

---

<div class="post-metadata">

**Author:** ![Rama](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/rama/32/882413_2.png) [@Rama](https://forums.unrealengine.com/u/Rama)\
**Post date:** [April 9, 2014, 9:47pm UTC](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111/2 "2014-04-09T21:47:15Z")

</div>

#Wiki Tutorial on Function Pointers

I wrote an entire tutorial on Function Pointers!

> **[A new, community-hosted Unreal Engine Wiki](https://forums.unrealengine.com/t/a-new-community-hosted-unreal-engine-wiki/141494)**
>
> After over a year in maintenance mode, the official Unreal Engine Wiki is now permanently offline. These resources now live on a new community-run Unreal Engine Community Wiki — ue4community.wiki! You will be able to find content from the official...

Enjoy!

#❤

Rama

#The Code, not as pretty as on wiki

```
#include "YourClass.generated.h"
 
#define BEHAVIORS_MAX 30
 
UCLASS()
class AYourClass : AActor //or any other class setup
{
	GENERATED_UCLASS_BODY()
 
public:
 
	//The Function Pointer Variable Type
	//Functions take in 0 parameters and return void
	typedef void (AYourClass::*FunctionPtrType)(void);
 
	//A static array of 30 Function Pointers
	FunctionPtrType BehaviorFunctions[BEHAVIORS_MAX];
 
	//Play a Behavior from the Function Pointer Array
	//	implementation does not vary in subclasses, so not virtual
	void PlayBehavior(int32 BehaviorIndex );
 
	//Initialize the array
	void InitBehaviors();
 
	//The actual functions which are implemented in subclasses
	//or this class itself
	virtual void PlayBehavior_Idle_LookLeftToRight();
	virtual void PlayBehavior_Idle_LookFullCircle();
	virtual void PlayBehavior_Idle_ScanUpDown();
	//...more functions

.cpp

#define LOOK_FULL_CIRCLE 0
 
#define SCAN_LEFT_TO_RIGHT 1
 
#define SCAN_UP_DOWN 2
//... more function index defines to 29
 
void AYourClass::InitBehaviors()
{
	BehaviorFunctions[LOOK_FULL_CIRCLE] = &AYourClass::PlayBehavior_Idle_LookFullCircle;
	BehaviorFunctions[SCAN_LEFT_TO_RIGHT] = &AYourClass::PlayBehavior_Idle_LookLeftToRight;
	BehaviorFunctions[SCAN_UP_DOWN] = &AYourClass::PlayBehavior_Idle_ScanUpDown;
	//...more functions
}
 
void AYourClass::PlayBehavior_Idle_LookLeftToRight(){}
void AYourClass::PlayBehavior_Idle_LookFullCircle(){}
void AYourClass::PlayBehavior_Idle_ScanUpDown(){}
//...rest of functions
 
 
void AYourClass::PlayBehavior(int32 BehaviorIndex )
{
	//valid range check
	if (BehaviorIndex >= BEHAVIORS_MAX || BehaviorIndex < 0) return;
	//~~
 
	//Special Thanks to Epic for this Syntax
	(this->* (BehaviorFunctions[BehaviorIndex]))();
 
	//the above line plays the appropriate function based on the passed in index!
}
```

---

<div class="post-metadata">

**Author:** ![staticvoidlol](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@staticvoidlol](https://forums.unrealengine.com/u/staticvoidlol)\
**Post date:** [April 10, 2014, 6:50am UTC](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111/3 "2014-04-10T06:50:31Z")

</div>

Rama you are a legend. Thanks!

P.S. I see the page was tagged as [Category:Code], so that’s why I didn’t find anything on the wiki.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_0dc1700e](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_0dc1700e](https://forums.unrealengine.com/u/anonymous_user_0dc1700e)\
**Post date:** [June 7, 2014, 11:50am UTC](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111/4 "2014-06-07T11:50:15Z")

</div>

Delegates ARE function pointers, with some extra functionality built in.
