Announcement

Collapse
No announcement yet.

Trying to get UEnum pointer to cast as byte

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • replied
    In 4.24 and above:

    Code:
    const FString Name = StaticEnum<EMyEnum>()->GetNameStringByValue(Value);
    StaticEnum() contains lots of conversion methods like this. It should go without saying, none of this works in non-editor builds where the name information/meta is stripped out.

    Leave a comment:


  • replied
    How to display enum value as string correctly ?
    Im getting this as a result :

    MyEnum::MyValue

    I want it to be like this :

    MyValue


    Here is my code :

    HTML Code:
    template<typename Enumeration>
    static FORCEINLINE FString GetEnumerationToString(Enumeration InValue)
    {
         return UEnum::GetValueAsString(InValue);
    }
    Last edited by xNezaki; 04-01-2020, 01:54 AM.

    Leave a comment:


  • replied
    There's no support for being able to select any arbitrary enum like that. The best alternative is to have a "uint8" entry - but to be perfectly honest this doesn't really look like a valid use-case for an enum in the first place.

    Leave a comment:


  • replied
    Hmm I'm not entirely sure what you mean by choosing between enums, but there should be a few ways to do that?

    You could store a list of byte values in an array like this:

    Code:
     UPROPERTY(Category = DataAssetValues, VisibleAnywhere, BlueprintReadWrite)
                TArray<uint8> EnumListValuesAsBytes;
    You could then have a property that indicates what kind of enum it is : (I was wrong, ENum* can be useful)

    Code:
     UPROPERTY(Category = DataAssetValues, VisibleAnywhere, BlueprintReadWrite)
                ENum* CurrentEnumType;
    However I haven't found a way to get the class representation of an enum yet, so what I would do is use template to do so :

    Code:
     template <typename T>
            void RegisterEnum(T);
    I'm just throwing things here, hopefully this will give you some ideas =)

    EDIT: Just found out that this works to get the class representation of an enum:

    Code:
        TSoftClassPtr<ERandomEnum> EnumSoftClassPointer;
        UClass* CurrentEnum;
        CurrentEnum = EnumSoftClassPointer->GetClass();
    or even this if you want ENum* :

    Code:
        TSoftClassPtr<ERandomEnum> EnumSoftClassPointer;
        UEnum* CurrentEnum;
        CurrentEnum = Cast<UEnum>(EnumSoftClassPointer->GetClass());
    Last edited by Dakryk; 02-25-2020, 06:13 PM.

    Leave a comment:


  • replied
    Dakryk This is not exactly what i'm trying to do. Essentially i would like to choose between enums in a data asset and would want to have them Get as a byte so i would be able to manipulate switches and selects. Would you make a library of all the enums in c++, and then uproperty of an array of created ones? Any thoughts of how you would make this or would i have to use an array of strings?

    Leave a comment:


  • replied
    This is very wrong, UEnum* won't work.

    This is how I declare my enum classes in UE4.24 :

    Code:
    UENUM()
    enum class EInputTypeDelegate : uint8
    {
        Forward,
        Sideways,
        MiddleClickAxis,
        LeftClickPress,
        LeftClickRelease,
        RightClickPress,
        RightClickRelease,
        MidClickPress,
        MidClickRelease,
        ActionBtn1, // F - Possess
        ActionBtn2, // R - Draft
        ActionBtn3, // ?
        ActionBtn4, // ?
        ActionBtn5, // ?
        JumpKey, // Space
        ShiftKey // Shift
    };
    Then if I want to use it as a property (I do not in that case but for some other enums, I do):

    Code:
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = InputSystem)
    EInputTypeDelegate LastInputTypeDelegate;
    And if I want it as a byte :

    Code:
    UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = InputSystem)
    TEnumAsByte<EInputTypeDelegate> LastInputTypeDelegateAsByte;
    If you want to see the enum in the editor, don't forget to add the meta property BlueprintType, like this:

    Code:
    UENUM(BlueprintType)
    enum class EMovementDirection : uint8
    {
        Forward,
        Right,
        Left,
        Backward
    };
    Another way to use TEnumAsByte inside a function :

    Code:
    TraceParametersPackage.TraceChannel = TEnumAsByte<ETraceTypeQuery>(ETraceTypeQuery::TraceTypeQuery1);
    I hope this help :-) There is more to say as I used the simplest type of enum definition here but it should help you get started. I would recommand you to try to learn how to use Templating, it's very powerful in combination with enums!
    Last edited by Dakryk; 02-24-2020, 01:42 PM.

    Leave a comment:


  • replied
    Apparently I need to make the pointer into a typename. Still unsure on how to do that.

    Leave a comment:


  • started a topic Trying to get UEnum pointer to cast as byte

    Trying to get UEnum pointer to cast as byte

    I am trying to pick an already existing blueprint enumeration in a data asset but display it as a byte after i have picked it.
    I am unsure how to make the Levels pointer be an enum for TEnumAsByte.

    I am new the C++ programming in UE4.
    Any suggestions on how to make this work?
Working...
X