How to increment an enum?

Godot Version



What is the best practice for incrementing enums? Basically I have an enum that represents level of cover:

enum CoverLevels{

As you can imagine its often useful to increment and decrement the cover level based on different conditions being met. I’ve been doing that with this code (level_of_cover is a property of the object that contains this method):

func increase_cover_level():
	if CoverLevels.find_key(level_of_cover + 1) != null:
		level_of_cover += 1

I get this warning at start up with that function though:

W 0:00:01:0010 Integer used when an enum value is expected. If this is intended cast the integer to the enum type.

Which leads me back to, what is the best practice for incrementing enums?

enums’ elements are basically constants
also, the idea to increment the enums during runtime is not right

If you wanted to do it like this
use dictionary

something like this will do

var CoverLevels:Dictionary={0:"NO_COVER",1:"PARTIAL_COVER",2:"COVER",3:"TOTAL_COVER"}

so the method will look like this:

func increase_cover_level():
	if CoverLevels.has(level_of_cover + 1):
		level_of_cover += 1
1 Like

Well…I’d rather be able to address it like an enum as it makes code easier to read…so in order to get something like the above I put this together and it seems to work without any warning. Kind of seems like I’m just tricking the compiler by hiding the data types, but it should be faster, and no warnings so I’m OK with that:

static var _cover_levels_dictionary : Dictionary
static var cover_levels_dictionary : Dictionary :
		if _cover_levels_dictionary == null or _cover_levels_dictionary.is_empty():
			_cover_levels_dictionary = {}
			for i in range(0,CoverLevels.size()):
				_cover_levels_dictionary[i] = i
		return _cover_levels_dictionary

func increase_cover_level():
	if REnums.cover_levels_dictionary.has(level_of_cover + 1):
		level_of_cover += REnums.cover_levels_dictionary[level_of_cover + 1]

I think your check logic is a little backwards. I think you should just add 1 to the level_of_cover and compare if it is still less than TOTOAL_COVER. Unless total cover is still valid then just add a cover_count and use that as your boundary.

if level_of_cover + 1 < TOTAL_COVER:
  level_of_cover += 1

There is no “best practice” for incrementing enums. It’s rarely useful, but this here is one usecase where it actually is useful.

The warning is just trying to remind you that the engine can’t check if your code is correct. So you can add an @warning_ignore("name_of_warning") annotation on the line above, that way the warning is suppressed. I have no idea how the warning is named exactly though, but you should be able to see that in the warning message itself (in the bottom of the script editor, there should also be a button “ignore” to automatically add the annotation)

Just make sure that to be careful careful to never increment the variable higher than the maximum enum value.

1 Like

If you change level_of_color to an int the warnings disappear.
Is there anything you can do with level_of_color:ColorLevel that you can’t do with level_of_color:int?