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"

Is it possible to check the length of an enum?

Suppose I have some arbitrary enum, and I'm trying to generate a random value from it. It's trivial to accomplish this with a static cast and a hardcoded random int:

   //.h
   UENUM(BlueprintType)
  namespace EColor 
  {
      enum Type
      {
           Red,
           Green,
           Blue,
      };
  }
  
  //.cpp
      EColor::Type myColor = static_cast<EColor::Type>( FMath::RandRange(0,2) );

However, this requires me to keep excruciating track of every place I use this logic in, and manually change the range I feed FMath whenever I modify the enum. Is there any way I can manually check the exact number of possible values an enum has, instead of hard-coding it every time?

Product Version: UE 4.10
Tags:
more ▼

asked Nov 28 '15 at 12:11 PM in C++ Programming

avatar image

HInoue
251 97 67 83

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

2 answers: sort voted first

Why not create a static function as such:

 const int32 Color_Count = 3;
 static EColor::Type GetRandColor()
 {
     return static_cast<EColor::Type>(FMath::RandRange(0, Color_Count - 1));
 }

And that's all you have to call when you need a random EColor::Type. When ever you add/remove values to/from namespace EColor, then you only need to update Color_Count.

more ▼

answered Nov 30 '15 at 08:43 PM

avatar image

MJ_01
822 38 39 241

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

Not that I know of. There is another way to do that though, kinda hacky but works

http://stackoverflow.com/a/2102615

more ▼

answered Nov 28 '15 at 12:30 PM

avatar image

AlekseyIlin
304 16 18 29

avatar image HInoue Nov 28 '15 at 12:41 PM

I'm kind of surprised by this- I know UE1-3 had something called EnumCount, but intellisense doesn't recognize this in 4 and I can't find a reference to it in the API.

(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