x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Safe to cast uint8 to enum and vice versa?

`UENUM()
enum class ETileType : uint8
{
ENone, ESurface, ESubsurface, EPlatform, EHole, EEnd
};

// get a random tile

uint8 n = FMath::RandRange(1, (uint8)ETileType::EEnd - 1);
ETileType NewTile = (ETileType)n;

Is this safe? Is there a better way?
edit sorry, i cannot get this post to format correctly.

Product Version: UE 4.10
Tags:
more ▼

asked Feb 14 '16 at 09:21 PM in C++ Programming

avatar image

Curious_Rubick
6 1 4 4

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

I would say this is ok assuming you don't want to get the ENone result (because enums start from 0, not 1).

Your enum is backed by a uint8 here, so no problems with too big numbers. You use EEnd as the max range which is good because you can safely add new enumeration.

Maybe I would use static_cast here, because it's C++ after all and later on you can easily search for casts in your project. Also maybe explicitly state what's the start of the range. Something like that:

 uint8 n = FMath::RandRange(static_cast<int>(ETileType::ESurface), static_cast<int>(ETileType::EEnd) - 1);
 ETileType NewTile = static_cast<ETileType>(n);

But this is a more-or-less cosmetic change, so you decide :)

more ▼

answered Feb 15 '16 at 08:33 AM

avatar image

fszczemton
291 16 19 248

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question