Function Pointers or Delegates

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!

#Wiki Tutorial on Function Pointers

I wrote an entire tutorial on Function Pointers!

Enjoy!

#:heart:

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

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.

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