While our game may be running without any issues in the editor or even in…
Exposing C++ variables to Blueprints
In this tutorial, I’m going to show you the required workflow in order to expose variables which were created in your code in the UE4 Editor. While this tutorial is dedicated to variables, the workflow is almost the same if you want to expose other aspects of your code. Even though this tutorial might seem quite long, this process is easy and after you complete it, exposing variables and other aspects of your code will be a matter of seconds.
Exposing variables, methods, etc… in the UE4 Editor requires a basic use of Macros. In Unreal Engine macros are used (mostly but not always) in order to inform the editor about specific characteristics of your class, variables or functions. These characteristics, declare for example that you want to be able to set a variable of a Blueprint directly from the Editor or maybe you want the ability to lock that variable so designers cannot change that specific value.
If it sounds complicated don’t worry – it’s not! The moment you complete this workflow everything will be clear to you. So, let’s start!
Creating the required C++ Class and variable
If you want to expose C++ variables to Blueprints you first need a class which will contain the variables you want to expose. So let’s start by that.
I created a C++ class named MyActor and in the header file I declared a variable as seen below:
Creating a Blueprint based on a C++ Class
In this step, I’m going to create a Blueprint based on the C++ Class I created above. To do that:
- Go to the desired folder you want to save your Blueprint
- Right-Click in the empty space inside the folder and select BlueprintClass
- Expand the All Classes menu
- In the text box just type in “MyActor”
- Select MyActor and click Next
- Rename the new Blueprint as BP_MyActor (Optional – I just like to keep things neat)
Exposing a variable using a Macro
In order to expose the Health variable I added in the first step in the Unreal Engine editor:
- Go to MyActor.h where the variable is declared
- Go to line 22 and type in the following macro: UPROPERTY(EditAnywhere) *
- Compile your project
Your code should be the following:
Now, go to blueprint you just created and take a look at the details panel. Your result should be similar to the image below:
In case you don’t see the result above and your code compiled successfully close your blueprint and open it again.
*The EditAnywhere is a macro specifier. Each macro has a set of specifiers. For a complete list of available specifiers for the UPROPERTY() macro take a look at the official Engine documentation.
This Post Has 0 Comments