API Reference

Class List

pc.ScriptComponent

Extends: pc.Component

The ScriptComponent allows you to extend the functionality of an Entity by attaching your own Script Types defined in javascript files to be executed with access to the Entity. For more details on scripting see Scripting.

Summary

Properties

scriptsAn array of all script instances attached to an entity.

Methods

createCreate a script instance using name of a ScriptType and attach to an entity script component.
destroyDestroy the script instance that is attached to an entity.
hasDetect if script is attached to an entity using name of ScriptType.
moveMove script instance to different position to alter update order of scripts within entity.

Events

create:[name]Fired when a script instance is created and attached to component
destroy:[name]Fired when a script instance is destroyed and removed from component
createFired when a script instance is created and attached to component
destroyFired when a script instance is destroyed and removed from component
disableFired when Component becomes disabled Note: this event does not takes in account entity or any of its parent enabled state
enableFired when Component becomes enabled Note: this event does not takes in account entity or any of its parent enabled state
errorFired when a script instance had an exception
removeFired when Component is removed from entity
stateFired when Component changes state to enabled or disabled Note: this event does not takes in account entity or any of its parent enabled state

Inherited

Properties

enabledEnables or disables the component.

Details

Constructor

ScriptComponent(system, entity)

Parameters

systempc.ScriptComponentSystemThe ComponentSystem that created this Component
entitypc.EntityThe Entity that this Component is attached to.

Properties

ScriptType[]scripts

An array of all script instances attached to an entity. This Array shall not be modified by developer.

Methods

create(name, [args])

Create a script instance using name of a ScriptType and attach to an entity script component.

entity.script.create('playerController', {
    attributes: {
        speed: 4
    }
});

Parameters

nameStringThe name of the Script Type
argsObjectObject with arguments for a script
args.enabledBooleanif script instance is enabled after creation
args.attributesObjectObject with values for attributes, where key is name of an attribute

Returns

ScriptType Returns an instance of a ScriptType if successfuly attached to an entity, or null if it failed because a script with a same name has already been added or if the ScriptType cannot be found by name in the pc.ScriptRegistry.

destroy(name)

Destroy the script instance that is attached to an entity.

entity.script.destroy('playerController');

Parameters

nameStringThe name of the Script Type

Returns

Boolean If it was successfuly destroyed

has(name)

Detect if script is attached to an entity using name of ScriptType.

if (entity.script.has('playerController')) {
    // entity has script
}

Parameters

nameStringThe name of the Script Type

Returns

Boolean If script is attached to an entity

move(name, ind)

Move script instance to different position to alter update order of scripts within entity.

entity.script.destroy('playerController');

Parameters

nameStringThe name of the Script Type
indNumberNew position index

Returns

Boolean If it was successfuly moved

Events

create:[name]

Fired when a script instance is created and attached to component

Parameters

scriptInstanceScriptTypeThe instance of the ScriptType that has been created

destroy:[name]

Fired when a script instance is destroyed and removed from component

Parameters

scriptInstanceScriptTypeThe instance of the ScriptType that has been destroyed

create

Fired when a script instance is created and attached to component

Parameters

nameStringThe name of the Script Type
scriptInstanceScriptTypeThe instance of the ScriptType that has been created

destroy

Fired when a script instance is destroyed and removed from component

Parameters

nameStringThe name of the Script Type
scriptInstanceScriptTypeThe instance of the ScriptType that has been destroyed

disable

Fired when Component becomes disabled Note: this event does not takes in account entity or any of its parent enabled state

enable

Fired when Component becomes enabled Note: this event does not takes in account entity or any of its parent enabled state

error

Fired when a script instance had an exception

Parameters

scriptInstanceScriptTypeThe instance of the ScriptType that raised the exception
errErrorNative JS Error object with details of an error
methodStringThe method of the script instance that the exception originated from.

remove

Fired when Component is removed from entity

state

Fired when Component changes state to enabled or disabled Note: this event does not takes in account entity or any of its parent enabled state

Parameters

enabledBooleanTrue if now enabled, False if disabled

Inherited

Properties

Booleanenabled

Enables or disables the component.