How to Use An Array of Pointers to Bound Functions in a UCLASS()?

Dear Friends at Epic,

I have tried very much to do this on my own since it does seem like just a c++ question.

But I rather stumped at the moment.

I am trying to make an array of function pointers, because I have a very large number of virtual functions I will be having each character implement. The virtual functions are basically a series of AI actions.

A function pointer array would be lovely!

#.h

typedef void (AEvolver::*EvolverFunctionPtr)(void);
	EvolverFunctionPtr BehaviorFunctions[BEHAVIORS_MAX];
	
	virtual void InitBehaviors();
	virtual void PlayBehavior_Idle_LookLeftToRight();
	virtual void PlayBehavior_Idle_LookFullCircle();
	virtual void PlayBehavior_Idle_ScanUpDown();

#.cpp

void AEvolver::InitBehaviors()
{
	BehaviorFunctions[0] = &AEvolver::PlayBehavior_Idle_LookLeftToRight;
	
	( * (BehaviorFunctions[0]))();
}
void AEvolver::PlayBehavior_Idle_LookLeftToRight(){}
void AEvolver::PlayBehavior_Idle_LookFullCircle(){}
void AEvolver::PlayBehavior_Idle_ScanUpDown() {}

#Compile Error

1>E:\RocketVictory\VictoryGame\Source\VictoryGame\Private\Characters\Evolver.cpp(106): error C2171: '*' : illegal on operands of type 'AEvolver::EvolverFunctionPtr'
1>E:\RocketVictory\VictoryGame\Source\VictoryGame\Private\Characters\Evolver.cpp(106): error C2064: term does not evaluate to a function taking 0 arguments

#What Compiles

BehaviorFunctions[0] = &AEvolver::PlayBehavior_Idle_LookLeftToRight;

#What Doesnt

( * (BehaviorFunctions[0]))();

I’m confused as to why I can get this to compile

BehaviorFunctions[0] = &AEvolver::PlayBehavior_Idle_LookLeftToRight;

but I cannot find a way to deference and use the function pointer.

#Background

Originally I was trying to do this and was getting the compile error that there was an illegal use of & with a bound function, so I changed my typedef to this:

typedef void (AEvolver::*EvolverFunctionPtr)(void);

from this

typedef void (EvolverFunctionPtr)(void);

#Thanks for the Help

Thanks for any help with this!

:slight_smile:

Rama

Hey Rama,

When we tried testing this, we searched around with your compiler errors and this looks like (as you said) it is just a C++ error, not an editor-related problem. In looking at the information for the C2064 compiler error on this page, one of the possible solutions suggested using:

this->*

So in our local repro, we used a similar code setup to what you posted and we think if you use:

( this->* (BehaviorFunctions[0]))();

instead of

( * (BehaviorFunctions[0]))();

you should be able to get around your compile error.

Let us know if you get around this issue and have any further questions about this topic that are editor related.

Thanks!

-Steve

Wooohooo!

Thanks Steve and other Epic Devs!

That worked!

#Tutorial Posted

I have posted a tutorial on UE4 Class Function Pointers on the forums, thanks to your solution :slight_smile:

If you can, please show the additional Epic Devs who solved this problem for me this forum post so they know their time was put to good use

:slight_smile:

http://forums.epicgames.com/threads/973803-Tutorials-Entry-Level-UE4-C-Overview-Q-amp-A-gt-Array-of-UE4-Class-Function-Pointers?p=31755406&viewfull=1#post31755406

Rama