# Creating Custom Actions

***

## Using the Action Creator Tool

1. Open **Tools > Action Creator** in the Unity menu
2. Enter an action name (e.g., `SpawnParticle`)
3. Select or enter a category
4. Choose the output folder (defaults to the standard package actions path)
5. Click **Create Action**

***

## Deleting a Custom Action Script

The Action Creator window includes a **Delete Action Script** section that safely removes a custom action from your project.

**Steps:**

1. Open **Tools > Action Creator**
2. Expand the **Delete Action Script** section
3. Assign the script asset you want to remove
4. Click **Delete Action Script**

**What it does:**

* Scans the entire project for usages of the action type
* Removes all `[SerializeReference]` entries for that type from **scenes**, **prefabs**, and **ActionFlowPreset** assets — both via `SerializedObject` API and direct YAML cleanup
* Deletes the script asset from disk
* Shows a progress bar with error handling throughout

> **Note:** This operation is irreversible. Back up your project or use source control before proceeding.

***

## Manual Creation

Create a new C# script inheriting from `Action`:

```csharp
using System;
using System.Collections;
using UnityEngine;
using FlowOfAction;
using BeeLabs.Attributes;

namespace FlowOfAction.Actions
{
    [Serializable]
    [ActionCategory("Custom/Effects")]
    public class SpawnParticle : Action
    {
        [Foldout("Settings", true)]
        [SerializeField] private ParticleSystem _particlePrefab;

        [Foldout("Settings")]
        [SerializeField] private Transform _spawnPoint;

        [Foldout("Settings")]
        [SerializeField] private bool _waitForCompletion;

        protected override IEnumerator DoAction()
        {
            if (_particlePrefab == null || _spawnPoint == null)
            {
                UnityEngine.Debug.LogWarning("SpawnParticle: Missing references.");
                yield break;
            }

            var instance = UnityEngine.Object.Instantiate(
                _particlePrefab,
                _spawnPoint.position,
                _spawnPoint.rotation
            );

            instance.Play();

            if (_waitForCompletion)
            {
                yield return new WaitForSeconds(instance.main.duration);
                UnityEngine.Object.Destroy(instance.gameObject);
            }
            else
            {
                yield return null;
            }
        }
    }
}
```

***

## Action Lifecycle

| Method                          | When called                                                |
| ------------------------------- | ---------------------------------------------------------- |
| `Initialize(FlowPlayer player)` | When FlowPlayer initializes. Override to cache references. |
| `DoAction()` (coroutine)        | Performs the action. Must yield at least once.             |
| `Stop()`                        | When the FlowPlayer is stopped. Override for cleanup.      |

***

## Accessing FlowPlayer

```csharp
protected override IEnumerator DoAction()
{
    FlowPlayer player = Player;       // owning FlowPlayer
    GameObject go = Player.gameObject;

    yield return null;
}
```

***

## Using Cached WaitForSeconds

For better performance, use the built-in cached wait methods:

```csharp
protected override IEnumerator DoAction()
{
    // Uses cached WaitForSeconds based on _delay field
    yield return GetCachedDelay();

    // Uses cached WaitForSeconds based on _duration field
    yield return GetCachedDuration();
}
```


---

# 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 dynamically by asking a question.

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

```
GET https://beelabs-dev.gitbook.io/beelabs-docs/flow-of-action/creating-custom-actions.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.
