# createScript

Function · category: Script

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/framework/script/script-create.js#L48

```ts
createScript(name: string, app?: AppBase): typeof ScriptType | null
```

Create and register a new [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md). It returns new class type (constructor function),
which is auto-registered to [ScriptRegistry](https://api.playcanvas.com/engine/classes/ScriptRegistry.md) 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.

**Parameters**

- `name` (`string`): 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, worker.
- `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 add a script to. By default it will use `Application.getApplication()` to get current
  [AppBase](https://api.playcanvas.com/engine/classes/AppBase.md).

**Returns** `typeof` [`ScriptType`](https://api.playcanvas.com/engine/classes/ScriptType.md) `| null`: A class type (constructor function) that inherits [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md),
which the developer is meant to further extend by adding attributes and prototype methods.
Returns null if there was an error.

**Example**

```ts
var Turning = 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);
};
```
