# registerScript

Function · category: Script

Source: https://github.com/playcanvas/engine/blob/f059dc005842f76e052cdf44d8370c8c7ec475ac/src/framework/script/script-create.js#L113

```ts
registerScript(script: typeof ScriptType, name?: string, app?: AppBase): void
```

Register an existing class type as a Script Type with [ScriptRegistry](https://api.playcanvas.com/engine/classes/ScriptRegistry.md). Useful when defining
an ES6 script class that extends [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md) (see example).

**Parameters**

- `script` (`typeof` [`ScriptType`](https://api.playcanvas.com/engine/classes/ScriptType.md)): The existing class type (constructor function) to be
  registered as a Script Type. Class must extend [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md) (see example). Please note: A
  class created using [createScript](https://api.playcanvas.com/engine/functions/createScript.md) is auto-registered, and should therefore not be passed
  into [registerScript](https://api.playcanvas.com/engine/functions/registerScript.md) (which would result in swapping out all related script instances).
- `name` (`string`, optional): 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.
- `app` ([`AppBase`](https://api.playcanvas.com/engine/classes/AppBase.md), optional): Optional application handler, to choose which [ScriptRegistry](https://api.playcanvas.com/engine/classes/ScriptRegistry.md)
  to register the script type with. By default it will use `Application.getApplication()` to get
  the current [AppBase](https://api.playcanvas.com/engine/classes/AppBase.md).

**Example**

```ts
// define an ES6 script class
class PlayerController extends ScriptType {

    initialize() {
        // called once on initialize
    }

    update(dt) {
        // called each tick
    }
}

// register the class as a script
registerScript(PlayerController);

// declare script attributes (Must be after registerScript())
PlayerController.attributes.add('attribute1', {type: 'number'});
```
