Stuck on Druid component set enabled

Hi everybody!
Just got familiar with Druid and started to play with button. I would like to use it as a toggle.
If component is initialising, then I set state to required one.
When user will click this component styles will reverse component state to an opposite one.

local sound_toggle = self.druid:new_button("sound_toggle/toggle", toggle_sound)
sound_toggle:set_style(toggleStyle)
sound_toggle:set_enabled(settings.options.sound)
local M = {}

M["button"] = {
    LONGTAP_TIME = 0.4,
    DOUBLETAP_TIME = 0.4,

    TEXTURE_ENABLED = "toggle_on",
    TEXTURE_DISABLED = "toggle_off",

    on_click = function(self, node)
        self:set_enabled(not self:is_enabled())
    end,

    on_set_enabled = function(self, node, state)
        dd('on_set_enabled', state)
        if state then
            gui.play_flipbook(node, hash(M.button.TEXTURE_ENABLED))
        else
            gui.play_flipbook(node, hash(M.button.TEXTURE_DISABLED))
        end
    end
}

return M

The problem is that with initial state it is working fine, but with toggling state it return error message:
ERROR:SCRIPT: /druid/base/hover.lua:86: attempt to index local ‘self’ (a boolean value)

Not sure why it is happening like that? Can anybody help with that?

1 Like

Can you show us line 86 in hover.lua? The error message shows that somewhere in the code self was overridden with boolean value, or to the function with an argument named self you passed a boolean value in that place

1 Like

Thanks for report! I see the problem with hover set_enabled, I’ll fix that soon

But what are you trying can be done without styles. Styles designed just for component visual customization. And disabled button with set_enabled will be not active for user input

Simple toggle button example code

local function on_sound_click(self, params, button)
    settings.options.sound = not settings.options.sound

    local image = settings.options.sound and "toggle_on" or "toggle_off"
    gui.play_flipbook(button.node, image)
end

function init(self)
    self.button_sound = self.druid:new_button("sound_toggle/toggle", on_sound_click)
end
1 Like

My idea was to exclude toggle state logic and animation to the style, which I can apply to different components.
Thanks for answers, Insality!

1 Like

Yes, it sounds okay to share logic via styles

It is expected to disable button, which was clicked? Since to enable it again, you need another action (from other button for example)

Current bug happen when trying to disable clicked button (issue #80)