# Save and Load State

Continuing with this simple example, we add another useful capability - saving the state of the controller to the project, and loading it back when the project file is opened.

You may want to save the state of your controller directly in the project so the next time the user opens the project, any state information is reloaded.

To do this:

1. Keep all information that needs to be saved and loaded in one JavaScript object.
2. Whenever something changes in these settings, save it out using the [`saveSettings`](/captivate-api/javascript-api-reference/using-the-servicehandler/additional-methods-with-scheduler.md#savesettings-method) command.
3. On startup, retrieve the input settings using the [`loadSettings`](/captivate-api/javascript-api-reference/using-the-servicehandler/additional-methods-with-scheduler.md#loadsettings-method) command.

In this example, the JavaScript object that manages all of the state information is called “settings.” Saving the state the code looks like this:

```javascript
// Save this latest state in the project.
scheduler.saveSettings(inputName, JSON.stringify(settings));
```

This should be called any time the settings change, typically when the user makes a change in the UI.

To retrieve the settings, call `loadSettings` from the project when the `ServiceHandler` connects:

```javascript
// On startup, retrieve the input settings from the project.
scheduler.loadSettings(inputName, (projectSettings) => {
  // Convert into a JavaScript object.
  let newSettings = JSON.parse(projectSettings);
  // ... apply the settings to the UI
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://developers.newbluefx.com/api-examples/sample-controllers-tour/03-save-and-load-state.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
