I was thinking of implementing an enum that defines the state of a game object, and I wanted to know if I could directly use flags within the enum's definition, instead of defining the object's state as a collection of flags with no easy, pre-defined, global name for the states used in the state machine.
For example, let's say there are 5 states: PreActivation (Created but not started; i.e. an enemy in a future wave), Active (Currently in use; i.e. an enemy on the screen, attacking you), Paused (No longer active, but may reactivate; i.e. an enemy if the player uses a time-freezing power), DeActivated (An object whose finished use but is still in the game world; i.e. an enemy whose body is left after death like in Doom 1 & 2), and ToRemove (An object slated for removal from the game; i.e. an enemy after you clear a level and move to the next one).
What I want to do is define the enum so the states hold all applicable flags; for instance, a DeActivated enemy: 1. Has been previously activated, and 2. Isn't currently active. My current thinking is doing something like this:
public enum ObjectState
{
// The first section are the flags
BeenActivated = 0b0000001, // Previously activated
CurrentlyActive = 0b0000010, // Currently activated
IsSuspended = 0b0000100, // It may be reactivated
ShouldRemove = 0b0001000, // It should be removed
// These are the states
PreActivation = 0b0000100, // Mot currently active, nor has it ever been active, but it will get activated
Active = 0b0000011, // Currently active, and it's been active
Paused = 0b0000101, // Not currently active, but it's been active before
DeActivated = 0b0000001, // Not currently active, but it's been active before, and it shouldn't get reactivated, but don't remove yet
ToRemove = 0b0001001 // Not currently active, but it's been active before, and it shouldn't get reactivated, it should be removed
}
As far as I know, this should work correctly, but I have a few main concerns:
- Are there any problems likely to come from this?
- Is this bad practice?
- Is this bad practice? And, if it is;
- A. What's wrong with it?
- B. What should I do instead? I'd just make the object's state a collection of these flags, but I'd like a shorthand enum for specific states, as this allows for complexity for specific instances and simplicity when it's needed. Is there a more acceptable way to achieve this?
Sorry if this is a repeat or I broke some other rule, but I just created an account today; this is my 1st post. Plus, I'm not sure what you would call this when searching, and I didn't get any similar hits from here or Google.
[Flags]
attribute to the enum and you're done, no problem at all. Also, you can use(1 << 0), (1 << 1), (1 << 2)
and so on for the values if you don't want to have binary numbers (just a matter or preference). – Sphygmo