Pages

domingo, 6 de setembro de 2020

Creating a Blueprint based on the TutoProjectCollectable C++ class

In this article, we are going to create a Blueprint that will use the TutoProjectCollectable C++ class as the parent class. This is a common workflow in Unreal Engine, where you create a base class in C++ with several features and create a child class in Blueprint to be able to modify some properties directly in the editor.

Before creating the Blueprint, add and select an instance of TutoProjectCollectable in the level, and see in the Details tab that you can modify the Static Mesh and the Material used by this instance, as shown in this image:


But if you want to create another type of item that can be collected by the player, it is better to create a Blueprint based on TutoProjectCollectable. In this way, the Blueprint will inherit all the functionality that has already been created and we will be able to define another default Static Mesh for this new type of item. In this new Blueprint, we can also change the variables that define the game area.

Let's create a Blueprint with the name BPTutoProjectCollectable. In the Content Browser, access the TutoProject folder that is inside the C++ Classes folder. Right-click on the TutoProjectCollectable class and choose the option Create Blueprint class based on TutoProjectCollectable, as shown in the image below. 


On the next screen, write BPTutoProjectCollectable in the Name field. In the Path field, choose the Blueprints folder that is inside the ThirdPersonCPP folder and click the Create Blueprint Class button.


Double-click on the new Blueprint to open the Blueprint editor. Go to the Components tab and select StaticMesh as shown in the image. 


In the Details tab, you can change the Static Mesh and the Material that will be used by the instances of this Blueprint. For Static Mesh select Shape_NarrowCapsule and for Material select M_Tech_Hex_Tile_Pulse. Both are from the Starter Content. 


Now click on the Class Defaults button in the Blueprint editor. Look for the property categories Tutorial and Game Area as shown in the image below. In these categories are the variables that we created in the C++ class TutoProjectCollectable using UPROPERTY(). 


These and other variables were inherited from the parent class. You can modify the values of the variables in the Game Area category to define the area where instances of this Blueprint can be created. Game Area variables only appear in the Blueprints editor. They will not appear in the property windows of instances that are at the level. This is because we define these variables using the EditDefaultsOnly specifier in UPROPERTY().

Compile the Blueprint and add a few instances of this Blueprint at the level. Test the game and note that when the player collects the instances of this Blueprint he gains points in the same way when an instance of the C++ class TutoProjectCollectable is collected. It is because all the functionality of the C++ class TutoProjectCollectable was inherited by this Blueprint. This is a key principle of object-oriented programming.