
Accessing a UPROPERTY from Blueprints
Accessing a UPROPERTY
from Blueprints is fairly simple. The member must be exposed as a UPROPERTY
on the member variable that you want to access from your Blueprints diagram. You must qualify the UPROPERTY
in your macro declaration as being either BlueprintReadOnly
or BlueprintReadWrite
to specify whether you want the variable to be either readable (only) from Blueprints, or even writeable from Blueprints.
You can also use the special value BlueprintDefaultsOnly
to indicate that you only want the default value (before the game starts) to be editable from the Blueprints editor. BlueprintDefaultsOnly
indicates the data member cannot be edited from Blueprints at runtime.
How to do it...
- Create some
UObject
-derivative class, specifying bothBlueprintable
andBlueprintType
, such as the following:UCLASS( Blueprintable, BlueprintType ) class CHAPTER2_API UUserProfile : public UObject { GENERATED_BODY() public: UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = Stats) FString Name; };
The
BlueprintType
declaration in theUCLASS
macro is required to use theUCLASS
as a type within a Blueprints diagram. - Within the UE4 Editor, derive a Blueprint class from the C++ class, as shown in Creating a Blueprint from your custom UCLASS.
- Create an instance of your Blueprint-derived class in the UE4 Editor by dragging an instance from the Content Browser into the main game world area. It should appear as a round white sphere in the game world unless you've specified a model mesh for it.
- In a Blueprints diagram which allows function calls (such as the Level Blueprint, accessible via Blueprints | Open Level Blueprint), try printing the Name property of your Warrior instance, as seen in the following screenshot:
Tip
Navigating Blueprints diagrams is easy. Right-click and drag to pan a Blueprints diagram; Alt + Right-Click + Drag to zoom.
How it works…
UPROPERTY
are automatically written Get
/Set
methods for UE4 classes. They must not be declared as private
variables within the UCLASS
, however. If they are not declared as public
or protected
members, you will get a compiler error of the form:
>> BlueprintReadWrite should not be used on private members