Skip to main content

ScriptComponent

Class

Used to bind custom JavaScript or TypeScript code to specific Lens events for dynamic interactivity.

  • Available on all platforms
Platform details: Snapchat, SPECS, Camera Kit Web, Camera Kit Mobile fully supported.

Platform details

  • Snapchat — Fully supported.

  • SPECS — Fully supported.

  • Camera Kit Web — Fully supported.

  • Camera Kit Mobile — Fully supported.

Used to bind custom JavaScript or TypeScript code to specific Lens events for dynamic interactivity. Top level contents of a JS file will run as an OnAwakeEvent.

Index

Remarks

These Script Components are attached to SceneObjects, providing the ability to modify properties and behaviors of those objects or others within the scene. Script Components expose input fields in the Inspector panel, allowing for customization of script behavior without altering code. Any script can access the ScriptComponent executing them through the variable script.

Example

// Bind a function to the MouthOpened event
function onMouthOpen(eventData)
{
print("mouth was opened");
}
var event = script.createEvent("MouthOpenedEvent");
event.bind(onMouthOpen);

Extends

Indexable

[index: string]: any

Properties

enabled

enabled: boolean

If disabled, the Component will stop enacting its behavior.

Default Value

true

Inherited from

Component.enabled


isBeingDestroyed

readonly isBeingDestroyed: boolean

Returns true if this Component is currently being destroyed. Becomes true as soon as destruction begins via Component#destroy or destruction of its owning SceneObject, before any OnDestroyEvent callbacks fire.

Since

Lens Scripting Version 364

Example

if (!script.isBeingDestroyed) {
// Component is still fully alive
}

Inherited from

Component.isBeingDestroyed


isEnabledInHierarchy

readonly isEnabledInHierarchy: boolean

Returns true if this Component, its SceneObject, and all of that SceneObjects parents are enabled.

Inherited from

Component.isEnabledInHierarchy


sceneObject

readonly sceneObject: SceneObject

The SceneObject this component is on.

Inherited from

Component.sceneObject


uniqueIdentifier

readonly uniqueIdentifier: string

Since

Lens Scripting Version 176

Inherited from

Component.uniqueIdentifier


updateOrder

readonly updateOrder: number

The relative execution order of this script's lifecycle callbacks (onStart, onUpdate, onLateUpdate, etc.) among all scripts in the Lens. Scripts with a smaller updateOrder run before scripts with a larger updateOrder. Reflects the updateOrder set on the attached ScriptAsset. Defaults to 0.

Remarks

updateOrder only orders scripts relative to each other; it does not affect ordering relative to non-script engine Components.

Since

Lens Scripting Version 367

Example

if (script.updateOrder < 0) {
// This script runs before scripts at the default updateOrder.
}

Methods

createEvent()

Call Signature

createEvent<K>(eventType): EventNameMap[K]

Adds a new SceneEvent, triggered by eventType events, to the ScriptComponent.

Type Parameters
Type Parameter
K extends keyof EventNameMap
Parameters
ParameterType
eventTypeK
Returns

EventNameMap[K]

Call Signature

createEvent<T>(eventType): T

Adds a new SceneEvent, triggered by eventType events, to the ScriptComponent.

Type Parameters
Type Parameter
T extends SceneEvent
Parameters
ParameterType
eventTypeTypeName<T>
Returns

T


destroy()

destroy(): void

Destroys the component.

Returns

void

Inherited from

Component.destroy


getReferencedEvents()

getReferencedEvents(): SceneEvent[]

Returns the list of SceneEvents referenced by this ScriptComponent.

Returns

SceneEvent[]

Since

Lens Scripting Version 354


getSceneObject()

getSceneObject(): SceneObject

Returns the SceneObject the component is attached to.

Returns

SceneObject

Inherited from

Component.getSceneObject


getTransform()

getTransform(): Transform

Returns the Transform this component is attached to.

Returns

Transform

Inherited from

Component.getTransform


getTypeName()

static getTypeName(): TypeName<ScriptComponent>

Returns the fully-qualified JavaScript type name for this class (for example, "Component.Camera"). This is a static accessor (no instance required) and returns the same value as the instance getTypeName().

Returns

TypeName<ScriptComponent>

Overrides

Component.getTypeName


getTypeName()

getTypeName(): string

Returns the name of this object's type.

Returns

string

Inherited from

Component.getTypeName


isOfType()

isOfType(type): boolean

Returns true if the object matches or derives from the passed in type.

Parameters

ParameterType
typestring

Returns

boolean

Inherited from

Component.isOfType


isSame()

isSame(other): boolean

Returns true if this object is the same as other. Useful for checking if two references point to the same thing.

Parameters

ParameterType
otherScriptObject

Returns

boolean

Inherited from

Component.isSame


removeEvent()

removeEvent(event?): void

Removes a previously added SceneEvent from the ScriptComponent.

Parameters

ParameterType
event?SceneEvent

Returns

void


requestPersistInput()

requestPersistInput(inputName): void

Requests Lens Studio to persist the named @input into the project model. Only has effect in the Scene Editor viewport.

Call after mutating the input from script when the change should persist. Will only persist if this ScriptComponent is still alive at the end of the frame.

Parameters

ParameterType
inputNamestring

Returns

void

Since

Lens Scripting Version 378

Example

script.topLeft = handle.getTransform().getLocalPosition();
script.requestPersistInput("topLeft");

See also