L
Laseph
Guest
Is there any way I could get the size of an enumerator or should I just use another data type?
What wwrong with post 4?Thanks, I will use this for the time being. There's probably not a nicer way to do this like with ds_list_size()?
In some cases it's good to know the number of enums defined. but only in c++ I found. where for example you are using a 3rd party system which has defines but you only support version 1 of the 3rd party system, and so some of your functions only work for the defines listed in version1But why would you even need this? Enums cannot have their sizes changed during runtime. And besides, they will be turned into integers by the compiler, so their names will not be of any meaning anymore...
Maybe we could help you if you told us why you needed this.
-Surgeon_
What for, if you don't mind me asking?Actually being able to programatically access the number of elements and the name of elemtns in an enum would be really, really nice!
Automated state system.What for, if you don't mind me asking?
Super late but that's an awesome solution, never seen that thanksWell since they are constants, it shouldn't be that hard to count them yourself.
But if you are not assigning your own values for the enum then you can just slip a constant at the end and it will be the size of the enum
enum rainbowcolors {
red,
orange,
yellow,
green,
blue,
indigo,
violet,
length
}
rainbowcolors.red=0
..orange=1
..yellow=2
....
length=7
There are 8 constants in this enum so the size is length+1
This is indeed the solution used across the industry--just include a constant at the end of the Enum and make sure it is always the last entry.Super late but that's an awesome solution, never seen that thanks
You actually don't need to subtract 1 in for loops. If enum.length is 6, that means you have 7 entries in the enum and a loop for i to enum.length would run 6 times.Be sure to subtract 1 from any for-loops if you are populating arrays based on the length of the Enum.
You're right. I was thinking more along the lines of how it's done in Excel VBA. http://www.cpearson.com/excel/Enums.aspxYou actually don't need to subtract 1 in for loops. If enum.length is 6, that means you have 7 entries in the enum and a loop for i to enum.length would run 6 times.
// THIS IS VBA, IT WON'T WORK AS-IS IN GML!!!
Enum FruitType
[_First] = 1
Apple = 1
Orange = 2
Plum = 3
[_Last] = 3
End Enum
// CREATE EVENT
enum SomeEnum
{
entry1,
entry2,
entry3,
entryn,
entryLAST
}
// CREATE EVENT OR SOME OTHER EVENT
for (var i = 0; i < SomeEnum.entryLAST; i++)
{
//Tests values of 0 - n without including the very last entry.
}
I use an enum to keep track of skills in my game, so that things like using the skill or activating skill update event as a sort of sub event system can be tracked over the network in my multiplayer game. Each character I decided may as well just have an instance of every unique skill at a time, so that skill can respond to net events properly whenever it ends up being equipped and needing to. So i can make a loop to instantiate every currently existing skill into a map of them by their id.. except i dont even need a map to keep a mapping of them all by id, bc i can just use an array of size SKILL.length. For example. I cam here looking for how a nice way to write a thing I needed for this a simple integer sequence id system im using to keep track of across the network, and that answer was perfect.What for, if you don't mind me asking?