# Calling order of setStateInformation(...) and prepareToPlay(...)?

**URL:** <https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834>\
**Category:** Audio Plugins\
**Created:** [August 1, 2018, 11:00am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834 "2018-08-01T11:00:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![parkellipsen](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@parkellipsen](https://forum.juce.com/u/parkellipsen)\
**Post date:** [August 1, 2018, 11:00am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/1 "2018-08-01T11:00:48Z")

</div>

I’m currently trying to restore a plugin state.

It seems like in Reaper the first call to prepareToPlay(…) is done before setStateInformation(…), so the necessary information to initialize my plugin isn’t present yet …

Is this the standard behaviour in all DAWs, or is the order different in others ?

Best,  
n

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [August 1, 2018, 11:04am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/2 "2018-08-01T11:04:35Z")

</div>

This is valid behaviour. Your plugin should open with a default state (which could be uninitialised) and then the host will call `setStateInformation` at some point in the future.

Basically you have to be able to handle anything as a plugin…

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [August 1, 2018, 12:18pm UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/3 "2018-08-01T12:18:47Z")

</div>

> [@parkellipsen](#):
>
> Is this the standard behaviour in all DAWs, or is the order different in others ?

unfortunately there is no standard behaviour.

---

<div class="post-metadata">

**Author:** ![pizzafilms](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/pizzafilms/32/562_2.png) [@pizzafilms](https://forum.juce.com/u/pizzafilms)\
**Post date:** [August 1, 2018, 3:25pm UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/4 "2018-08-01T15:25:53Z")

</div>

Here’s another odd behavior from Cubase that you should be aware of as well:

> [@Cubase calling setCurrentProgram after setStateInformation](https://forum.juce.com/t/cubase-calling-setcurrentprogram-after-setstateinformation/21637):
>
> I’m making sure that get/set stateInformation is working properly for my plugin in every host I can and it is in everything…except Cubase on the Mac. For whatever reason, it seems that Cubase is calling setCurrentProgram after setStateInformation when it loads a project with my plugin in it, and therefore destroying the state that the project was saved in. I tested other plugins and they seem work properly, but not mine…and only in Cubase. Does anyone have any clues here? Thanks.

---

<div class="post-metadata">

**Author:** ![parkellipsen](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@parkellipsen](https://forum.juce.com/u/parkellipsen)\
**Post date:** [August 3, 2018, 7:27am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/5 "2018-08-03T07:27:36Z")

</div>

Hmm so basically, there’s no way to tell whether a plugin is just a added to a DAW session or restored as part of a DAW session at construction time. In the first case, the setStateInformation isn’t called at all in Reaper.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [August 3, 2018, 8:25am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/6 "2018-08-03T08:25:41Z")

</div>

It kind of makes sense for the host to call setStateInformation at the beginning to make sure, the plugin is in the state as it would be when it is restored. But it is not a requirement, so the evidence works one way:

- If it is not called, it is definitely not restored (given the host implements save and restore 😉 )
- If it is called, it could be either way, like dave said, be prepared for both

---

<div class="post-metadata">

**Author:** ![xenakios](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/xenakios/32/4047_2.png) [@xenakios](https://forum.juce.com/u/xenakios)\
**Post date:** [August 3, 2018, 8:49am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/7 "2018-08-03T08:49:41Z")

</div>

> [@parkellipsen](#):
>
> In the first case, the setStateInformation isn’t called at all in Reaper.

Which should not matter since your plugin should have some kind of a default state anyway. (Set up in the constructor and/or member initializers) Why would you need to set that up later in setStateInformation?

---

<div class="post-metadata">

**Author:** ![dave96](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/dave96/32/8487_2.png) [@dave96](https://forum.juce.com/u/dave96)\
**Post date:** [August 3, 2018, 8:58am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/8 "2018-08-03T08:58:01Z")

</div>

> [@parkellipsen](#):
>
> Hmm so basically, there’s no way to tell whether a plugin is just a added to a DAW session or restored as part of a DAW session at construction time.

Why would you need to know this? Does it determine some behaviour of your plugin? If so, you may need to rethink this as, yes, it’s not really knowable.

> [@parkellipsen](#):
>
> In the first case, the setStateInformation isn’t called at all in Reaper

What state would it set?

Remember there’s situations where you might be a _new instance_ but _have state_, such as during a copy/paste operation.

TL;DR, you need to be able to deal with anything at any time…

---

<div class="post-metadata">

**Author:** ![parkellipsen](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@parkellipsen](https://forum.juce.com/u/parkellipsen)\
**Post date:** [August 3, 2018, 10:24am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/9 "2018-08-03T10:24:57Z")

</div>

Well, it’s complicated … my current project requires to share information with the world outside the DAW, and restore a state based on an ID stored in the plugin’s memory block … so a default state would be rather useless in that context.

I’m thinking about a stateless solution, but so far i can’t think of any …

---

<div class="post-metadata">

**Author:** ![parkellipsen](https://avatars.discourse-cdn.com/v4/letter/p/7993a0/32.png) [@parkellipsen](https://forum.juce.com/u/parkellipsen)\
**Post date:** [August 3, 2018, 10:25am UTC](https://forum.juce.com/t/calling-order-of-setstateinformation-and-preparetoplay/28834/10 "2018-08-03T10:25:52Z")

</div>

Hmm the copy and paste thing is a good point … i have to think about that …
