Use of UE C++ Struct

[UE C++] Struct

C++ style Struct

Like other C++ ordinary structures:

struct TestStruct
{
	FName StructName = FName("TestStruct");

	AActor* TargetActor;
};

Note: It is not recommended to use UE's Object such as AActor for this structure. The reason is that this Struct is not visible to the UE's garbage collection system. Using this Struct may cause null pointer reference problems.

U++ style Struct

Need to USTRUCT()add GENERATED_USTRUCT_BODY():

USTRUCT()
struct FTestStruct
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY()
	FName StructName = FName("TestStruct");

	UPROPERTY()
	AActor* TargetActor;
};

Precautions:

  • UE will not automatically perform memory management on UStruct
  • You can use to UPROPERTYmark the member variables of a structure to make it visible to UE's reflection system and blueprint scripts.
  • The structure must start with F , TestStruct -> FTestStruct
  • GENERATED_USTRUCT_BODY()You can use GENERATED_BODY()instead
  • The UStruct created this way is not visible to the blueprint

Blueprint visible Struct

You need to add BlueprintTypethe keyword and add corresponding UPROPERTYattributes to the visible structure member variables:

USTRUCT(BlueprintType)
struct FTestStruct
{
	GENERATED_USTRUCT_BODY()

    //蓝图图表可以访问
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Test Struct")
	FName StructName = FName("TestStruct");

    //蓝图图表无法访问
	UPROPERTY()
	AActor* TargetActor;
};

Precautions:

  • The Make function is displayed for any UStruct with a BlueprintType tag
  • If there is at least one BlueprintReadOnlyor BlueprintReadWriteattribute in UStruct, Break will be displayed
  • Pure nodes created by Break provide an output pin for each property marked with BlueprintReadOnlyorBlueprintReadWrite

    Make&Break Struct
  • EditAnywhereThe function is to specify default values ​​for Struct member variables in the editor details panel interface.

    EditAnywhere

Data Table

None of the above Structs can be applied to Data Table. If you want to use Data Table, Struct needs to be inherited FTableRowBaseas the parent class:

USTRUCT(BlueprintType)
struct FTestStruct : public FTableRowBase
{
	GENERATED_USTRUCT_BODY()

    //Data Table 可以编辑
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Test Struct")
	FName StructName = FName("TestStruct");

    //Data Table 无法编辑
	UPROPERTY()
	AActor* TargetActor;
};

Create Data Table
Things to note :

  • Need to introduce header files#include"Engine/Datatable.h"
  • Variables not marked with EditAnywhereor EditDefaultsOnlywill be displayed in the Data Table, but cannot be edited.

    Target Actor cannot be edited

Other details

  • Ustructs can use UE's smart pointers and garbage collection system to prevent Uobjects from being deleted by garbage collection.
  • Structs differ from Uobjects in that they work best with simple data types. For more complex interactions in your project, you may want to create UObject or AActor subclasses.
  • Do not consider using network replication for UStructs. You can consider UProperty variables when replicating over the network (the UPROPERTY properties in USTRUCT are all network replicated by default)
  • Functions created in UStructs can only be used in C++ and cannot be used in blueprints (you can consider using BlueprintFunctionLibrary instead, BPLibrary implementation method )

Reference links:

Official documentation

Finally, the author is also a novice. He writes articles mainly to record the learning process. The cover comes from the Internet, and any infringement will be deleted.

Guess you like

Origin blog.csdn.net/qq_52179126/article/details/129768456