# ScriptAttributes

Class · category: Script

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

Container of Script Attribute definitions. Implements an interface to add/remove attributes and
store their definition for a [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md). Note: An instance of ScriptAttributes is
created automatically by each [ScriptType](https://api.playcanvas.com/engine/classes/ScriptType.md).

## Constructors

### constructor

```ts
new ScriptAttributes(scriptType: typeof ScriptType)
```

Create a new ScriptAttributes instance.

**Parameters**

- `scriptType` (`typeof` [`ScriptType`](https://api.playcanvas.com/engine/classes/ScriptType.md)): Script Type that attributes relate to.

## Methods

### add

```ts
add(name: string, args: object): void
```

Add Attribute.

**Parameters**

- `name` (`string`): Name of an attribute.
- `args` (`object`): Object with Arguments for an attribute.
    - `args.array` (`boolean`, optional): If attribute can hold single or multiple values.
    - `args.assetType` (`string`, optional): Name of asset type to be used in 'asset' type attribute
      picker in Editor's UI, defaults to '*' (all).
    - `args.color` (`string`, optional): String of color channels for Curves for field type 'curve',
      can be any combination of `rgba` characters. Defining this property will render Gradient in
      Editor's field UI.
    - `args.curves` (`string[]`, optional): List of names for Curves for field type 'curve'.
    - `args.default` (`any`, optional): Default attribute value.
    - `args.description` (`string`, optional): Description for Editor's for field UI.
    - `args.enum` (`any[]`, optional): List of fixed choices for field, defined as array of objects,
      where key in object is a title of an option.
    - `args.max` (`number`, optional): Maximum value for type 'number', if max and min defined, slider
      will be rendered in Editor's UI.
    - `args.min` (`number`, optional): Minimum value for type 'number', if max and min defined, slider
      will be rendered in Editor's UI.
    - `args.placeholder` (`string | string[]`, optional): Placeholder for Editor's for field UI.
      For multi-field types, such as vec2, vec3, and others use array of strings.
    - `args.precision` (`number`, optional): Level of precision for field type 'number' with floating
      values.
    - `args.schema` (`any[]`, optional): List of attributes for type 'json'. Each attribute
      description is an object with the same properties as regular script attributes but with an
      added 'name' field to specify the name of each attribute in the JSON.
    - `args.size` (`number`, optional): If attribute is array, maximum number of values can be set.
    - `args.step` (`number`, optional): Step value for type 'number'. The amount used to increment the
      value when using the arrow keys in the Editor's UI.
    - `args.title` (`string`, optional): Title for Editor's for field UI.
    - `args.type` (`"string" | "number" | "boolean" | "rgb" | "curve" | "json" | "vec2" | "vec3" | "vec4" | "entity" | "asset" | "rgba"`): Type
      of an attribute value. Can be:

      - "asset"
      - "boolean"
      - "curve"
      - "entity"
      - "json"
      - "number"
      - "rgb"
      - "rgba"
      - "string"
      - "vec2"
      - "vec3"
      - "vec4"

**Example**

```ts
PlayerController.attributes.add('fullName', {
    type: 'string'
});
```

**Example**

```ts
PlayerController.attributes.add('speed', {
    type: 'number',
    title: 'Speed',
    placeholder: 'km/h',
    default: 22.2
});
```

**Example**

```ts
PlayerController.attributes.add('resolution', {
    type: 'number',
    default: 32,
    enum: [
        { '32x32': 32 },
        { '64x64': 64 },
        { '128x128': 128 }
    ]
});
```

**Example**

```ts
PlayerController.attributes.add('config', {
    type: 'json',
    schema: [{
        name: 'speed',
        type: 'number',
        title: 'Speed',
        placeholder: 'km/h',
        default: 22.2
    }, {
        name: 'resolution',
        type: 'number',
        default: 32,
        enum: [
            { '32x32': 32 },
            { '64x64': 64 },
            { '128x128': 128 }
        ]
    }]
});
```

### get

```ts
get(name: string): any
```

Get object with attribute arguments. Note: Changing argument properties will not affect
existing Script Instances.

**Parameters**

- `name` (`string`): Name of an attribute.

**Returns** `any`: Arguments with attribute properties.

**Example**

```ts
// changing default value for an attribute 'fullName'
var attr = PlayerController.attributes.get('fullName');
if (attr) attr.default = 'Unknown';
```

### has

```ts
has(name: string): boolean
```

Detect if Attribute is added.

**Parameters**

- `name` (`string`): Name of an attribute.

**Returns** `boolean`: True if Attribute is defined.

**Example**

```ts
if (PlayerController.attributes.has('fullName')) {
    // attribute fullName is defined
}
```

### remove

```ts
remove(name: string): boolean
```

Remove Attribute.

**Parameters**

- `name` (`string`): Name of an attribute.

**Returns** `boolean`: True if removed or false if not defined.

**Example**

```ts
PlayerController.attributes.remove('fullName');
```
