Blog / September 7, 2023 / 3 mins read / By Suneet Agrawal

UPROPERTY in Unreal

Unreal Engine, renowned for its capabilities in creating visually stunning and highly interactive games and simulations, relies on several core features to achieve its potential. Among these, Unreal Property Specifiers, or UPROPERTY, stand out as a powerful tool for developers.

What is UPROPERTY

UPROPERTY is a macro in Unreal Engine that allows developers to define various attributes and behaviors for properties within classes. In this blog post, we’ll delve deep into UPROPERTY and explore not only its garbage collection aspects but also its key attributes, serialization, saving game state, editor integration, reflection, networking, replication, and more.

Garbage Collection with UPROPERTY

Unreal Engine’s memory management is pivotal for both performance and stability. UPROPERTY plays a vital role in garbage collection, ensuring that objects are properly managed and deallocated when they are no longer needed. Here’s an example of using UPROPERTY for garbage collection:

UPROPERTY(Transient)
TArray<AActor*> MyActors; // This array will not be included in garbage collection

By marking a property as Transient, Unreal Engine knows not to include it in the garbage collection process, which can be useful for temporary or non-persistent data.

Key Attributes of UPROPERTY

UPROPERTY offers a comprehensive range of key attributes, each designed to fine-tune the behavior of properties within Unreal Engine classes:

  • EditAnywhere: Allows the property to be edited in the Unreal Editor.
  • BlueprintReadWrite: Enables read and write access in Blueprints.
  • Replicated: Indicates that the property should be replicated across networked instances.
  • SaveGame: Specifies that the property should be saved and loaded when saving game state.
  • VisibleAnywhere: Makes the property visible but not editable in the Unreal Editor.
  • Category: Organizes properties within the Unreal Editor’s property window.
  • Config: Allows properties to be loaded from configuration files.

Here’s an example demonstrating a combination of these attributes:

UPROPERTY(EditAnywhere, BlueprintReadWrite, Replicated, SaveGame, VisibleAnywhere, Category = "Player Data", Config)
int32 PlayerScore;

Serialization and Saving Game State

UPROPERTY plays a pivotal role in serialization, ensuring that game state can be saved and loaded seamlessly. When combined with the SaveGame attribute, properties can be automatically included in save files:

UCLASS()
class UMyGameSave : public USaveGame
{
    GENERATED_BODY()

public:
    UPROPERTY(VisibleAnywhere)
    int32 PlayerScore;
};

Editor Integration

Unreal Engine’s Editor Integration is one of its strengths. UPROPERTY can be used to expose variables and properties to the Unreal Editor for designers and artists to work with:

UPROPERTY(EditAnywhere)
UTexture2D* MyTexture;

This allows for easy tweaking and customization within the editor.

Reflection

Reflection in Unreal Engine is the ability to inspect and manipulate an object’s properties at runtime. UPROPERTY helps enable reflection by providing metadata that Unreal Engine can use to introspect an object’s structure:

UObject* MyObject = GetSomeObject();
UProperty* MyProperty = FindField<UProperty>(MyObject->GetClass(), TEXT("MyProperty"));

This allows you to access and modify properties dynamically.

Networking and Replication

Multiplayer games rely on networking and replication to ensure that game state is synchronized across clients. UPROPERTY’s Replicated attribute helps facilitate this process:

UPROPERTY(Replicated)
FVector PlayerLocation;

By marking a property as Replicated, Unreal Engine automatically handles network synchronization.

Conclusion

UPROPERTY stands as a fundamental and versatile concept in Unreal Engine, allowing developers to control memory management, expose properties to the editor, manage game state, and support networking and replication. Understanding the rich variety of attributes available for UPROPERTY is essential for crafting efficient and engaging Unreal Engine projects. As you dive deeper into Unreal Engine development, you’ll discover UPROPERTY as an invaluable tool for creating dynamic and immersive experiences.

Comments