Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed enum to wrong string cause by misuse GetNameStringByValue and GetNameStringByIndex #4817

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from

Conversation

julee
Copy link

@julee julee commented Oct 29, 2021

This change is Reviewable

@julee julee requested a review from a team as a code owner October 29, 2021 02:55
@update-docs
Copy link

update-docs bot commented Oct 29, 2021

Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would update our CHANGELOG.md based on your changes.

@bernatx
Copy link
Contributor

bernatx commented Oct 29, 2021

Hi Julee,
Thanks for the revision, but can you explain what you are solving and what was the problem?
I think the way you are modifying this index by value is not right. Can you explain more? The enums are assigned a value from 0 to N, and that is the index they represent in the enum. Why using an arbitrary value? Also, in that case, we need to cast the value to 64 bits, because they are not 32bits value like the index is.

Regards

@julee
Copy link
Author

julee commented Oct 30, 2021

Hi Julee, Thanks for the revision, but can you explain what you are solving and what was the problem? I think the way you are modifying this index by value is not right. Can you explain more? The enums are assigned a value from 0 to N, and that is the index they represent in the enum. Why using an arbitrary value? Also, in that case, we need to cast the value to 64 bits, because they are not 32bits value like the index is.

Regards

No, you can't assume the enum index is the enum value, because enum value can assign to any value!

Check the QualityLevelUE.h:

UENUM(BlueprintType)
enum class EQualityLevel : uint8
{
  Null = 0, // Workarround for UE4.24 issue with enums
  Low    = CARLA_ENUM_FROM_RPC(Low)    UMETA(DisplayName = "Low"),
  // Medium = CARLA_ENUM_FROM_RPC(Medium) UMETA(DisplayName = "Medium"),
  // High   = CARLA_ENUM_FROM_RPC(High)   UMETA(DisplayName = "High"),
  Epic   = CARLA_ENUM_FROM_RPC(Epic)   UMETA(DisplayName = "Epic"),

  SIZE      UMETA(Hidden),
  INVALID   UMETA(Hidden)
};

We can see enum index is not the with enum value ( EQualityLevel::Low index is 1, but the enum value is 0).
And we can't assume assigning enum value is a special case, it's very common.

Unreal Engine documentation also pointed out the issue:

Index is the internal index into the Enum array, and is not useful outside of the Enum system Value is the value set in the Enum Class in C++ or Blueprint Enums can be sparse, which means that not every valid Index is a proper Value, and they are not necessarily equal It is not safe to cast an Index to a Enum Class, always do that with a Value instead Gets the internal index for an enum value.

You say:

Also, in that case, we need to cast the value to 64 bits

This is just according whether the enum is defined to 64 bits int or not, if not, cast to int32_t doesn't matter. Unreal give the parameter with int64_t is because the enum can be defined to 64 bits int, it must support all enum definitions.

@stale
Copy link

stale bot commented Jan 9, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Issue has not had recent activity label Jan 9, 2022
@julee
Copy link
Author

julee commented Jan 9, 2022

This bug should be fixed

@stale stale bot removed the stale Issue has not had recent activity label Jan 9, 2022
@stale
Copy link

stale bot commented Jun 1, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Issue has not had recent activity label Jun 1, 2023
@stale stale bot removed the stale Issue has not had recent activity label Nov 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants