Attached value of ComboCox outdated at Listener

When one of my UI components gets notified via a parameter listener on a change on a ComboBox, the value I read from the tree state is outdated.

I have a tree parameter, representing a choice, declared like this (getOptions returns a StringArray):

layout.add(std::make_unique<juce::AudioParameterChoice>("WaveShapeFunction", "WaveShapeFunction", getOptions(), 0));

Now one of my UI components subscribes to this parameter via a listener:

for (auto param : audioProcessor.getParameters()) {
    ...
    if (param->getName(64) == "WaveShapeFunction") { param->addListener(this); continue; }
}
void Component::parameterValueChanged(int parameterIndex, float newValue) {
    ... // in practice, it's a call back to the Processor to get the tree state value
    ... // I've condensed it here into one line
    value = apvts.getRawParameterValue("WaveShapeFunction")->load();
    DBG(value);
}

Listening itself works, but the value got is always one selection behind.
So the “standard” selection is 0.
I select 4, output: 0. I select 2, output: 4.

I’ve completely wrecked my brain by now, does anyone have an idea?

From JUCE docs of getRawParameterValue():

Note that calling this method from within AudioProcessorValueTreeState::Listener::parameterChanged() is not guaranteed to return an up-to-date value for the parameter.

I would suggest using the float newValue in your case.

THAT was absolutely solid advise. I actually did not think about looking at the docs at ALL… That’s what you get for running into a wall for hours.

For anyone interested, if you’re trying to use the newValue from the Listener’s parameterValueChanged on a ComboBox, you’ll have to map the parameter’s value (float, 0…1) to the range of your choices to get a usable index:

void SomeComponent::parameterValueChanged(int parameterIndex, float newValue) {
    // the number of options your ComboBox offers (number not index, not 0-based)
    int numOptions = 1234;
    int selectedIndex = juce::jmap(newValue, 0.f, (float)numOptions - 1);
}