How to limit an exported int variable to odd numbers only?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AgeOfAsparagus

Limiting to even numbers is easy:

export(int, 0, 20, 2) var my_even = 10

Is there a way to allow only odd numbers in the exported variable?

So far this is the best I could come up with, but it’s ugly:

export my_odd = 11 setget set_my_odd

func set_my_odd(new_val):
	if my_odd % 2 == 0: # it's even, so bump it up or down
		if new_val > my_odd:
			my_odd = new_val + 1
		else:
			my_odd = new_val - 1
	else:
		my_odd = new_val
:bust_in_silhouette: Reply From: exuin

There doesn’t appear to be a way to do this currently. Feel free to submit a proposal on GitHub!

You can actually make your function one line with this

my_odd = new_val + ((new_val+1)%2 * sign(new_val-my_odd))

but one line doesn’t mean better or easier to understand.