• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Enum vs comparing strings

in the game I'm working on I load in my weapon stats from an external .csv file that is loaded into a global variable, global.lootTable, at the start of the game. Each gun has up to two special traits from a pool of 36 which may grow or shrink as I further develop the game. I am trying to decide how to handle the traits as each influences the game in different ways. Some influence the bullets fired directly like causing the bullet to bounce or give a status effect. Others create extra instances of the bullet, exp. shotgun, or influence enemy death to cause aoe damage or turn to stone. I have two ideas on how to do this.

The game is a roguelike so at the start of every run it checks the 5 gun slots and pulls the stats from the global.lootTable using an accessor [# somenumb, yOffset]. yOffset is the gun and the somenumb is pulled sequentially to load the data into an array for each slot. At this point I have a choice. I can leave the data for the traits as a string or I can interpret them with a string comparison script that returns an enum.

1. If I leave it as a string every shot fired will run the trait strings through a switch comparison. This has little effect if it has no traits as it is the first case. However if its traits are near the bottom it will need to run a comparison through all 30+ traits above it. Once the trait is found it runs the code to influence the game.

2. If I make each trait an enum it increases both memory usage and load times. This caused build time to go up 2ish seconds which isn't too bad. But as I can give each trait a numerical value I can run a if-else statement that splits all the traits into groups of ten or so. Then a switch statement finds the exact value and runs the code to influence the game. This caps the most amount of checks to 12 before the code fires. each new set of ten adds one to the max checks.


The problem I have is that I am not too familiar with the strain enums cause. Could this many enums make the additional checks caused by a string comparison a better choice? Is there a much simpler option that I am missing?

edit: I would like to add that the fastest firing weapon with traits fires 12 per second meaning the checks ran per second for 1 is 360 vs 144 for the 2
 
Last edited:

Relic

Member
AFAIK an enum isn’t a big memory drain- it’s just referencing to an integer that is global in scope and makes use of GML auto complete functions. Build time may be longer- but is that what your end user cares about?
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
Personally, I'd go with using strings... When the game is compiled, these will be considered constant values anyway (similar to enums) and 30 items in a switch really isn't that much. It'll also keep the code easier to maintain as everything is in one place and you don't have extra code to chunk the checks.

Note that if you are in doubt, then test BOTH systems in the debugger and switch on Profiling to see what the actual strain is. ;)
 
Top