What's the variable type that has predefined string choice, help to autocorrect and return error if i write the string wrong

Godot Version

` Godot-4

Question

` I recall seeing someone else code it and i don’t remember where. I tried to look for it but couldn’t.
something like this:

var sugar = {"0%","50%","100%"}
and i can use it to compare
if(cup1.sugar == cup2.sugar)

if i write thing like cup1.sugar=“40%” or i write cup1.sugar=“50” (missing%) it will allert me like “there’ no choice named 40%”

Or st like this:
like this:

st st {
	POSSIBLE CHOICE ONE
	POSSIBLE CHOICE TWO
}
var object.something = POSSIBLECHOICEONE

If I remember correctly, it return the string like in int like this for easier comparation
0% = 0 50% =1 100% = 2

it might give autocorrection so i don’t need to write the rest. or doesn’t show up when i write the number 4. like in Animation.play(“…”) there’re multiple yellow colored words to choose from. example: “run”,“idle”,… if i write “a” there will be nothing showup

for now i need to write this to check manually if i use the wrong string and it’s tiring

func check ():
	if sugar!="0%" || sugar!="25%" || sugar!="50%" || sugar!="70%" ||sugar!="100%":
		print("there doesn't exist this type"+str(sugar) "recheck your code")

Can you tell me what type, class, func,… it is
Thank you very much

Closest thing I can see is an enum (shorthand for enumeration).

You can use it to create a list of constants you can cycle between.

Example pseudocode:

enum EXAMPLE = {CHOICE_ONE, CHOICE_TWO, CHOICE_THREE}

What the Godot docs say about enums:

You can search for Godot enum tutorials for more info.

3 Likes

Just as the poster above me said, an ENUM is the closest thing.

enum Sugar {
    ZERO = 0,  # "0%"
    FIFTY = 1, # "50%"
    HUNDRED = 2 # "100%"
}

var cup_sugar: Sugar = Sugar.ZERO

# You can compare directly
func compare_cups(cup1_sugar: Sugar, cup2_sugar: Sugar) -> bool:
    return cup1_sugar == cup2_sugar

The editor will show autocomplete options and it will give an error if you try to assign an invalid value

3 Likes

Oh my thank you so much, it solved all my problems. Thank you :smiling_face_with_three_hearts:

3 Likes

Thank you. Godot has such a kind community. I used to have question on Unity forum unanswered for months and even never I was so touched having you guys here :smiling_face_with_three_hearts:

3 Likes