TEnumAsByte saving and loading

Kind of a newbie question, but I’ve been working on this plug-in for saving and loading texture presets. I’ve gotten to a point where I can save out and load back in most of the values from a texture editor window, but I am having issues with the TEnumAsBytes.

I’m saving out the values like this:

TEnumAsByte< enum TextureCompressionSettings >  compressionSettings = texture->CompressionSettings;

Then convert it to an FString using GetValue()

"compressionSettings = " + compressionSettings.GetValue() + "\n"

I’m not sure if that’s working as intended, and then the second problem is loading it back in. Loading it in from a file, the value would be loaded into an FString. How do I convert that to a value I can use to set TEnumAsByte with?

wanted to ping this again, as I’m still not solving this problem. I can save out and load in all the other attributes of the texture class, but the ones that are TEnumAsByte. For some reason those are proving to be problematic. Any help would be appreciated.

From one noob to another, I can provide the method I used to get this done:

Common.h

    UENUM(BlueprintType)
    enum class Achievement : uint8
    {
        Achievement1        UMETA(DisplayName = "Achievement One"),
        Achievement2        UMETA(DisplayName = "Achievement Two"),
        Achievement3        UMETA(DisplayName = "Achievement Three"),
        Achievement4        UMETA(DisplayName = "Achievement Four")
    };


.h

UCLASS()
class MYAPP_API UMyScore : public UObject
{
    GENERATED_BODY()

private:

    FText levelName;

    UPROPERTY()
    TArray<Achievement> achievements;

public:

    friend FArchive& operator<<(FArchive& ar, UMyScore& data)
    {
        ar << data.levelName;

        if (ar.IsSaving())
        {
            int32 achievementCount = data.achievements.Num();
            ar << achievementCount;

            for (int32 x = 0; x < achievementCount; x++)
            {
                uint8 val = (uint8)data.achievements[x];
                ar << val;
            }
        }
        else
        {
            int32 achievementCount;
            ar << achievementCount;

            for (int32 x = 0; x < achievementCount; x++)
            {
                uint8 val;
                ar << val;
                data.achievements.Add((Achievement)val);
            }
        }

        return ar;
    }

Thanks for that, but I’m not seeing how that helps me. UTexture2D is a Unreal object class that has TEnumAsByte variables as part of its class, I need to figure out how to get that data and write back to it so I can use that class.

I did find this example -

https://forums.unrealengine.com/archive/index.php/t-33140.html

// Create the texture we're going to fill with data.
UTexture2D* Tex2D = UTexture2D::CreateTransient(Width, Height, PF_B8G8R8A8);

#endif // WITH_EDITORONLY_DATA
Tex2D->PlatformData->NumSlices = 1;
Tex2D->NeverStream = true;
Tex2D->Rename(*FileName);
Tex2D->CompressionSettings = TC_Default;

So setting it shouldn’t be difficult if I can replace TC_Default with an FString. Then I need to figure out why I’m getting nothing when I call

"compressionSettings = " + compressionSettings.GetValue() + "\n"

Reading and Writing the enum:

if (ar.IsSaving())
{
    uint8 val = (uint8)data.CompressionSettings;
    ar << val;
}
else
{
    uint8 val;
    ar << val;
    data.CompressionSettings = val;
}

That’s assuming CompressionSettings is an enum.

I can try that. Again, compressionSettings is a TEnumAsByte, but I’ll give it a go and let you know what happens.

I thought I had answered this, but you were half right . I got the value correctly using your method

uint8 mipGenSettings = (uint8)texture->MipGenSettings;

But in order to use that info to set the TEnumAsByte, I had to use the value to create a new enum. The set function required another enum.

TEnumAsByte< enum TextureMipGenSettings > temp(FCString::Atoi(*value));
texture->MipGenSettings = temp;