How do you create enums for state charts in ObjC?
Hi mates!
How do you create enums for state charts in ObjC?
Usually, in C++ I do something like:
enum GAME_STATE {
STARTING,
PLAYING,
...
ENDING
};
GAME_STATE m_game_state;
But it seems "enum" does not exist in ObjC.
How do you usually implement state charts or finite state machines in ObjC? I mean, How do you exactly represent states?
Thanks a lot.
How do you create enums for state charts in ObjC?
Usually, in C++ I do something like:
enum GAME_STATE {
STARTING,
PLAYING,
...
ENDING
};
GAME_STATE m_game_state;
But it seems "enum" does not exist in ObjC.
How do you usually implement state charts or finite state machines in ObjC? I mean, How do you exactly represent states?
Thanks a lot.
enum's exist in C, and as Objective-C is a strict superset of C they exist in that also; the following compiles happily in Objective-C:
Code:
typedef enum
{
STARTING,
PLAYING,
ENDING
}
GAME_STATE;
[i][snip][/i]
GAME_STATE m_game_state;Mark Bishop
sealfin Wrote:enum's exist in C, and as Objective-C is a strict superset of C they exist in that also; the following compiles happily in Objective-C:
Code:
typedef enum
{
STARTING,
PLAYING,
ENDING
}
GAME_STATE;
[i][snip][/i]
GAME_STATE m_game_state;
Thanks for reply.
Just one question, in my C++ programs I don't use typedef before enum, why do I have to use typedef if C has enums?
Thanks.
sealfin Wrote:Because C++ is not a strict superset of C: link.
Thanks!
I guess you are talking about:
"A struct, union, or enum declaration in C++ is a first class type, while in C it is not."
isn't it?
This is interesting stuff. I had a similar problem, except it would just give me warnings saying that the statement did not declare anything. This happened when trying to declare the enum in the scope of a class interface. Simply moving it to global scope fixed the problem. Also, when declaring instances of the enum, I always had to put the "enum" before the declaration. For example:
Any thoughts?
Code:
@interface Whatever
{
enum ENUM {ZERO, ONE, TWO, ETC}; // Gives warning.
}
@endCode:
enum ENUM {...}; // No problem.
@interface Whatever
{
enum ENUM m_enum; // Errors without the "enum" at the beginning.
}
@endAny thoughts?
Instance variables go between those braces, not any old declaration you like.
In C, "struct S { ... }" declares a type called "struct S", and "enum E { ... }" declares a type called "enum E". In C++, "struct S { ... }" declares a type with two names, "struct S" and "S", and "enum E { ... }" declares a type with two names, "enum E" and "E". To get the simpler name like C++ in C you need a typedef, for example "struct S { ... }; typedef struct S S;", or you can shorten that to "typedef struct S { ... } S;".
In C, "struct S { ... }" declares a type called "struct S", and "enum E { ... }" declares a type called "enum E". In C++, "struct S { ... }" declares a type with two names, "struct S" and "S", and "enum E { ... }" declares a type with two names, "enum E" and "E". To get the simpler name like C++ in C you need a typedef, for example "struct S { ... }; typedef struct S S;", or you can shorten that to "typedef struct S { ... } S;".
Or if you don't care about having a custom type you can just do (in C):
enum {
STARTING,
PLAYING,
ENDING
};
enum {
STARTING,
PLAYING,
ENDING
};
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Game State Management | Mattonaise | 6 | 6,134 |
Apr 16, 2011 10:18 AM Last Post: Mattonaise |
|
| Taking Pictures of Game State inside an app | Bersaelor | 7 | 4,598 |
Nov 3, 2009 05:07 AM Last Post: Bersaelor |
|
| objc++ problem | vidjogamer | 7 | 2,865 |
Aug 5, 2009 02:02 PM Last Post: longjumper |
|
| Save game state using Property list or Text File? | Graphic Ace | 2 | 3,202 |
Apr 6, 2009 03:48 AM Last Post: Graphic Ace |
|
| Saving the game state when pressing the Home button | Nacho | 3 | 3,001 |
Feb 10, 2009 05:33 PM Last Post: Nacho |
|

