[SOLVED] Attributes with Enums

I’m aware that you can do enum attributes like this:

MyScript.attributes.add('myEnum', {
    type: 'number',
    enum: [
        { 'Something': 1 },
        { 'SomethingElse': 2 },
        { 'SomethingElseAgain': 3 }
    ],
    title: "Some Enum"
});

And that you can also do:

MyScript.MyEnum = 
    {
        Something: 1,
        SomethingElse: 2,
        SomethignElseAgain: 3
    };

Is there a way to combine the two?
(such that the enum can be exposed within the editor and still be referenced in code without having to use ‘magic numbers’.

E.g. something like the following (but this obviously won’t work):

MyScript.attributes.add('myEnum', {
    type: 'number',
    enum: MyScript.MyEnum,
    title: "Some Enum"
});

MyScript.MyEnum = 
    {
        Something: 1,
        SomethingElse: 2,
        SomethignElseAgain: 3
    };

if (someValue == MyScript.MyEnum.Something) { ... }

Wondering if anyone has found a nice solution to this?

I’m not sure if you can combine the two. @will or @yaustar might be able to give much better answer than me.

You can do something like this:

Movement.SomeEnum = {
    A: 1,
    B: 2,
    C: 3
};

Movement.attributes.add('someEnum', {
    type: 'number',
    enum: [
        { 'A': Movement.SomeEnum.A },
        { 'B': Movement.SomeEnum.B },
        { 'C': Movement.SomeEnum.C }
    ],
    title: "Some Enum"
});

Not great but at least you don’t duplicate the values.

2 Likes

Thank you for the idea, seems like a good half-way house!