UNPKG

playcanvas

Version:

PlayCanvas WebGL game engine

83 lines (82 loc) 4.05 kB
/** * Create and register a new {@link ScriptType}. It returns new class type (constructor function), * which is auto-registered to {@link ScriptRegistry} using its name. This is the main interface to * create Script Types, to define custom logic using JavaScript, that is used to create interaction * for entities. * * @param {string} name - Unique Name of a Script Type. If a Script Type with the same name has * already been registered and the new one has a `swap` method defined in its prototype, then it * will perform hot swapping of existing Script Instances on entities using this new Script Type. * Note: There is a reserved list of names that cannot be used, such as list below as well as some * starting from `_` (underscore): system, entity, create, destroy, swap, move, scripts, onEnable, * onDisable, onPostStateChange, has, on, off, fire, once, hasEvent. * @param {AppBase} [app] - Optional application handler, to choose which {@link ScriptRegistry} * to add a script to. By default it will use `Application.getApplication()` to get current * {@link AppBase}. * @returns {typeof ScriptType|null} A class type (constructor function) that inherits {@link ScriptType}, * which the developer is meant to further extend by adding attributes and prototype methods. * Returns null if there was an error. * @example * var Turning = pc.createScript('turn'); * * // define 'speed' attribute that is available in Editor UI * Turning.attributes.add('speed', { * type: 'number', * default: 180, * placeholder: 'deg/s' * }); * * // runs every tick * Turning.prototype.update = function (dt) { * this.entity.rotate(0, this.speed * dt, 0); * }; * @category Script */ export function createScript(name: string, app?: AppBase): typeof ScriptType | null; export namespace createScript { export { reservedAttributes }; } /** * Register a existing class type as a Script Type to {@link ScriptRegistry}. Useful when defining * a ES6 script class that extends {@link ScriptType} (see example). * * @param {typeof ScriptType} script - The existing class type (constructor function) to be * registered as a Script Type. Class must extend {@link ScriptType} (see example). Please note: A * class created using {@link createScript} is auto-registered, and should therefore not be pass * into {@link registerScript} (which would result in swapping out all related script instances). * @param {string} [name] - Optional unique name of the Script Type. By default it will use the * same name as the existing class. If a Script Type with the same name has already been registered * and the new one has a `swap` method defined in its prototype, then it will perform hot swapping * of existing Script Instances on entities using this new Script Type. Note: There is a reserved * list of names that cannot be used, such as list below as well as some starting from `_` * (underscore): system, entity, create, destroy, swap, move, scripts, onEnable, onDisable, * onPostStateChange, has, on, off, fire, once, hasEvent. * @param {AppBase} [app] - Optional application handler, to choose which {@link ScriptRegistry} * to register the script type to. By default it will use `Application.getApplication()` to get * current {@link AppBase}. * @example * // define a ES6 script class * class PlayerController extends pc.ScriptType { * * initialize() { * // called once on initialize * } * * update(dt) { * // called each tick * } * } * * // register the class as a script * pc.registerScript(PlayerController); * * // declare script attributes (Must be after pc.registerScript()) * PlayerController.attributes.add('attribute1', {type: 'number'}); * @category Script */ export function registerScript(script: typeof ScriptType, name?: string, app?: AppBase): void; export function getReservedScriptNames(): Set<string>; import { AppBase } from '../app-base.js'; import { ScriptType } from './script-type.js'; declare const reservedAttributes: {}; export {};