# 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:** 1\
**Showing post:** 2

<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!
}
```

---

_[View the full topic](https://forums.unrealengine.com/t/function-pointers-or-delegates/280111)._
