I’m currently watching the tutorial videos for UE4
What is the difference between an array and an enum? As far as I can tell, they are the same thing. Why would you use an enum over an array (and vice versa)?
I’m currently watching the tutorial videos for UE4
What is the difference between an array and an enum? As far as I can tell, they are the same thing. Why would you use an enum over an array (and vice versa)?
They are 100% different things, i suggest learning the basics of programming first.
Anyway, here is the explanation. An array is actualy a container of data, think a “Shelf” where you place AActors, numbers, or whatever the hell you want, then you access it with the number, for example yourarray[1] will get the object with its in the position number 1. If you still dont know, see here Arrays - C++ Tutorials
Enums are totally different, its just a way to asign a name to a number, its useful for some places when you want to have several possiblities, think about an object state, you can have Idle, Runing, Dead, etc. But you dont want to store the full name as its problematic, and working with numbers like Dead=1, running = 2, Dead = 3 is confusing as hell, so you create an enum with those 3 words, and that way you can select one. If its a bit confusing for you, enums normally are until you actually use them, take a look here What Is an Enum in Programming Languages?
Keep in mind those links are for written lenguages and not specially for blueprint, but they are still useful to understand the concept. Feel free to ask if you are confused
I have worked with arrays in visual basic, but I haven’t touched enum before.
So basically, an enum is the opposite(ish) of an array. It uses a word to index a value instead?
Can you do 3d enums like you can do 3d arrays?
kinda, but you cant do multidimensional enums. Enums are to give proper names to things, to types. Its creating your own type that can be one of several names, very useful in gamedev, and you will see them all over the place in unreal engine.
So if you can’t do multidimensional stuff with enums; What can you do with an enum that you can’t do with an array?
From what I’m understanding, a programmer got lazy and decided to create an the enum type as a shortcut.
they are NOT used for the same thing, in the array you store things, in the enum you dont store anything, is just giving a name to a number to use it easily when coding, for the engine, all enums are just ints. Also you cant change an enum at runtime
So an enum is just an array of pointers?
As the name describes enum is used to enumerate a group of values.
An examples usage is for something like the state of a NPC.
You could make an integer variable to describe this without enums and you’d then have to write a check like:
if(NpcState == 1) // using C++ for example.
You’d then have to know what 1 means. Sleeping/Dieing/Hungry, whatever.
Enums are used to write the same code but give the value names the programmer can easily understand.
So instead you make an enum called NpcStates and fill it with the names you need.
You can then write:
if(NpcState == NpcStates::Hungry)
I was confused about this once. I realized that an enumeration is a special array of labels. Labels are easier for us humans to recognize and process. These labels are converted to numerical value that’s easier for the computer to process. With the pseudo code below, its easy to see that the Human Version Enumerations are easier to read than the computer version.
Human’s Version
enum fruits
{
apple, grape, bananna, blueberry, orange, watermelon, cherry, strawberry
}
fruits myfavoritefruit
myfavoritefruit = blueberry
enum CarMake
{
Dodge, Nissan, Lexus, Ford
}
CarMake MyCarMake
MyCarMake = Dodge
enum BotState
{
Running, Patrolling, AimShoot, Shooting, Evading, Hunting
}
BotState FSM
if Energy = 10 Then FSM = Hunting
if EnemyInRange = TRUE Then FSM = AimShoot
Computer’s Version
enum fruits
{
1, 2, 3, 4, 5, 6, 7, 8
}
fruits myfavoritefruit
myfavoritefruit = 4
enum CarMake
{
1, 2, 3, 4
}
CarMake MyCarMake
MyCarMake = 1
enum BotState
{
1, 2, 3, 4, 5, 6
}
BotState FSM
if Energy = 10 Then FSM = 6
if EnemyInRange = TRUE Then FSM = 3