Creating a user-editable UPROPERTY

Each UCLASS that you declare can have any number of UPROPERTY declared for it within it. Each UPROPERTY can be a visually editable field, or some Blueprints accessible data member of the UCLASS.

There are a number of qualifiers that we can add to each UPROPERTY, which change the way it behaves from within the UE4 Editor, such as EditAnywhere (screens from which the UPROPERTY can be changed), and BlueprintReadWrite (specifying that Blueprints can both read and write the variable at any time in addition to the C++ code being allowed to do so).

Getting ready

To use this recipe, you should have a C++ project into which you can add C++ code. In addition, you should have completed the preceding recipe, Making a UCLASS – deriving from UObject.

How to do it...

  1. Add members to your UCLASS declaration as follows:
    UCLASS( Blueprintable )
    class CHAPTER2_API UUserProfile : public UObject
    {
      GENERATED_BODY()
      public:
      UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
      float Armor;
      UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats)
      float HpMax;
    };
  2. Create a Blueprint of your UObject class derivative, and open the Blueprint in the UE4 editor by double-clicking it from the object browser.
  3. You can now specify values in Blueprints for the default values of these new UPROPERTY fields:
    How to do it...
  4. Specify per-instance values by dragging and dropping a few instances of the Blueprint class into your level, and editing the values on the object placed (by double-clicking on them).

How it works…

The parameters passed to the UPROPERTY() macro specify a couple of important pieces of information regarding the variable. In the preceding example, we specified the following:

  • EditAnywhere: This means that the UPROPERTY() macro can be edited either directly from the Blueprint, or on each instance of the UClass object as placed in the game level. Contrast this with the following:
    • EditDefaultsOnly: The Blueprint's value is editable, but it is not editable on a per-instance basis
    • EditInstanceOnly: This would allow editing of the UPROPERTY() macro in the game-level instances of the UClass object, and not on the base blueprint itself
  • BlueprintReadWrite: This indicates that the property is both readable and writeable from Blueprints diagrams. UPROPERTY() with BlueprintReadWrite must be public members, otherwise compilation will fail. Contrast this with the following:
    • BlueprintReadOnly: The property must be set from C++ and cannot be changed from Blueprints
  • Category: You should always specify a Category for your UPROPERTY(). The Category determines which submenu the UPROPERTY() will appear under in the property editor. All UPROPERTY() specified under Category=Stats will appear in the same Stats area in the Blueprints editor.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.27.72