Given that MiniScript is a very minimalistic scripting language, it purposely lacks an Enum type. Its types are Number, String, List, Map, and funcRef (ie. a function reference) -- and that's it.
That means that when you want to express an Enum type, you may find yourself using magic numbers. You can use Numbers after all -- in fact I would imagine most underlying Enum implementations do in fact use numerical types under the hood -- but if you find yourself writing code like ship.firingMode = 2
, you're probably going to be in for a world of hurt when you try to return to your space shooter game after a six month break. You may not be able to remember the significance, nuances, and intricacies of firing-mode 2 off a quick glance.
Alternatively, you can use the String type. ship.firingMode = "MISSILE_MODE"
is valid MiniScript and is not a terrible alternative to Numbers, but it could lend itself to some interesting errors if you mistype the string somewhere, since ship.firingMode = "MISSSILE_MODE"
is also valid MiniScript.
An Enum class
I decided to implement a simple Enum class in MiniScript. I have just published it to GitHub in a library called bclib[1]. I hope to add more items to this library in the near future.
Let's set up a simple example of how we might use this Enum class in a space shooter. In this theoretical game, our ship can fire off all sorts of different objects, but it can only fire items of one type at a time:
import "bclib"
eShipShootingMode = bclib.Enum.create("Safety Guns Missile Mines Probe")
eShipShootingMode
represents your Enum type. We have chosen five firing modes:
- Safety (ie. Weapons off)
- Guns
- Missile
- Mines
- Probe
Note: we could have passed a list of strings, but in this example, we used a single string as our argument. Either will work. In the case of a single string, the create
function will split the string along spaces.
Now the ship will need to store its firing mode in a variable:
ship = {} // Define your ship properties here
ship.firingMode = eShipShootingMode.Safety // Weapons off
What if the user presses the key for guns? We can switch firing modes easily:
ship.firingMode = eShipShootingMode.Guns
The above line should easily convey to any reader of your code what your intention is: you want the ship to have its firing mode set to fire guns.
Also, trying to set your firing mode to eShipShootingMode.Misssile
(ie. a typo with one too many s
characters) will crash the program straight up when that line is executed. With a String, that won't happen unless you're doing extra error checking.
Other Functions
-
bclib.Enum.count
: A function that returns how many members the particular enum type has. -
bclib.Enum.getName
: A function that returns the name instead of the numerical identifier. In our example,eShipShootingMode.getName(eShipShootingMode.Guns)
will return "Guns". This can be useful for logging purposes, for example, as you'd want the string instead of the numerical value when logging, or else you'd need to look up what firing mode 2 is all over again.
[1] Short for "BibleClinger Library". Not too original, but straightforward.
Top comments (0)