# Plugin loaded with no state in FL 21

**URL:** https://forum.juce.com/t/plugin-loaded-with-no-state-in-fl-21/57600
**Category:** Audio Plugins
**Created:** [August 28, 2023, 1:28pm UTC](https://forum.juce.com/t/plugin-loaded-with-no-state-in-fl-21/57600 "2023-08-28T13:28:43Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![crowbait](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@crowbait](https://forum.juce.com/u/crowbait)
#### Post date: [August 28, 2023, 1:28pm UTC](https://forum.juce.com/t/plugin-loaded-with-no-state-in-fl-21/57600/1 "2023-08-28T13:28:43Z")

</div>

I have to preface this by saying that this is not only my first project in JUCE, but my first in C++ as well.

I’m working in VS on Win11.  
The plugin I made works fine in AudioPluginHost, but not in FL Studio - I realize this may then be a problem with FL, but I think it’s more likely I did something wrong.

What happens is:  
You can load the plugin, play around with it, every thing is fine. But when you save the project and reopen it, things get weird:

- IF the plugin’s editor is open on load (saved with editor open), the plugin does not produce output until you close and re-open the editor.
- IF the editor is closed on load, the plugin does not produce any output until you open the editor, close it and open it AGAIN.

… both of these things happen ALMOST all the time, but rarely it just works when loading. **Also** I’m certain that there is no output because my `Gain`s are initialized with a default of `0`.  
The UI shows the correct settings directly after loading.

My guess is that it’s tied to loading the state (FL weirdly calls `getState` before `setState`, see [here](https://forum.juce.com/t/fl-studio-20-vst-3-getstateinformation-called-when-plugin-is-being-created/32381)). I’m doing it like this:

```auto
void GnomeDistortAudioProcessor::getStateInformation(juce::MemoryBlock& destData) {
    juce::MemoryOutputStream mos(destData, true);
    apvts.state.writeToStream(mos);
}

void GnomeDistortAudioProcessor::setStateInformation(const void* data, int sizeInBytes) {
    auto state = juce::ValueTree::readFromData(data, sizeInBytes);
    if (state.isValid()) {
        apvts.replaceState(state);
        updateSettings(getChainSettings(apvts), getSampleRate(), leftChain, rightChain);
    }
}

```

updateSettings is a helper function defined as

```auto
...
settings.WaveShapeAmount = apvts.getRawParameterValue("WaveShapeAmount")->load();
settings.WaveShapeFunction = static_cast<WaveShaperFunction>(apvts.getRawParameterValue("WaveShapeFunction")->load());
settings.PostGain = apvts.getRawParameterValue("PostGain")->load();
settings.Mix = apvts.getRawParameterValue("DryWet")->load();
...

```

With

```auto
struct ChainSettings {
    float LoCutFreq{ 0 }, PeakFreq{ 0 }, PeakGain{ 0 }, PeakQ{ 0 }, HiCutFreq{ 0 }, PreGain{ 0 }, Bias{ 0 }, WaveShapeAmount{ 0 }, PostGain{ 0 }, Mix{ 0 };
    int LoCutSlope{ FilterSlope::Slope12 }, HiCutSlope{ FilterSlope::Slope12 }, WaveShapeFunction{ WaveShaperFunction::HardClip };
};

```

and `FilterSlope`, `WaveShaperFunction` as basic `enum`s.

I can see from my logs, that FL calls `getState` and then `setState`. In the `getState`, all values are the default ones I specified when I generate the ValueTree (so not the `struct` defaults) and the subsequent `setState` has the values that _should_ have been loaded (so that’s right at least).  
But it seems to be using the default values in the “`ChainSettings`” `struct`. I should also mention that the _correct_ parameters are shown in the UI immediately, even before the re-opening that makes the Processor load its parameters.

One other thing I’ve noticed: when you re-open the interface and the parameters are right and everything starts working - my Choice-type parameters do NOT get loaded correctly. But this happens in AudioPluginHost as well so it’s more likely I screwed up - interestingly however, on the first load in FL, before the plugin starts working again, the Choices _get_ loaded correctly - only when reopening (so that it actually works) are they reset to standard.

Now sadly, I can’t attach a debugger to FL, most likely because they want to protect their software - whenever I want to attach, it immediately closes. Whether I go via Debug-\>Attach or the Project Properties and Debug Command, FL closes immediately as VS tries to attach to it.  
I’ve resorted to writing to a text file as some kind of DBG output log.

_This must be a total newbie question, but I’ve absolutely run into a wall here, with my very limited knowledge of JUCE and C++ in general._

---

<div class="post-metadata">

### Author: ![crowbait](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@crowbait](https://forum.juce.com/u/crowbait)
#### Post date: [August 29, 2023, 10:51am UTC](https://forum.juce.com/t/plugin-loaded-with-no-state-in-fl-21/57600/2 "2023-08-29T10:51:27Z")

</div>

I’ve been logging the way in which FL calls some functions with some select parameter’s values logged as well (Gain is in dB):

```auto
createParameterLayout
getStateInformation
got state
LoCutFreq 20 - PreGain 0 - WaveShapeAmt 0 - WaveShapeFunc 0
editor costructor 
setStateInformation
LoCutFreq 357 - PreGain 19 - WaveShapeAmt 0.99 - WaveShapeFunc 3
prepareToPlay
LoCutFreq 357 - PreGain 19 - WaveShapeAmt 0.99 - WaveShapeFunc 3
prepareToPlay
LoCutFreq 357 - PreGain 19 - WaveShapeAmt 0.99 - WaveShapeFunc 3
prepareToPlay
LoCutFreq 357 - PreGain 19 - WaveShapeAmt 0.99 - WaveShapeFunc 3

```

I don’t know why it calls `prepareToPlay`multiple times.  
Also, even though the editor is constructed before setting the state, it shown the correct values in all regards (including the Choice parameters).  
When I re-open the GUI to make things work, it only calls `editor constructor` again, with no further reading or setting of states.

I have also tried to use the generic editor; it’s even worse: it behaves the same after startup BUT does not even start to work after re-opening the editor.

---

<div class="post-metadata">

### Author: ![crowbait](https://avatars.discourse-cdn.com/v4/letter/c/c2a13f/32.png) [@crowbait](https://forum.juce.com/u/crowbait)
#### Post date: [August 29, 2023, 12:15pm UTC](https://forum.juce.com/t/plugin-loaded-with-no-state-in-fl-21/57600/3 "2023-08-29T12:15:29Z")

</div>

I feel incredibly stupid.

I had a Gain processor that was not properly fed a gain value `unless` you set the “WaveshaperFunction” to it’s default value. Since Gains get initialized with a default (linear) gain of `0`, it produced no sound.

However, re-opening the editor in FL re-set all Choice type parameters to their defaults and that circumvented that issue.

Now there only remains the question _why_ FL resets those parameters when opening the editor.  
That also was my stupidity at play. I falsely assumed that the editor’s constructor would be called _once_, which is not the case. It gets called every time the GUI is opened. So when I set the ComboBox’s default value as `WaveshapeSelect.setSelectedId(1);` (0 being “nothing selected”), I overruled the actual value every time. The correct approach is `WaveshapeSelect.setSelectedId(audioProcessor.apvts.getRawParameterValue("WaveShapeFunction")->load() + 1);`.

So, I had to bugs that partly mitigated each other. I hope someone might find this helpful in the future.
