Skip to main content
gamedev.net
🔒 Locked

Enum

Started by Tazel Jan 28, 2003 at 3:50 PM 7 replies 300+ views
Original Post
Tazel
Tazel
I know what enum does and how it works but what to use it for is the question...
tcache Contact Me ----------- AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
IADaveMark
IADaveMark
Any time you have a list of things that you would normally number. For example, I use it for types of things or states. Rather than saying something hard to read like:

if (CurrentState == 3)
{
SetState(5);
}

you can say...

if (CurrentState == STATE_HUNTING)
{
SetState(STATE_ATTACKING);
}

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
Thunder_Hawk
Thunder_Hawk
To help get rid of magic numbers for one:

  
enum MODE {START, PLAY, NEW, RESET, PAUSE, GAMEOVER, QUIT, GOBACK};
mode = PLAY;
if (mode == PLAY) {
DoGameLogic();
}

etc....

_____________________________

And the Phoenix shall rise from the ashes...

--Thunder_Hawk -- ¦þ
______________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma
JoeyBlow2
JoeyBlow2
1) Its easier to read the code.

2) Less bugs... Think of a calculator program. You have Operation "Add", "Multiply", "Divide" with the numbers 1,2,3.


switch(operation)
{
case 1:
number+= value;
case 2:
number*= value;
case 3:
number/= value;
}


How about if you want to add in "Subtract" as being #2 instead.

You'd have to look throughout the entire code where anything is checking the operation variable (switches or if...statements) and change them to use the new number values.

If you made them ENUM's, you would just change the ENUM's around and all the ifs and switch statements would all be converted for you.



enum
{
ENUM_ADD,
ENUM_SUBTRACT,
ENUM_MULTIPLY,
ENUM_DIVIDE
};

switch(operation)
{
case ENUM_ADD:
number+= value;
case ENUM_MULTIPLY:
number*= value;
case ENUM_DIVIDE:
number/= value;
}


It reduces the number of bugs in case you did change the scale. And it doesn't slow down the code at all (its figured out at compile-time, not during run-time)... #2 is the main reason to do enums... My work does enums ALL THE TIME. If there is ever is a scale of numbers to do operations its always enumed...

[edited by - JoeyBlow2 on January 28, 2003 5:11:03 PM]
Tazel
Tazel
So...why would you number like the states of a program?



tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcacheContact Me-----------AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
IADaveMark
IADaveMark
quote:
Original post by Tazel
So...why would you number like the states of a program?

Uh... because you have to keep them separate somehow. How else would you be able to determine what state you are in?

Dave Mark - President and Lead Designer
Intrinsic Algorithm -
"Reducing the world to mathematical equations!"
Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit<
felisandria
felisandria
It's all about readability, maintenance, and the fact that some guy who's never seen your code before is going to be messing with it at some point.

Tell me, which would you rather see when you have to jump in and do some maintenance on code that isn't yours (or even code that IS yours but you haven't touched in a few weeks or months or years), 200 case statements numbered 1 to 200, or 200 case statements with things like

case STATE_WALKING:

?

Yes, you could just use a #define instead... but if you use an enum you don't have to go hunting to see if you already used that number for a #define earlier... and it's easier to control scope... and it's typed according to the enum...

-fel

[edited by - felisandria on January 28, 2003 6:01:20 PM]
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.