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:

Then, I’ve implemented the following logic:

Compile and save your code. When you’re done with that, add the following Blueprint code to your character:

bp_reflection_code

 

Here is my output after pressing E a couple of times:

reflection_output2

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!

Share postShare on Facebook0Tweet about this on TwitterShare on LinkedIn0Share on Reddit0