Legacy GM Getting the size of an enum

L

Laseph

Guest
Is there any way I could get the size of an enumerator or should I just use another data type?
 

jo-thijs

Member
Hi and welcome to the GMC!

I'm not sure though what you mean with size of an enumerator.
Can you please explain in more detail?
 
L

Laseph

Guest
Thanks for responding, with the size of an enum I mean the amount of constants defined in the enum.
 
S

seanm

Guest
Well 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
 
L

Laseph

Guest
Thanks, I will use this for the time being. There's probably not a nicer way to do this like with ds_list_size()?
 

Surgeon_

Symbian Curator
But 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_
 
I

icuurd12b42

Guest
Thanks, I will use this for the time being. There's probably not a nicer way to do this like with ds_list_size()?
What wwrong with post 4?

rainbowcolors.length?

But 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_
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 version1
 

GMWolf

aka fel666
What for, if you don't mind me asking?
Automated state system.
Just create aj ebum for states. Then script would automatically get assigned...
I'm currently doing this with an array of strings.

Other things like enum valueOf and all would be great too.

I don't use enums much because of how limited they are in GM, but in Java, where enums have massive amounts of methods (for enums), I end up using them a lot!
 
H

hunijkah

Guest
Well 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
Super late but that's an awesome solution, never seen that thanks
 
Super late but that's an awesome solution, never seen that thanks
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.

Be sure to subtract 1 from any for-loops if you are populating arrays based on the length of the Enum.
 
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.
You're right. I was thinking more along the lines of how it's done in Excel VBA. http://www.cpearson.com/excel/Enums.aspx


Code:
// 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
Anyway, yes, a basic for-loop like this would work for your enum type in GML:

Code:
// 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.
}
 
What for, if you don't mind me asking?
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.
 
Top