Exported variable with a range that does not include zero

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By rraallvv
:warning: Old Version Published before Godot 3 was released.

How can I export a variable that allows only -1 and +1 as values?

export(int/float, -1, 1, 2) var direction

The declaration above only allows values of 0 and 1

EDIT: I misread your code, this actually looks like a bug in your particular case.

However, if you wanted to allow values between -2 and 2 for example, and forbidding zero, it would currently not be possible. What I do in this case usually is to either expose the variable in a different way (multiplier instead of a divider for example) or just add a defensive if in the setter to be sure to not set incorrect values.
If you need only a few values though, maybe you could export it as an enumeration?

In 3.0 there have been mentions of improving range definitions Add option to limit export to positive or negitive values by kitsune · Pull Request #11599 · godotengine/godot · GitHub but I don’t know what’s the state of it.

Zylann | 2017-11-07 13:37

this seems a bug to me.
according to documentation for export, it should be possible i think.
GDScript reference — Godot Engine (stable) documentation in English

in my short test,

export(int, -2, 2, 2) var direction

this allows -2, 0, 2

volzhs | 2017-11-07 13:40

yeah, I misread the code (though part of my answer is still valid).
Maybe that’s because the editor snaps with zero instead of the min value?

Zylann | 2017-11-07 13:44

possibly. yeah.
the initial value of slider in the inspector should be handled properly.

volzhs | 2017-11-07 13:52

I’ll try with some mutator, thanks for info!

rraallvv | 2017-11-07 17:07

It could be a bug, maybe. I was thinking the same thing.

rraallvv | 2017-11-07 17:12