Slider::getValue() in valueFromTextFunction

Hello is there any way to get slider value inside of lambda valueFromTextFunction?

When I try to do this:

mySlider.valueFromTextFunction = [](const String& value)
{
    //... bla bla bla...
    float sliderValue = mySlider.getValue();
    //... bla bla bla...
};

Then I get error:

`this’ cannot be implicitly captured in this context

I feel like I understand why I get that error. But can’t figure it out how to get slider value in that context.

Add ‘this’ to your function declaration and you’ll be fine.

mySlider.valueFromTextFunction = [this](const String& value)
{
    //... bla bla bla...
    float sliderValue = mySlider.getValue();
    //... bla bla bla...
};
1 Like

Great it was so easy. Thanks a lot.

Just pointing out, that this is not the intended use of the lambda.

The lambda is supposed to convert any supplied string into a float value and the other lambda converts any supplied float value into a String.
So there is no need to call getValue(), you get all you need supplied as argument.

The host might call this with any value, not only the actual value (e.g. to fill a dropdown or whatever the host developer came up with).

Another caveat, the SliderAttachment copies the lambda, so potentially the lambda ends up in places, where the existence of the captured this pointer is no longer guaranteed.

1 Like

Calling getValue in that lambda is OK in the case the string cannot be converted into a value, returning current value in that case is appropriate.

  • Jussi

But the lambda is expected to convert any string into a value, not to return anything about the current value.
It might be called for that purpose most of the time, but in the cases, when it is supposed to convert a different value (to display min and max value in a host UI for instance, or populating a drop down menu), you get unwanted side effects.

2 Likes