【UE4 C++ 基础知识】<4> 枚举 Enum、结构体 Struct

枚举

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

    //定义一个原生enum class
    enum class EMyType 
    {
    	Type1,
    	Type2,
    	Type3,
    };
    
    namespace EState 
    {
    	enum Type {
    		Stop,
    		Close,
    		Open
    	};
    }
    
    // 带反射枚举
    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 = "当前状态");
    

    image

  • 枚举值与字符串转换, 针对 UENUM

    • .h

      UENUM()
      enum class ECultureTeam :uint8 {
      	EN = 0,
      	ZH = 1
      };
      //根据 enum 类型获取字符串
      template<typename TEnum>
      FString GetEnumValueAsString(const FString& Name, TEnum value);
      
      //根据字符串 获取 enum 值
      template<typename TEnum>
      TEnum GetEnumValueFromString(const FString& Name, FString value);
      
    • .cpp

      template<typename TEnum>
      FString DataHandle::GetEnumValueAsString(const FString& Name, TEnum value)
      {
      	const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, *Name, true);
      	if (!EnumPtr)
      		return FString("InValid");
      	return EnumPtr->GetNameStringByIndex((int32)value);
      }
      
      template<typename TEnum>
      TEnum DataHandle::GetEnumValueFromString(const FString& Name, FString value)
      {
      	const UEnum* EnumPtr = FindObject<UEnum>(ANY_PACKAGE, *Name, true);
      	if (!EnumPtr)
      		return TEnum(0);
      	return (TEnum)EnumPtr->GetIndexByNameString(value);
      }
      
    • 调用

      GetEnumValueAsString<ECultureTeam>(FString("ECultureTeam"), CurrentCulture);
      GetEnumValueFromString<ECultureTeam>(FString("ECultureTeam"), FString("EN"));
      
      

结构体

  • 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;
    

    image

  • 继承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;
    

    image

参考

结构体

posted @ 2021-04-22 21:07  砥才人  阅读(2588)  评论(0编辑  收藏  举报