官术网_书友最值得收藏!

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:
  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.

See also

主站蜘蛛池模板: 当雄县| 衡东县| 泸定县| 彩票| 扎兰屯市| 民丰县| 内江市| 铁力市| 苗栗市| 安乡县| 霍州市| 勐海县| 百色市| 巴彦县| 胶州市| 繁峙县| 郁南县| 河西区| 平原县| 昆明市| 浮梁县| 汉阴县| 馆陶县| 沁源县| 天台县| 铜鼓县| 宾阳县| 余干县| 桐城市| 板桥市| 湾仔区| 稷山县| 灵寿县| 绥中县| 陈巴尔虎旗| 江山市| 安远县| 临湘市| 武陟县| 宜丰县| 马公市|