# ScriptRegistry

Class · extends [`EventHandler`](https://api.playcanvas.com/engine/classes/EventHandler.md) · category: Script

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

Container for all [Script](https://api.playcanvas.com/engine/classes/Script.md) classes that are available to this application. Note that
PlayCanvas scripts can access the Script Registry from inside the application with
[AppBase#scripts](https://api.playcanvas.com/engine/classes/AppBase.md#scripts).

## Constructors

### constructor

```ts
new ScriptRegistry(app: AppBase)
```

Create a new ScriptRegistry instance.

**Parameters**

- `app` ([`AppBase`](https://api.playcanvas.com/engine/classes/AppBase.md)): Application to attach registry to.

## Methods

### add

```ts
add(script: typeof Script): boolean
```

Add a script to the registry, keyed by its name. The name is taken from the script's static
`scriptName` property (for [Script](https://api.playcanvas.com/engine/classes/Script.md) classes), or assigned by [createScript](https://api.playcanvas.com/engine/functions/createScript.md) /
[registerScript](https://api.playcanvas.com/engine/functions/registerScript.md). Note: when [createScript](https://api.playcanvas.com/engine/functions/createScript.md) or [registerScript](https://api.playcanvas.com/engine/functions/registerScript.md) is called,
the script is added to the registry automatically, so calling this method directly is only
required when registering a [Script](https://api.playcanvas.com/engine/classes/Script.md) class manually (e.g. in an engine-only project).

If a script with the same name already exists in the registry, and the new script has a
`swap` method defined, it will perform code hot swapping automatically in an async manner.

**Parameters**

- `script` (`typeof` [`Script`](https://api.playcanvas.com/engine/classes/Script.md)): The script class to add. Must have a
  resolvable name (a static `scriptName`, an assigned `__name`, or an inferable class name).

**Returns** `boolean`: True if the script was added for the first time. False if a script with
the same name already exists, or if the script has no resolvable name.

**Example**

```ts
var PlayerController = createScript('playerController');
// playerController Script Type will be added to ScriptRegistry automatically
console.log(app.scripts.has('playerController')); // outputs true
```

**Example**

```ts
// engine-only: register an ESM Script class manually
class Rotator extends Script {
    static scriptName = 'rotator';
}
app.scripts.add(Rotator);
console.log(app.scripts.has('rotator')); // outputs true
```

### addSchema

```ts
addSchema(id: string, schema: AttributeSchema): void
```

Registers a schema against a script instance.

**Parameters**

- `id` (`string`): The key to use to store the schema
- `schema` ([`AttributeSchema`](https://api.playcanvas.com/engine/interfaces/AttributeSchema.md)): An schema definition for the script

### get

```ts
get(name: string): typeof Script | null
```

Get a [Script](https://api.playcanvas.com/engine/classes/Script.md) class by name.

**Parameters**

- `name` (`string`): Name of the [Script](https://api.playcanvas.com/engine/classes/Script.md).

**Returns** `typeof` [`Script`](https://api.playcanvas.com/engine/classes/Script.md) `| null`: The script class if it exists in the registry or null
otherwise.

**Example**

```ts
var PlayerController = app.scripts.get('playerController');
```

### getSchema

```ts
getSchema(id: string): AttributeSchema | undefined
```

Returns a schema for a given script name.

**Parameters**

- `id` (`string`): The key to store the schema under

**Returns** [`AttributeSchema`](https://api.playcanvas.com/engine/interfaces/AttributeSchema.md) `| undefined`: - The schema stored under the key

### has

```ts
has(nameOrType: string | typeof Script): boolean
```

Check if a [Script](https://api.playcanvas.com/engine/classes/Script.md) class with the specified name is in the registry.

**Parameters**

- `nameOrType` (`string | typeof` [`Script`](https://api.playcanvas.com/engine/classes/Script.md)): The name or class of the [Script](https://api.playcanvas.com/engine/classes/Script.md).

**Returns** `boolean`: True if the [Script](https://api.playcanvas.com/engine/classes/Script.md) class is in the registry.

**Example**

```ts
if (app.scripts.has('playerController')) {
    // playerController is in ScriptRegistry
}
```

### list

```ts
list(): typeof Script[]
```

Get list of all [Script](https://api.playcanvas.com/engine/classes/Script.md) classes from registry.

**Returns** `typeof` [`Script`](https://api.playcanvas.com/engine/classes/Script.md)`[]`: list of all [Script](https://api.playcanvas.com/engine/classes/Script.md) classes in registry.

**Example**

```ts
// logs array of all Script Type names available in registry
console.log(app.scripts.list().map(function (o) {
    return o.name;
}));
```

### remove

```ts
remove(nameOrType: string | typeof Script): boolean
```

Remove a [Script](https://api.playcanvas.com/engine/classes/Script.md) class from the registry.

**Parameters**

- `nameOrType` (`string | typeof` [`Script`](https://api.playcanvas.com/engine/classes/Script.md)): The name or class of the [Script](https://api.playcanvas.com/engine/classes/Script.md).

**Returns** `boolean`: True if removed or False if already not in registry.

**Example**

```ts
app.scripts.remove('playerController');
```

## Inherited from [EventHandler](https://api.playcanvas.com/engine/classes/EventHandler.md)

- `fire(name: string, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any): EventHandler`
- `hasEvent(name: string): boolean`
- `off(name?: string, callback?: HandleEventCallback, scope?: any): EventHandler`
- `on(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
- `once(name: string, callback: HandleEventCallback, scope?: any): EventHandle`
