Pages

quinta-feira, 3 de setembro de 2020

TutoProjectCollectable class: UPROPERTY()

There is a macro in Unreal Engine with the name UPROPERTY() that is used to expose variables to the editor.

Let's create two variables in the TutoProjectCollectable class just to see the use of UPROPERTY() in practice.

Open the file TutoProjectCollectable.h and put the definition of the UEVersion and SpecialId variables below the component variables as shown in this code: 

...

public:	
	// Sets default values for this actor's properties
	ATutoProjectCollectable();

	// Called every frame
	virtual void Tick(float DeltaTime) override;
    
   	UPROPERTY(VisibleAnywhere)
	USceneComponent* RootScene;

	UPROPERTY(VisibleAnywhere)
	UStaticMeshComponent* StaticMesh;
    
	UPROPERTY(VisibleAnywhere, Category = Tutorial)
	float UEVersion;

	UPROPERTY(EditAnywhere, Category = Tutorial)
	int32 SpecialId;

...

An UPROPERTY macro can receive specifiers and is related to the definition of a variable below it. Examples of specifiers are VisibleAnywhere, which indicates that the property cannot be edited but is visible in all property windows, and EditAnywhere, which indicates that the property can be edited in all property windows.

Another UPROPERTY specifier is Category, which specifies in which category the property will be displayed in the properties window.

Since the UEVersion variable cannot be edited due to the use of VisibleAnywhere, we will assign a value to it in the constructor that is in the file TutoProjectCollectable.cpp:

...

// Sets default values
ATutoProjectCollectable::ATutoProjectCollectable()
{

	UEVersion = 4.25f;

...

Let's see the use of UPROPERTY. Compile the project and add an instance of TutoProjectCollectable to the level. Select the instance and see the UPROPERTY variables in the Details tab, Tutorial category. The UEVersion variable is read-only and the SpecialId variable can be edited as shown in this image:


A note regarding component pointers. They are defined as UPROPERTY with the VisibleAnywhere specifier. This means that the pointer cannot be changed in the editor, but the properties of the component can be modified in the editor. There is no need to define them as EditAnywhere.

We will create variables to store the boundaries of the game area. They will be used when creating new instances of TutoProjectCollectable while the game is running.

Put the definition of the new variables in the TutoProjectCollectable.h file after the UEVersion and SpecialId variables, as shown in the code below:

...

	UPROPERTY(VisibleAnywhere, Category = Tutorial)
	float UEVersion;

	UPROPERTY(EditAnywhere, Category = Tutorial)
	int32 SpecialId;

	UPROPERTY(EditDefaultsOnly, Category = "Game Area")
	float XMinimum;
		
	UPROPERTY(EditDefaultsOnly, Category = "Game Area")
	float XMaximum;

	UPROPERTY(EditDefaultsOnly, Category = "Game Area")
	float YMinimum;

	UPROPERTY(EditDefaultsOnly, Category = "Game Area")
	float YMaximum;

	UPROPERTY(EditDefaultsOnly, Category = "Game Area")
	float FloorZValue;

...

The variables of the game area are defined as UPROPERTY with the EditDefaultsOnly specifier. This means that these variables can be edited in the Blueprints editor if you create a Blueprints using TutoProjectCollectable as the parent class, but they will not appear in the instance property windows.

There is another UPROPERTY specifier called EditInstanceOnly. In this case, the variable appears in the property window of an instance, but it does not appear in the Blueprints editor.

The variables are grouped in a category called "Game Area". The use of quotation marks is necessary because of the blank space that exists in the category name.

The values of the variables of the game area are assigned in the file TutoProjectCollectable.cpp, inside the constructor:

...

// Sets default values
ATutoProjectCollectable::ATutoProjectCollectable()
{

	UEVersion = 4.25f;

	//Default Values for Game Area
	XMinimum	= -1800.0f;
	XMaximum	=  1000.0f;
	YMinimum	= -1400.0f; 
	YMaximum	=  1400.0f;
	FloorZValue	=   130.0f;

...


Table of Contents C++