# Lua exports

> Place, schedule and remove fireworks from a FiveM server resource and inspect local particle statistics.

Use the server exports to control fireworks from a DJ script, event schedule or other FiveM resource. Add the dependency to the calling resource so Fireworks starts first. [FiveM Lua exports](https://docs.fivem.net/docs/scripting-manual/runtimes/lua/#using-exports)

```lua [fxmanifest.lua]
dependency "sky_fireworks"
```

## Server exports

<table>
<thead>
  <tr>
    <th>
      Export
    </th>
    
    <th>
      Success
    </th>
    
    <th>
      Behaviour
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        GetFireworkCatalog()
      </code>
    </td>
    
    <td>
      Array of entries
    </td>
    
    <td>
      Each entry has <code>
        preset
      </code>
      
      , <code>
        item
      </code>
      
      , <code>
        kind
      </code>
      
      , <code>
        label
      </code>
      
       and <code>
        shots
      </code>
      
      . Use a returned preset or item ID when spawning.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        SpawnFirework(preset, coords, options?)
      </code>
    </td>
    
    <td>
      Article ID
    </td>
    
    <td>
      Places an unlit article. Pass a <code>
        vector3
      </code>
      
       or <code>
        { x, y, z }
      </code>
      
       at the base of the model. Optional <code>
        heading
      </code>
      
       is in degrees and <code>
        bucket
      </code>
      
       defaults to 0.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        GetFirework(id)
      </code>
    </td>
    
    <td>
      Article table
    </td>
    
    <td>
      Returns <code>
        id
      </code>
      
      , <code>
        preset
      </code>
      
      , <code>
        coords
      </code>
      
      , <code>
        heading
      </code>
      
      , <code>
        bucket
      </code>
      
       and <code>
        status
      </code>
      
      . After ignition it includes <code>
        runId
      </code>
      
       and <code>
        startsInMs
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        IgniteFirework(id, options?)
      </code>
    </td>
    
    <td>
      Run ID
    </td>
    
    <td>
      Schedules one article. Optional <code>
        delayMs
      </code>
      
       sets its first cue offset and <code>
        fuse = true
      </code>
      
       plays the in-game fuse where supported.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        IgniteFireworks(cues, options?)
      </code>
    </td>
    
    <td>
      Run ID
    </td>
    
    <td>
      Takes an array of <code>
        { id, delayMs? }
      </code>
      
       entries for the same routing bucket. Equal delays start together. Optional <code>
        fuse = true
      </code>
      
       applies to the whole sequence.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        GetFireworkShow(runId)
      </code>
    </td>
    
    <td>
      Run table
    </td>
    
    <td>
      Returns <code>
        id
      </code>
      
      , <code>
        ids
      </code>
      
      , <code>
        bucket
      </code>
      
      , <code>
        status
      </code>
      
      , <code>
        startsInMs
      </code>
      
       and <code>
        endsInMs
      </code>
      
      .
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        DeleteFirework(id)
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Removes an unlit article or stops a run with a single article.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        StopFireworkShow(runId)
      </code>
    </td>
    
    <td>
      <code>
        true
      </code>
    </td>
    
    <td>
      Cancels a scheduled run or stops an active one, including every article in that run.
    </td>
  </tr>
</tbody>
</table>

Article status is `placed`, `scheduled` or `running`. Run status is `scheduled` until the first cue and `running` until playback ends. Both IDs are strings and expire at the end of playback, on removal or when their owning resource stops. Each resource can access and control its own IDs. API placements stream to nearby players in the selected routing bucket without changing player inventories.

Delays are whole milliseconds and default to 0. The sequence can last up to 30 minutes within the playback budget. Fireworks adds a short synchronization lead. Fuses and large sequences can add more lead time. Read `startsInMs` from the run right after ignition to line up music or lighting with its first cue. Roman candles and Bengal flares from the catalogue play as stationary effects.

### Start a timed sequence

Call this from your server-side cue handler. Supply the actual base coordinates of each article and the event's routing bucket. The two fountains start together, followed by the battery 2.5 seconds later.

```lua [server.lua]
local fireworks = exports["sky_fireworks"]

local function fireDisplay(left, right, center, bucket)
    local placements = {
        { preset = "goldfountain", coords = left, delayMs = 0 },
        { preset = "silverfan", coords = right, delayMs = 0 },
        { preset = "sky_firework_signalnine", coords = center, delayMs = 2500 },
    }

    local cues = {}
    local function clearPlacements()
        for _, cue in ipairs(cues) do
            fireworks:DeleteFirework(cue.id)
        end
    end

    for _, placement in ipairs(placements) do
        local id, reason = fireworks:SpawnFirework(placement.preset, placement.coords, { bucket = bucket })
        if not id then
            clearPlacements()
            return false, reason
        end
        cues[#cues + 1] = { id = id, delayMs = placement.delayMs }
    end

    local runId, reason = fireworks:IgniteFireworks(cues)
    if not runId then clearPlacements() end
    return runId, reason
end
```

### Handle a rejected call

An unsuccessful call returns `false, reason`. A rejected ignition leaves the placed articles available for retry or removal.

<table>
<thead>
  <tr>
    <th>
      Error
    </th>
    
    <th>
      Meaning
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        invalid_caller
      </code>
      
      , <code>
        not_found
      </code>
    </td>
    
    <td>
      The call has no owning server resource, or the ID is unknown, expired or belongs to another resource.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        invalid_preset
      </code>
      
      , <code>
        invalid_coords
      </code>
      
      , <code>
        invalid_options
      </code>
    </td>
    
    <td>
      Use a preset from the catalogue, valid world coordinates and supported option types.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        invalid_cues
      </code>
      
      , <code>
        invalid_delay
      </code>
      
      , <code>
        duplicate_id
      </code>
    </td>
    
    <td>
      Supply a dense array with one entry per article and a whole delay in milliseconds.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        already_ignited
      </code>
      
      , <code>
        part_of_show
      </code>
    </td>
    
    <td>
      The article already belongs to a run. Use <code>
        StopFireworkShow
      </code>
      
       to cancel a run containing several articles.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        different_bucket
      </code>
      
      , <code>
        different_area
      </code>
    </td>
    
    <td>
      A sequence needs one routing bucket and one site. Every article must be within 1,000 metres of its first listed article.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        limit
      </code>
      
      , <code>
        busy
      </code>
    </td>
    
    <td>
      The placement cap or the shared playback budget was reached. Try a smaller sequence, stagger its launches or wait for current runs to finish.
    </td>
  </tr>
</tbody>
</table>

## particleStats()

`exports["sky_fireworks"]:particleStats()` takes no arguments and returns a table of numeric statistics for the local player's Fireworks renderer. Read it on demand during a show to compare distance, effect density and quality.

```lua [client.lua]
local stats = exports["sky_fireworks"]:particleStats()

print(("Fireworks native handles: %d (peak %d), failed starts: %d"):format(
    stats.active, stats.native_peak, stats.native_start_failures
))
```

<table>
<thead>
  <tr>
    <th>
      Fields
    </th>
    
    <th>
      Meaning
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        active
      </code>
      
      , <code>
        active_primary
      </code>
      
      , <code>
        native_peak
      </code>
    </td>
    
    <td>
      Active native handles, active primary handles and the peak active handle count.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        visible_batteries
      </code>
      
      , <code>
        native_batteries
      </code>
    </td>
    
    <td>
      Nearby active sources considered by the native budget, including the sources currently allocated native detail.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        native_start_failures
      </code>
      
      , <code>
        native_backoff
      </code>
      
      , <code>
        native_configured_budget_limited
      </code>
      
      , <code>
        battery_limited
      </code>
    </td>
    
    <td>
      Failed native starts and skips due to backoff or budget.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        native_configured_budget
      </code>
      
      , <code>
        native_quality
      </code>
      
      , <code>
        sampled_frame_ms
      </code>
    </td>
    
    <td>
      Configured native budget, current local quality multiplier and smoothed client frame time in milliseconds.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        started
      </code>
      
      , <code>
        removed
      </code>
      
      , <code>
        expired
      </code>
      
      , <code>
        culled
      </code>
      
      , <code>
        late
      </code>
      
      , <code>
        fallback
      </code>
    </td>
    
    <td>
      Native stage starts, removal and expiry, visibility or timing skips, and alternative rendering.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        rise_started
      </code>
      
      , <code>
        rise_limited
      </code>
      
      , <code>
        launch_limited
      </code>
      
      , <code>
        burst_limited
      </code>
      
      , <code>
        ground_limited
      </code>
      
      , <code>
        fuse_limited
      </code>
    </td>
    
    <td>
      Stage-specific counters for rise, launch, burst, ground and fuse effects.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        visual_prepares
      </code>
      
      , <code>
        visual_limited_jobs
      </code>
      
      , <code>
        visual_skipped_jobs
      </code>
      
      , <code>
        visual_frustum_culled
      </code>
      
      , <code>
        visual_peak_triangles
      </code>
      
      , <code>
        smoke_light_samples
      </code>
    </td>
    
    <td>
      World-space render work, limits, view culling, peak triangle count and lighting samples.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        show_cleanup_batches
      </code>
      
      , <code>
        largest_show_cleanup
      </code>
    </td>
    
    <td>
      Cleanup count and the largest cleanup batch.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        timed_crackle_clouds
      </code>
      
      , <code>
        timed_crackle_onsets
      </code>
      
      , <code>
        timed_crackle_nests
      </code>
      
      , <code>
        timed_crackle_max_rise
      </code>
      
      , <code>
        timed_crackle_max_drop
      </code>
      
      , <code>
        timed_crackle_max_drift
      </code>
      
      , <code>
        timed_crackle_peak_triangles
      </code>
    </td>
    
    <td>
      Measurements for timed world-space crackle effects.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        relit_bouquets
      </code>
      
      , <code>
        brocade_segments
      </code>
      
      , <code>
        filtered_brocade_grains
      </code>
      
      , <code>
        willow_dust_prepared
      </code>
      
      , <code>
        willow_dust_drawn
      </code>
    </td>
    
    <td>
      Additional counters for authored spark, trail and dust effects.
    </td>
  </tr>
  
  <tr>
    <td>
      <code>
        crackle_active
      </code>
      
      , <code>
        crackle_started
      </code>
      
      , <code>
        crackle_limited
      </code>
      
      , <code>
        crackle_peak
      </code>
    </td>
    
    <td>
      Legacy native crackle counters. The current renderer returns zero for these fields.
    </td>
  </tr>
</tbody>
</table>

Some counters reset when the renderer starts a new playback session after its owned stages clear. Read the export during comparable shots. Its counters describe **Fireworks only**. Use the [performance guide](/docs/scripts/fireworks/features/sound-and-streaming#audio-and-visual-load) for shared pool and audio checks.

## Record the local mix

The Fireworks audio page provides a JavaScript diagnostic helper for listening checks. Select the `sky_fireworks` frame in FiveM NUI Developer Tools to use this example. [FiveM NUI Developer Tools](https://docs.fivem.net/docs/scripting-manual/nui-development/full-screen-nui/#developer-tools)

```js
const take = await recordFireworksMix(12);
console.log(take.sampleRate, take.startFrame, take.paddedFrames, take.gaps);
```

The duration is in seconds and must be greater than 0 and at most 90. The Promise returns the local stereo WAV in `take.wav` together with the sample rate, starting frame and gap information. You can pass an optional `AbortSignal` as the second argument. This is a local NUI helper.
