# Load state vs. default state flow

**URL:** <https://forum.juce.com/t/load-state-vs-default-state-flow/45787>\
**Category:** Audio Plugins\
**Created:** [May 1, 2021, 10:21am UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787 "2021-05-01T10:21:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![schnabel](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@schnabel](https://forum.juce.com/u/schnabel)\
**Post date:** [May 1, 2021, 10:21am UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787/1 "2021-05-01T10:21:21Z")

</div>

Hello,

I’ve a plugin that loads samples. I have some parameters and depending on that it loads different samples. I store those parameter like described in this [tutorial](https://docs.juce.com/master/tutorial_audio_processor_value_tree_state.html).

My problem: The function that loads the saved state can trigger early or late, I think it depends on the DAW. So this is a problem of “load defaults” vs. “load saved state” when the plugin starts.

My current implementation is like this:

The function that loads the samples is called `PluginProcessor::loadSamples(...)`

In the function that triggers when a state is loaded (`setStateInformation`) I call the `loadSamples` function. So in case the DAW has a stored state if goes that flow and it loads everything successfully.

But when you add the plugin to a new project in a DAW, there should be an initial/default state so it loads the samples. In this case, the `setStateInformation` will not trigger because there’s nothing stored. SO for this case I have the following code in my `PluginProcessor::prepareToPlay` function:

```
auto asyncFn = [&]() { 
    juce::Thread::sleep(200);   
    if (!samplesAlreadyLoaded) {
        loadSamples(...);
    }
};
std::thread(asyncFn).detach();

```

This seems to work, but isn’t that hacky as hell? I mean, this is just something like a fallback in case nothing has been loaded and if so, then it will load the samples with the default parameters.

I have to know the parameters before I load the samples, because depending on the parameters it will be different sample files…

What is the best practice to handle this case? I’d like to get rid of that dirty code. Because my code is not efficient if the default-flow (waits 200ms) and it also can fail if the load-state-flow is slower than 200ms … I guess you know what I mean.

Any hints are welcome 🙂  
Have a nice weekend

---

<div class="post-metadata">

**Author:** ![imagiro](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/imagiro/32/17746_2.png) [@imagiro](https://forum.juce.com/u/imagiro)\
**Post date:** [January 29, 2022, 7:29pm UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787/2 "2022-01-29T19:29:45Z")

</div>

I’ve encountered the same problem and have ended up with the same solution - from what I can tell there’s no way to know if a DAW is going to load a plugin state until it does 🤷

---

<div class="post-metadata">

**Author:** ![HowardAntares](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/howardantares/32/4629_2.png) [@HowardAntares](https://forum.juce.com/u/HowardAntares)\
**Post date:** [January 29, 2022, 11:31pm UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787/3 "2022-01-29T23:31:02Z")

</div>

Why not always load the default samples, immediately after creating your parameters and setting their default values, and then if setStateInformation() happens to be called later, load the samples based on that state information? That seems more logical to me.

---

<div class="post-metadata">

**Author:** ![Rincewind](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/rincewind/32/10645_2.png) [@Rincewind](https://forum.juce.com/u/Rincewind)\
**Post date:** [January 30, 2022, 8:56am UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787/4 "2022-01-30T08:56:32Z")

</div>

Could you give some examples of DAWs that work and DAWs that don‘t work? I was recently working with the Kontakt AU and Kontakt refused to load anything in setState when it is not actually playing.

---

<div class="post-metadata">

**Author:** ![Mrugalla](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mrugalla/32/13092_2.png) [@Mrugalla](https://forum.juce.com/u/Mrugalla)\
**Post date:** [January 30, 2022, 2:16pm UTC](https://forum.juce.com/t/load-state-vs-default-state-flow/45787/5 "2022-01-30T14:16:37Z")

</div>

the usual way to do this is to let the parameters have a default value themselves. some hosts, like JUCE’ standalone build, will call setStateInformation regardless, but then you can just say if the valueTree has no state for that parameter yet do nothing. some DAWs, like cubase, will only call setStateInformation if there has been state before. Also make sure to never save something to or load from your valueTree in the constructor of the audioProcessor cause it will be overriden shortly later in setStateInformation anyway
