How do you change the index of an enum?

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

I’ve been trying to get this enum working for a few days now and I can’t find anything about actually setting an enum.

Let’s say that myEnum is at index 0 right now and I want to change it to index 1,
what I thought I’d have to do is this: myEnum = 1, but that doesn’t seem to work. As I said, I haven’t been able to find anything related to setting the index of an enum. Maybe it’s right in front of my nose without realizing.

Thanks in advance.

EDIT: I seem to have been able to figure this out on my own. Turns out you can’t directly change a value in an autoloaded singleton script from another script. I just made a function that takes in an int and changed the enum’s index to that from within the singleton script. I’ve never worked with singletons before, so I guess this might be basic knowledge for some.

TL;DR: I managed to solve it.

I can’t reproduce it in any way, what do you mean by setting an index of enum?

Xrayez | 2018-09-02 13:12

You know how the values in an enum are represented by an int? You can get the current selected value from an enum but I can’t figure out how to set which value is the selected one.

Marmot | 2018-09-02 13:16

:bust_in_silhouette: Reply From: Xrayez

Enums in GDScript are implemented something like dictionaries internally (see docs), so the same operations on dictionaries apply for enums:

enum MyEnum {


MyEnum.FIRST_KEY = 42
# or
MyEnum['FIRST_KEY'] = 42


I think when you try to assign FIRST_KEY = 1 like that you’re in situation of rvalue = rvalue assignment which doesn’t work, or perhaps the enum values aren’t in sync with enum variables or something…

One would expect enum values to be constant though, oh well.

Isn’t an enum like a list of options where one option is always the selected one?


By default I think index 0 (“1” in this case) is the selected one. I want it to select index 1 (“2” in this case). Or am I wrong? From my experience with UE4 I would think that an enum works like that.

Does this make sense?

Marmot | 2018-09-02 13:54

I don’t think GDScript has notion of “currently selected, active enum value”. You’d need to implement your own class using dictionaries that hold the currently selected value then. To give you idea:

class Selector:
    var _idx = 0
    var selected = null setget set_selected, get_selected

    var data = {}

    func add_value(p_value):
        data[_idx] = p_value
        _idx += 1

    func set_selected(p_idx):
        selected = p_idx

    func get_selected():
        return data[selected]

Xrayez | 2018-09-02 14:01

:bust_in_silhouette: Reply From: MysteryGM

It is actually very easy, I am using it in my game like this:

enum MortalStates { FullHealth = 1, Dead = 2, Wounded = 3, Critical = 4}

This is for a very small browser RPG and it works perfectly.

:bust_in_silhouette: Reply From: Zylann

Can I see which code you used to solve your problem?
I think I misunderstand what the actual issue is just from the first post.

Just in case, here is a thing:
enums are constant. You can choose which value they have in the declaration, but you are not supposed to be able to change them at runtime. If you want to, use a dictionary. If you find a way to change enums at runtime, it’s not an intented behavior AFAIK and might be fixed in the future.
I precise it anyways because we had similar bugs with consts, to the point it got exploited, and one day it got fixed and someone asked devs to not prevent consts from being changed at runtime… the irony^^