Skip to content

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.

A simple function call when the script unloads.

ac.onRelease(function()
ac.log("Script is shutting down")
end)

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.

ParameterTypeRequiredDescription
callbackfunctionYesThe function to run on release. It receives the item as its first argument.
itemanyNoAn optional object to pass to the callback, stored using a weak reference.

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)