Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Jakey on Wed 30/05/2012 17:35:40

Title: Enumerating Through Enumerations
Post by: Jakey on Wed 30/05/2012 17:35:40
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.
Title: Re: Enumerating Through Enumerations
Post by: monkey0506 on Wed 30/05/2012 18:23:01
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.
Title: Re: Enumerating Through Enumerations
Post by: Jakey on Wed 30/05/2012 18:27:39
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
Title: Re: Enumerating Through Enumerations
Post by: monkey0506 on Wed 30/05/2012 19:13:01
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
Title: Re: Enumerating Through Enumerations
Post by: RickJ on Sun 03/06/2012 05:15:20
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.
Title: Re: Enumerating Through Enumerations
Post by: Jakey on Mon 04/06/2012 10:25:51
I believe that the default value for the first list item in AGS is 1, but I could be wrong.