This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote

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.

in Engine by (27 points)
edited by

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

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.

3 Answers

0 votes

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

enum MyEnum {
    FIRST_KEY,
    SECOND_KEY,
    THIRD_KEY,
}

print(MyEnum) 
# (FIRST_KEY:0), (SECOND_KEY:1), (THIRD_KEY:2)

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

print(MyEnum) 
# (FIRST_KEY:42), (SECOND_KEY:1), (THIRD_KEY:2)

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.

by (1,422 points)

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

myEnum{
1,
2,
3
}

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?

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]
+2 votes

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.

by (1,492 points)
0 votes

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^^

by (29,510 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.