Enum Attributes seem not to work as expected

I’ve implemented some attributes as enums, and, unlike simpler types (numbers, for instance), they do not seem to behave as documented.

TextDraw.attributes.add('VerticalAnchor', {
    type: 'string',
    enum: [
        { 'Centre'      : "Centre" },
        { 'Top'         : "Top" },
        { 'Bottom'      : "Bottom" }
    ],
    default: 'Centre',
    description: 'Select the style of vertical anchoring for this text item'
});

Specifically, if I set no default, the value of the attribute is always ‘null’. If I set a default, the value is always the default, and never reflects changes made in the editor. Lastly, the function that should be called when an attribute changes never fires, ie:

    // fires for all attribute changes
    this.on('attr', function(name, value, prev) {
        // new attribute value
        console.log("Attribute change: " + name + " => " + value + " prev: " + prev );
    });

(this fires for numeric attribute changes)

… am I doing something wrong? I’d dearly like to use an enum for readability. Thanks lots!

Hey. Just tested what you are trying to do in this project: https://playcanvas.com/project/488175/overview/enumstring
Seems to work correctly. Do you parse script in Editor after changing attributes?

1 Like

hey, max! Thanks lots for the reply!

Pretty weird, I forked your example, of course it does indeed work!

Then I took that code and put it into my JS file, no joy… then I created an identical test.js to yours in my own project, and that code worked just fine. Also, my own code fragment works fine in your test project.

I noticed that if I clicked away from the object and back again, the settings hadn’t ‘stuck’ and reset to ‘…’, so eventually, I actually reloaded the editor page and boom… things started working. Pretty confused, but everything is AOK now.

Thanks for the reference code and the help! :slight_smile:

(yes, I had been hitting the ‘parse’ button regularly, by the way!)

Hm, could have been a bug that somehow re-parsing would not working for some reason. But would need a clear way to reproduce such scenario.

I’ll keep an eye on it, if it crops up / I can give you a clear repro, I’ll let you know!

1 Like