/** * @import { ScriptComponentSystem } from './system.js' * @import { Script } from '../../script/script.js' */ /** * The ScriptComponent enables an {@link Entity} to have custom behavior by attaching scripts * written in JavaScript (or TypeScript). * * You should never need to use the ScriptComponent constructor directly. To add a * ScriptComponent to an {@link Entity}, use {@link Entity#addComponent}: * * ```javascript * const entity = new Entity(); * entity.addComponent('script'); * ``` * * Once the ScriptComponent is added to the entity, you can access it via the * {@link Entity#script} property: * * ```javascript * // Option 1: Add a script using the name registered in the ScriptRegistry * entity.script.create('cameraControls'); * * // Option 2: Add a script using the script class * entity.script.create(CameraControls); * ``` * * For more details on scripting see the [Scripting Section](https://developer.playcanvas.com/user-manual/scripting/) * of the User Manual. * * @hideconstructor * @category Script */ export class ScriptComponent extends Component { /** * Fired when a {@link Script} instance is created and attached to the script component. * This event is available in two forms. They are as follows: * * 1. `create` - Fired when a script instance is created. The name of the script type and the * script type instance are passed as arguments. * 2. `create:[name]` - Fired when a script instance is created that has the specified script * type name. The script instance is passed as an argument to the handler. * * @event * @example * entity.script.on('create', (name, scriptInstance) => { * console.log(`Instance of script '${name}' created`); * }); * @example * entity.script.on('create:player', (scriptInstance) => { * console.log(`Instance of script 'player' created`); * }); */ static EVENT_CREATE: string; /** * Fired when a {@link Script} instance is destroyed and removed from the script component. * This event is available in two forms. They are as follows: * * 1. `destroy` - Fired when a script instance is destroyed. The name of the script type and * the script type instance are passed as arguments. * 2. `destroy:[name]` - Fired when a script instance is destroyed that has the specified * script type name. The script instance is passed as an argument. * * @event * @example * entity.script.on('destroy', (name, scriptInstance) => { * console.log(`Instance of script '${name}' destroyed`); * }); * @example * entity.script.on('destroy:player', (scriptInstance) => { * console.log(`Instance of script 'player' destroyed`); * }); */ static EVENT_DESTROY: string; /** * Fired when the script component becomes enabled. This event does not take into account the * enabled state of the entity or any of its ancestors. * * @event * @example * entity.script.on('enable', () => { * console.log(`Script component of entity '${entity.name}' has been enabled`); * }); */ static EVENT_ENABLE: string; /** * Fired when the script component becomes disabled. This event does not take into account the * enabled state of the entity or any of its ancestors. * * @event * @example * entity.script.on('disable', () => { * console.log(`Script component of entity '${entity.name}' has been disabled`); * }); */ static EVENT_DISABLE: string; /** * Fired when the script component has been removed from its entity. * * @event * @example * entity.script.on('remove', () => { * console.log(`Script component removed from entity '${entity.name}'`); * }); */ static EVENT_REMOVE: string; /** * Fired when the script component changes state to enabled or disabled. The handler is passed * the new boolean enabled state of the script component. This event does not take into account * the enabled state of the entity or any of its ancestors. * * @event * @example * entity.script.on('state', (enabled) => { * console.log(`Script component of entity '${entity.name}' changed state to '${enabled}'`); * }); */ static EVENT_STATE: string; /** * Fired when the index of a {@link Script} instance is changed in the script component. * This event is available in two forms. They are as follows: * * 1. `move` - Fired when a script instance is moved. The name of the script type, the script * type instance, the new index and the old index are passed as arguments. * 2. `move:[name]` - Fired when a specifically named script instance is moved. The script * instance, the new index and the old index are passed as arguments. * * @event * @example * entity.script.on('move', (name, scriptInstance, newIndex, oldIndex) => { * console.log(`Script '${name}' moved from index '${oldIndex}' to '${newIndex}'`); * }); * @example * entity.script.on('move:player', (scriptInstance, newIndex, oldIndex) => { * console.log(`Script 'player' moved from index '${oldIndex}' to '${newIndex}'`); * }); */ static EVENT_MOVE: string; /** * Fired when a {@link Script} instance had an exception. The handler is passed the script * instance, the exception and the method name that the exception originated from. * * @event * @example * entity.script.on('error', (scriptInstance, exception, methodName) => { * console.log(`Script error: ${exception} in method '${methodName}'`); * }); */ static EVENT_ERROR: string; /** * Create a new ScriptComponent instance. * * @param {ScriptComponentSystem} system - The ComponentSystem that created this Component. * @param {Entity} entity - The Entity that this Component is attached to. */ constructor(system: ScriptComponentSystem, entity: Entity); /** * A map of script name to initial component data. * * @type {Map} * @private */ private _attributeDataMap; /** * Holds all script instances for this component. * * @type {Script[]} * @private */ private _scripts; _updateList: SortedLoopArray; _postUpdateList: SortedLoopArray; _scriptsIndex: {}; _declarationOrder: any[]; _destroyedScripts: any[]; _destroyed: boolean; _scriptsData: readonly Script[]; _oldState: boolean; _beingEnabled: boolean; _isLoopingThroughScripts: boolean; _executionOrder: number; /** * Sets the array of all script instances attached to an entity. This array is read-only and * should not be modified by developer. * * @type {Script[]} */ set scripts(value: ReadonlyArray