【UE4 C++ 基础知识】<4> 枚举 Enum、结构体 Struct
阅读原文时间:2023年07月09日阅读:1

枚举

  • UENUM宏搭配BlueprintType可以将枚举暴露给蓝图,不使用的话,仅能在C++使用

    //定义一个原生enum class
    enum class EMyType
    {
    Type1,
    Type2,
    Type3,
    };

    UENUM(BlueprintType)
    enum class ECurrentState : uint8
    {
    Idle UMETA(DisplayName="空闲"),
    Attack UMETA(DisplayName="攻击"),
    Roll UMETA(DisplayName="翻滚"),
    Dead UMETA(DisplayName="死亡"),
    };

    UPROPERTY(EditAnywhere,BlueprintReadWrite)
    ECurrentState MyCurrentState UMETA(DisplayName = "当前状态");

结构体

  • BlueprintType可以将枚举暴露给蓝图

    //结构体
    USTRUCT(BlueprintType)
    struct FMyStruct
    {
    GENERATED_BODY()

    // 暴露给蓝图
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Test Variables")
    int32 MyIntegerMemberVariable;
    
    // 不暴露给蓝图
    int32 NativeOnlyMemberVariable;
    
    // 蓝图图表无法访问此UObject指针,但是指针对UE4的反射、智能指针和垃圾回收系统可见。
    UPROPERTY()
    UObject* SafeObjectPointer;

    };

    UPROPERTY(EditAnywhere,BlueprintReadWrite)
    FMyStruct MyStruct;

  • 继承FTableRowBase之后可以用于DataTable

    struct FInventoryItemInfo : public FTableRowBase
    {
    GENERATED_BODY()

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FString ItemName UMETA(DisplayName="名称");
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        int32 index UMETA(DisplayName="编号");
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        bool bCanStaking UMETA(DisplayName="可否叠加");
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        int32 Count UMETA(DisplayName="数量");
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        UTexture2D* Icon UMETA(DisplayName="图标");
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TSubclassOf<AActor> ItemClass;
    
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        USkeletalMesh* SkeletalMesh;

    };

    UPROPERTY(EditAnywhere,BlueprintReadWrite)
    FInventoryItemInfo EmptyItem;

参考

结构体

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章