onRelease
Registers a function to be executed when the script is stopped. This is primarily used to clean up resources, stop sounds, or revert state changes before the script terminates.
Usage Patterns
Section titled “Usage Patterns”A simple function call when the script unloads.
ac.onRelease(function() ac.log("Script is shutting down")end)Pass an object to the callback. If the object is garbage collected before the script unloads, the callback will not run.
local myData = { id = 123 }
ac.onRelease(function(item) ac.log("Cleaning up for item ID: " .. item.id)end, myData)Description
Section titled “Description”The ac.onRelease function allows you to hook into the script lifecycle.
When you save a file in your editor, CSP triggers a hot-reload which unloads the current script instance before starting the new one.
This function is the designated place to reset any modifications made to the simulation, such as emissive materials or active UI elements.
If you provide an item as the second argument, CSP stores it with a weak reference. This ensures that the documentation doesn’t accidentally prevent the object from being garbage collected. If the object naturally leaves memory during script execution, the associated cleanup callback is automatically discarded.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
callback | function | Yes | The function to run on release. It receives the item as its first argument. |
item | any | No | An optional object to pass to the callback, stored using a weak reference. |
Examples
Section titled “Examples”Ensuring a car mesh stops glowing when the script reloads or the session ends.
local meshes = ac.findMeshes("EXT_LIGHT_GLOW")meshes:setMaterialProperty("ksEmissive", 1.0)
ac.onRelease(function() -- reset the emissive property so it doesn't stay stuck 'on' meshes:setMaterialProperty("ksEmissive", 0.0)end)Stopping an active audio event to prevent it from looping after the script unloads.
local sound = ac.AudioEvent("event:/cars/my_car/horn")sound:start()
ac.onRelease(function() -- stop and dispose of the audio resource sound:dispose()end)