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

How to do it...

  1. First, we will need to mark the class as Blueprintable and then add the following members to your UCLASS declaration, which are shown in bold:
/**
* UCLASS macro options sets this C++ class to be
* Blueprintable within the UE4 Editor
*/
UCLASS( Blueprintable )
class CHAPTER_02_API UUserProfile : public UObject
{
GENERATED_BODY()

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

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

  1. Return to Unreal Editor and then hit the Compile button to update our code.
  2. Once updated, create a blueprint of your UObject class derivative, if it hasn't been created already.

This can be done in the same way that we saw in the previous recipe, but it is also possible to do by hand, which we will do now.

  1. To do this, go to the Content Browser tab and click on the folder icon to select what section of the project you want to work in. From the window that pops up, select the Content section:
Selecting the Content folder in the Content Browser
  1. From there, select the Add New button and then select Blueprint Class:
Creating a Blueprint Class from the Content Browser
  1. From the Pick Parent Class menu, you'll see some buttons for Common Classes. Below that, you'll see the All Classes option with an arrow to click on to expand it. From there, type in the name of your class (in our case, UserProfile) and then select it from the list. Afterwards, click on the Select button: 
  1. From there, you'll see the item appear in the Content Browser, where you can rename the instance to whatever you'd like; I named mine MyProfile.
  2. Once created, we can open the blueprint in the UE4 Editor by double-clicking it.
  1. You can now specify values in blueprints for the default values of these new UPROPERTY fields:
Since the blueprint is empty, it may open as a data-only blueprint that does not include the middle and left-hand side sections. To see the full blueprint menu, you may need to click Open Full Blueprint Editor at the top of the menu to make the screen look like the one in the previous screenshot. However, the variables should still be visible and modifiable, either way.
  1. Specify the per-instance values by creating new instances of the blueprint and editing the values on the object that's placed (by double-clicking on them).