bps_to_cpp_bp2

In this tutorial, I’m going to show you how you can call functions (which are implemented in blueprints) in your C++ code. The steps required are the following:

  1. Declare the corresponding function in C++
  2. Implement the function in the Blueprint graph
  3. Call the overrided function

Let’s start!

Declaring the Blueprint Function

The functions I am going to create is going to accept as input a number and print it on our viewport.

I created a class named MyActor which inherits from Actor. Then, I’ve added the following functionality (lines 23-24):

bps_to_cpp_h1

Note 1: Always use int32 as a type instead of int because int type is dependent on how large the compiler is and you might have compile errors.

Note 2: BlueprintImplementableEvent means that the following function is going to be implement on the Blueprint Editor.

Now that the first step is completed let’s call the function in our code. Switch to the source file and inside the code of the BeginPlay() method call the PrintOnScreen function with input any integer you want (I used the number 1 in this example):

bps_to_cpp_s1

Note: As the comment suggests, the BeginPlay() function is called when the game starts or when the actor is spawned.

Implementing the function in Blueprint

This step involves the creation of a Blueprint class based on the class I created above. This means that I added a Blueprint class which has as a parent the MyActor class which I created in the previous step. If you are followed the steps above, open up your Blueprint and:

  1. On the functions menu click the “+” symbol
  2. Choose Override
  3. Choose the function PrintOnScreen (the editor is going to place a new node at this point)
  4. Drag a wire from the newly placed node and choose the Print Text function
  5. Connect the IntToPrint to the Text
  6. Compile

Your graph state should be similar to the image below:

bps_to_cpp_bp2

Now, don’t forget to place the blueprint in your level and press play.

You should see the number you typed in the top left corner of your viewport (mine is 1):

bps_to_cpp_output

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