# 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: 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://docs.openbrush.app/user-guide/using-plugins/writing-plugins/writing-a-background-plugin.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.
