
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...
- 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; };
- Create a Blueprint of your
UObject
class derivative, and open the Blueprint in the UE4 editor by double-clicking it from the object browser. - You can now specify values in Blueprints for the default values of these new
UPROPERTY
fields: - 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 theUPROPERTY()
macro can be edited either directly from the Blueprint, or on each instance of theUClass
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 basisEditInstanceOnly
: This would allow editing of theUPROPERTY()
macro in the game-level instances of theUClass
object, and not on the base blueprint itself
BlueprintReadWrite
: This indicates that the property is both readable and writeable from Blueprints diagrams.UPROPERTY()
withBlueprintReadWrite
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 aCategory
for yourUPROPERTY()
. TheCategory
determines which submenu theUPROPERTY()
will appear under in the property editor. AllUPROPERTY()
specified underCategory=Stats
will appear in the sameStats
area in the Blueprints editor.
See also
- A complete
UPROPERTY
listing is located at https://docs.unrealengine.com/latest/INT/Programming/UnrealArchitecture/Reference/Properties/Specifiers/index.html. Give it a browse.