Is there any way to subtype enums?

For example, if we have something like

Location :=enum{PizzaHut, Arcade, Apartment, House}

Is there any way to do something like

Business := enum{Location.PizzaHut, Location.Arcade}

where we can guarantee Business is a subtype of Location?

an enum is an integral type defined in C as a re-representation of int (Unreal by extension tries to restrict this to uint8, though there is engine support for int32) that resolves back to an integer, so that the programmer can use effectively a string literal as though it was an int for things like switch-case or “simple” if statements without having magic number integer literals floating in if statements and switch-case blocks and doing actual string compares.

in theory in C++ the answer would be “maybe” (any type can be inherited with enough effort), but for UEF the direct answer would be: No, not in the tools given to you.

that being said what you are doing is similar to a technique that is often used. where an enumeration is used to determine some kind of secondary check.

for example if I have a class AEnemy that could be an: AGrunt, ASoldier, or ABrawler instead of trying to cast to each one I could just have the AEnemy hold an enum for EnemyType then depending on the value of that EnemyType I could cast to the correct type to get the appropriate members and functions.

though I would think that a location would have a business at it, not a location is of type business. so if the Location was the physical object/class that holds your enum of business (I would suggest maybe adding a NONE just in case) then that would probably be cleaner for this example. then maybe the Business is a separate Component, or member of the Location (because a Location would “has-a” business at it)

I don’t believe you can, but you could map those enums if you want to group them, something like

LocationType := enum:
    Business
    SomethingElse

Locations : [LocationType][]Location = map:
    LocationType.Business => array{Location.PizzaHut, Location.Arcade}
    LocationType.SomethingElse => array{Location.SomethingElse}

Btw, enums don’t take a capitalized first letter, they respect lower_snake_case (see Verse Code Style Guide)

You could make a class 'game_location’ with a string field ‘Name’ and an enum ‘Type’ field (which would include ‘business’) . game_location could have a function ‘bIsBusiness( ): logic’

So all of your locations would be stored in an array of game_locations and any particular location could be tested to determine if it is a business. The class would be expandable if you wanted to include things like the vector3 location of the game_location.

In other languages I’d use a structure, but in my limited experience Verse seems to play better with classes than structs.

3 Likes