> For the complete documentation index, see [llms.txt](https://docs.openbrush.app/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.openbrush.app/user-guide/using-plugins/writing-plugins/writing-a-background-plugin.md).

# Writing a Background Plugin

Background Plugins run all the time when activated. They are useful for animating layers or continually generating new shapes. You can still check for user actions such as pressing the trigger and change the action accordingly. They are very versatile but because they run continuously in they background, they can affect performance (especially if you run several at the same time).

Always use one of the other script types in preference if it is more suitable.

Unlike other plugin types, Background Plugins don't expect you to return a value and they don't do anything with any values you do return.

In contrast other plugin types use return values as follows:

1. Pointer Plugins use the returned value to change the pointer position, rotation or scale.
2. Symmetry Plugins use the returned values to create multiple new pointers.
3. Tool Plugins can use the returned value as the path of a new brush stroke which is drawn immediately.

You can of course draw in a Background Plugin but it's left to you to do so explicitly:

```lua
function Main()
    if Brush.triggerPressedThisFrame then
        myPath = Path:New()
        position = getRandomPosition()
        myPath:Insert(Transform:New(position))
        myPath:Insert(Transform:New(position))
        myPath:Draw()
    end
end

function getRandomPosition()
    return Brush.position + Random.insideUnitSphere
end
```

There are some potentially new details to be aware of here:

1. We are defining our own function to generate a random position around the current brush position
2. We are using the `Random` class to generate a point that is never more than a certain distance away from the origin. We then add that to the current brush position
3. We are manually drawing the path with `myPath:Draw()`


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.openbrush.app/user-guide/using-plugins/writing-plugins/writing-a-background-plugin.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
