skip to Main Content

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:

vs-var

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:

  1. Go to the desired folder you want to save your Blueprint
  2. Right-Click in the empty space inside the folder and select BlueprintClass 
  3. Expand the All Classes menu
  4. In the text box just type in “MyActor bp_creation
  5. Select MyActor and click Next 
  6. 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:

  1. Go to MyActor.h where the variable is declared
  2. Go to line 22 and type in the following macro: UPROPERTY(EditAnywhere) *
  3. Compile your project

Your code should be the following:

macro-code

Now, go to blueprint you just created and take a look at the details panel. Your result should be similar to the image below:

var_expose

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.

Avatar photo

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top