checkboxes don't accept ObservableBoolean as value

hi guys,

i came across something rather inconvenient. as the title tells you, checkboxes don’t work with the type ObservableBoolean.

i want to initialize a checkbox value with an option. here is some example code:

class "Preferences"(renoise.Document.DocumentNode)  
  
function Preferences:__init()  
 renoise.Document.DocumentNode.__init(self)  
 self:add_property("checkbox_value", false)  
end  
  
options = Preferences()  
renoise.tool().preferences = options  
  
local vb = renoise.ViewBuilder()  
  
local checkbox = vb:checkbox{  
 value = options.checkbox_value,  
}  

executing this throws the following error:

*** std::logic_error: 'ViewBuilder: expected a boolean argument for property 'value''  
*** stack traceback:  
*** [C]: in function 'checkbox'  
*** main.lua:13: in main chunk  

i can work around this by using a function like this:

function to_boolean(val)  
 assert(type(val) == "ObservableBoolean",  
 "expected ObservableBoolean")  
  
 if val == true then  
 return true  
 else  
 return false  
 end  
end  

i guess this problem can arise with many different controls that have boolean properties. that’s not cool.

could you maybe allows this, renoise devs? or are there good reasons not to?

cheers!

EDIT: okay, actually, this is true for all Observable data types. not just Booleans, but also Numbers and Strings (didn’t try that, but i assume so). i am not assuming this is a bug and i guess there is a reason why it doesnt work that way, but i would still like to know why. ;)

Either use

value = options.checkbox_value.value  

to initialize the control with a default value

OR

bind = options.checkbox_value  

to bind the control’s value to a prefs value, so changes to the prefs value will be picked up by the control and changes to the control will be picked up by the prefs value autmatically.

I suspect that options.checkbox_value may be nil for some reason (i don’t see where you defined the “options” checkbox_value but you should take care that it exists and is executed before that code snippet is executed) which will throw that error.
You can check this by adding a notifier function that simply prints the value options.checkbox_value, then you can actually see what value it really contains.
You could also consider the bind option (you can then bind it to a specific control which takes care that your checkbox will always be set to whatever that particular value is at that moment)

oh, duh! :lol: totally makes sense. thanks a lot! somehow i forgot about .value, but using bind makes more sense in my case anyway.

its self:add_property(“checkbox_value”, false) in Preferences:__init(), which gets executed in line 8: options = Preferences()

or did i get you wrong? yeah. as i replied to taktik, bind might be the best thing to do in my case. i don’t know why i didn’t consider it, derp, but thank you guys for the help!