TUTORIAL: Enumeration in Game Maker: Studio and Faking It
For those of you who don’t follow the Game Maker news, YoYo Games has recently added enumeration in the Early Access release! It’s a valuable feature, but I fear it may be some time before it hits the stable versions. Thus, I have put together a forwards-compatible fake enum that uses different declaration, but identical access syntax.
In this post, I shall explain how to start using some of the new enum syntax, and how to fake it until it reaches the stable versions.
To put it simply, enumeration assigns integers to a set of named constants. Thus, instead of setting your mode variable to 1 or 2, you can set it to MODE.play or MODE.pause. It makes it easier to distinguish different states, or can help keep track of array values, and things like that.
Enumeration in Early Access is pretty easy. It works similar to a var declaration, so you would simply do
enum GROUP { itemA=1, itemB, itemC };
This will assign 1 to GROUP.itemA, 2 to GROUP.itemB, and so forth. Those values can be accessed just like that. Now, with our fake enumerators, we can use the same access syntax, but we’ll have to declare it a little differently. This will let you use enums wherever you want in the stable version, but will be easily upgradeable once enums reach the stable versions.
NOTE: All names provided can be changed, but remember to change them in the provided code if you use it!
Start by creating a blank object and calling it fake_enum. Make it persistent, and then close it. Next, create a script called init_enums. Here, we’ll put all the declaration. The declaration for each enum goes like this:
globalvar GROUP;
GROUP=instance_create(0,0,fake_enum);
with (GROUP)
{
  itemA=1;
  itemB=2;
  itemC=3;
}
If you don’t already have a controller object that’s present in the very first room, go ahead and make one now. In your controller object, run the script init_enums in the Game Start event.
Now you can use your enums!
WHAT DO I DO WHEN ENUMS COME TO STABLE?
All you have to do when enums come to stable is change init_enums. Use the syntax as listed in the first section, and everything should work the same way.
WHY NOT JUST USE CONSTANTS?
You could just use constants! There’s essentially no difference in access times between real enums and constants, at least on Windows, so it’s really up to preference. Fake enums do have a speed disadvantage (they are about 60% slower on Windows), but will receive the speed boost once they are switched to real enums.
If anything doesn’t work, feel free to hit me up on Twitter or message/ask me on here.Â