Unreal Engine 4.x Scripting with C++ Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

  1. Create some UObject-derivative class, specifying both Blueprintable and BlueprintType, such as in the following code, using the same class we created previously:
/**
* UCLASS macro options sets this C++ class to be
* Blueprintable within the UE4 Editor
*/
UCLASS(Blueprintable, BlueprintType)
class CHAPTER_02_API UUserProfile : public UObject
{
GENERATED_BODY()

public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
float Armor;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
float HpMax;

UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
FString Name;
};

The BlueprintType declaration in the UCLASS macro is required to use the UCLASS as a type within a blueprints diagram.

  1. Save and compile your code.
  2. Within the UE4 editor, derive a blueprint class from the C++ class if needed, as shown in the previous recipe or in the Creating a Blueprint from your custom UCLASS recipe.
  3. Double-click on your instance and change the Name variable to have a new value, for instance, Billy. Afterwards, hit the Compile button to save all of your changes:
  1. In a blueprints diagram that allows function calls (such as the Level Blueprint, which is accessible via Blueprints | Open Level Blueprint), we can now try to make use of the variable we added. Perhaps we can try printing the Name property whenever the game starts.
  1. To have something happen at the start of the game, we will need to create a BeginPlay event. You can do this by right-clicking in the blueprint graph and selecting Add Event | Event BeginPlay:

Now, need to create an instance of the class. Since it's derived from UObject, we cannot instantiate it from drag and drop, but we can create something through the Construct Object from Class Blueprint node.

  1. Right-click to the right of the node that was just created and from the search bar, type in construct and select the Construct Object from Class node from the list:
  1. Next, connect the line from the right of the Event BeginPlay node to the left of the Construct Node by dragging the arrow on the bottom-right of the Event BeginPlay node to the arrow on the left-hand side of the Construct Node and releasing it.
Navigating blueprints diagrams is easy. Right-click and drag to pan a blueprints diagram,  Alt  + right-click + drag, or use the mouse wheel to zoom. You can left-click and drag any node to position it wherever you want. You can also select multiple nodes at the same time and move them all together. You can find more information on blueprints here: https://docs.unrealengine.com/en-US/Engine/Blueprints/BestPractices.
  1. Under the Class section, click on the dropdown and type in the name of the blueprint you created (MyProfile) and select it from the list.
  1. You also need to select something for the Outer property that will be the owner of the object. Click and drag the blue circle and move the mouse to the left of the node, and then let go of the mouse to create a new node. When the menu pops up, type in the word self and then select the Get a reference to self option. If all went well, your blueprint should look something like this: 

This will create a variable using the information from the MyProfile instance we created earlier. However, we have no way to use it yet unless we make it a variable. Drag and drop this to the right of the Return Value property and select Promote to a variable. This will automatically create a variable called NewVar_0 and create a Set node, but you can rename it to whatever you want using the menu on the left-hand side of the menu. 

  1. To the right of the SET node, drag and drop the white arrow on the top right of the node and create a Print Text node.
  2. We now need something to print, and the Name property will work perfectly for this. To the right of the SET node, drag and drop the blue node and select the Variables | Stats | Get Name node.
  3. Finally, connect the Name value to the In Text property of the Print Text node. It will automatically create a conversion node to change the name string into a Text object that it can understand.

In the end, the entire blueprint should look something like this:

The completed blueprint

If all went well, you should be able to hit the Compile button and then play the game by hitting the Play button on the top of the menu:

Upon playing, you should see Billy show up on the screen, just as we set previously!