Enumerating Through Enumerations

Started by Jakey, Wed 30/05/2012 17:35:40

Previous topic - Next topic

Jakey

I want to loop through all items in an enum. I'd rather not use if/else's to check each item in the list because it would be nicer if I didn't have to add another "if else" case if I decide to add more items to the enum.

Is there an easier/nicer way to enumerate through my enums??

Right now I figure I can create a counter starting at 1 and looping until a defined size of the enum, since the items are only ints anyways. But, I'd like to know if there is an even more efficient way where I wouldn't have to #define the number of items in the list ahead of time.

monkey0506

If you know the last defined item in the enum you can use that. Otherwise an int based loop is probably the best route. Some things have predefined maxes though too like Game.MouseCursorCount and Game.FontCount.

Jakey

#2
Yeah, that's a little better than defining the length I suppose. I'll just have to make sure never to change the last item. Thanks for the reply.

Edit: Haha, I could always add an "EnumType_END" item that's sole purpose is to provide me with the length. This would also prevent me from having to remember a "+1" in the stopping condition for loops :P

monkey0506

Yeah, I wasn't sure if you meant the built-in enums or just your custom ones. An "end" enum value isn't a terrible idea, a lot of people do that in mainstream programming languages, so why not AGS? :P

RickJ

Just for good measure set the first enumerated value equal to 0 which is currently the default behavior but not necessarily guaranteed.  Then as monkey says use end or none as the last enumerated value.

Jakey

I believe that the default value for the first list item in AGS is 1, but I could be wrong.

SMF spam blocked by CleanTalk