In this post I'm going to show you how to create latent Blueprint nodes that…
In this post, we’re going to utilize the built-in reflection system in UE4 to perform changes in our game at runtime. Reflection is the ability of a program to inspect its own code (or other associated code) and change at runtime (for more information about reflection, click here). By default, C++ isn’t capable of that, however Unreal Engine is kind enough and created this ability for us!
In this post, we’re going to create one BlueprintCallable function which will take a single FString parameter. That function will search all the available functions of our object and if it manages to find a function whose name is the same of our parameter, it will execute it. This is the most common case of using reflection.
In case you find yourself writing C++ out of the engine and want to use this feature, a simple workaround is to create a map container with string keys and pointers to functions as mapped types. Then, you can manual search your map and execute your associated function using the corresponding function pointer.
Let’s move on and use the reflection system of the engine!
Utilizing UE’s reflection system
I’ve created a Third Person C++ template project and inside my character’s header file, I’ve added to following declarations:
Character's header file
/*This function will search which function to execute*/
UFUNCTION(BlueprintCallable, Category = Reflection)
void ExecuteFunction(FString FunctionToExecute);
Then, I’ve implemented the following logic:
Character's source file
void AReflectionCharacter::ExecuteFunction(FString FunctionToExecute)
//FindFunction will return a pointer to a UFunction based on a
//given FName. We use an asterisk before our FString in order to
//convert the FString variable to FName
UFunction* Function = FindFunction(*FunctionToExecute);
//When you're using a pointer - make sure to check if it's valid first!
//The following pointer is a void pointer,
//this means that it can point to anything - from raw memory to all the known types -
void* locals = nullptr;
//In order to execute our function we need to reserve a chunk of memory in
//the execution stack for it.
FFrame* frame = new FFrame(this, Function, locals);
//Unfortunately the source code of the engine doesn't explain what the locals
//pointer is used for.
//After some trial and error I ended up on this code which actually works without any problem.
//Actual call of our UFunction
CallFunction(*frame, locals, Function);
//delete our pointer to avoid memory leaks!
Compile and save your code. When you’re done with that, add the following Blueprint code to your character:
Here is my output after pressing E a couple of times:
A big thank you to Eduard Gelbling and Sergiu Crăiţoiu for their suggestions and ideas which led to an updated code in this post! You’re awesome!