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

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

主站蜘蛛池模板: 合江县| 安吉县| 嘉黎县| 昭通市| 仙游县| 吉木乃县| 泊头市| 安徽省| 漳州市| 塔城市| 化德县| 东乡县| 天台县| 平舆县| 阿克陶县| 沙田区| 沙雅县| 兴仁县| 黄大仙区| 和政县| 金昌市| 麻栗坡县| 那曲县| 罗甸县| 盐津县| 环江| 西安市| 柳林县| 宜良县| 黄石市| 五台县| 贵定县| 天长市| 邯郸县| 上林县| 新昌县| 迁安市| 嘉禾县| 横山县| 海宁市| 治县。|