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:
- Declare the corresponding function in C++
- Implement the function in the Blueprint graph
- Call the overrided function
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):
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):
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:
- On the functions menu click the “+” symbol
- Choose Override
- Choose the function PrintOnScreen (the editor is going to place a new node at this point)
- Drag a wire from the newly placed node and choose the Print Text function
- Connect the IntToPrint to the Text
Your graph state should be similar to the image below:
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):