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
| scripts | An array of all script instances attached to an entity. |
Methods
| create | Create a script instance using name of a pc.ScriptType and attach to an entity script component. |
| destroy | Destroy the script instance that is attached to an entity. |
| has | Detect if script is attached to an entity using name of pc.ScriptType. |
| move | Move 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. |
| create | Fired when a script instance is created and attached to component. |
| destroy | Fired when a script instance is destroyed and removed from component. |
| disable | Fired when Component becomes disabled Note: this event does not take in account entity or any of its parent enabled state. |
| enable | Fired when Component becomes enabled Note: this event does not take in account entity or any of its parent enabled state. |
| error | Fired when a script instance had an exception. |
| move | Fired when a script instance is moved in component. |
| remove | Fired when Component is removed from entity. |
| state | Fired when Component changes state to enabled or disabled Note: this event does not take in account entity or any of its parent enabled state. |
| move:[name] | Fired when a script instance is moved in component. |
Inherited
Properties
| enabled | Enables or disables the component. |
| entity | The Entity that this Component is attached to. |
| system | The ComponentSystem used to create this Component. |
Methods
| fire | Fire an event, all additional arguments are passed on to the event listener. |
| hasEvent | Test if there are any handlers bound to an event name. |
| off | Detach an event handler from an event. |
| on | Attach an event handler to an event. |
| once | Attach an event handler to an event. |
Details
Constructor
ScriptComponent(system, entity)
Parameters
| system | pc.ScriptComponentSystem | The ComponentSystem that created this Component. |
| entity | pc.Entity | The Entity that this Component is attached to. |
Properties
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 pc.ScriptType and attach to an entity script component.
entity.script.create('playerController', {
attributes: {
speed: 4
}
});
Parameters
| name | string, typeof pc.ScriptType | The name of the Script Type (or alternatively the pc.ScriptType to instantiate). |
| args | object | Object with arguments for a script. |
| args.enabled | boolean | If script instance is enabled after creation. Defaults to true. |
| args.attributes | object | Object with values for attributes (if any), where key is name of an attribute. |
| args.preloading | boolean | If script instance is created during preload. If true, script and attributes must be initialized manually. Defaults to false. |
| args.ind | number | The index where to insert the script instance at. Defaults to -1, which means append it at the end. |
Returns
pc.ScriptTypeReturns an instance of a pc.ScriptType if successfully attached to an entity, or null if it failed because a script with a same name has already been added or if the pc.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
| name | string | The name of the Script Type. |
Returns
booleanIf it was successfully destroyed.
has(name)
Detect if script is attached to an entity using name of pc.ScriptType.
if (entity.script.has('playerController')) {
// entity has script
}
Parameters
| name | string | The name of the Script Type. |
Returns
booleanIf 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.move('playerController', 0);
Parameters
| name | string | The name of the Script Type. |
| ind | number | New position index. |
Returns
booleanIf it was successfully moved.
Events
create:[name]
Fired when a script instance is created and attached to component.
entity.script.on('create:playerController', function (scriptInstance) {
// new script instance 'playerController' is added to component
});
Parameters
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been created. |
destroy:[name]
Fired when a script instance is destroyed and removed from component.
entity.script.on('destroy:playerController', function (scriptInstance) {
// script instance 'playerController' has been destroyed and removed from component
});
Parameters
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been destroyed. |
create
Fired when a script instance is created and attached to component.
entity.script.on('create', function (name, scriptInstance) {
// new script instance added to component
});
Parameters
| name | string | The name of the Script Type. |
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been created. |
destroy
Fired when a script instance is destroyed and removed from component.
entity.script.on('destroy', function (name, scriptInstance) {
// script instance has been destroyed and removed from component
});
Parameters
| name | string | The name of the Script Type. |
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been destroyed. |
disable
Fired when Component becomes disabled Note: this event does not take in account entity or any of its parent enabled state.
entity.script.on('disable', function () {
// component is disabled
});
enable
Fired when Component becomes enabled Note: this event does not take in account entity or any of its parent enabled state.
entity.script.on('enable', function () {
// component is enabled
});
error
Fired when a script instance had an exception.
entity.script.on('error', function (scriptInstance, err, method) {
// script instance caught an exception
});
Parameters
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that raised the exception. |
| err | Error | Native JS Error object with details of an error. |
| method | string | The method of the script instance that the exception originated from. |
move
Fired when a script instance is moved in component.
entity.script.on('move', function (name, scriptInstance, ind, indOld) {
// script instance has been moved in component
});
Parameters
| name | string | The name of the Script Type. |
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been moved. |
| ind | number | New position index. |
| indOld | number | Old position index. |
remove
Fired when Component is removed from entity.
entity.script.on('remove', function () {
// entity has no more script component
});
state
Fired when Component changes state to enabled or disabled Note: this event does not take in account entity or any of its parent enabled state.
entity.script.on('state', function (enabled) {
// component changed state
});
Parameters
| enabled | boolean | True if now enabled, False if disabled. |
move:[name]
Fired when a script instance is moved in component.
entity.script.on('move:playerController', function (scriptInstance, ind, indOld) {
// script instance 'playerController' has been moved in component
});
Parameters
| scriptInstance | pc.ScriptType | The instance of the pc.ScriptType that has been moved. |
| ind | number | New position index. |
| indOld | number | Old position index. |
Inherited
Properties
Methods
fire(name, [arg1], [arg2], [arg3], [arg4], [arg5], [arg6], [arg7], [arg8])
Fire an event, all additional arguments are passed on to the event listener.
obj.fire('test', 'This is the message');
Parameters
| name | object | Name of event to fire. |
| arg1 | * | First argument that is passed to the event handler. |
| arg2 | * | Second argument that is passed to the event handler. |
| arg3 | * | Third argument that is passed to the event handler. |
| arg4 | * | Fourth argument that is passed to the event handler. |
| arg5 | * | Fifth argument that is passed to the event handler. |
| arg6 | * | Sixth argument that is passed to the event handler. |
| arg7 | * | Seventh argument that is passed to the event handler. |
| arg8 | * | Eighth argument that is passed to the event handler. |
Returns
pc.EventHandlerSelf for chaining.
hasEvent(name)
Test if there are any handlers bound to an event name.
obj.on('test', function () { }); // bind an event to 'test'
obj.hasEvent('test'); // returns true
obj.hasEvent('hello'); // returns false
Parameters
| name | string | The name of the event to test. |
Returns
booleanTrue if the object has handlers bound to the specified event name.
off([name], [callback], [scope])
Detach an event handler from an event. If callback is not provided then all callbacks are unbound from the event, if scope is not provided then all events with the callback will be unbound.
var handler = function () {
};
obj.on('test', handler);
obj.off(); // Removes all events
obj.off('test'); // Removes all events called 'test'
obj.off('test', handler); // Removes all handler functions, called 'test'
obj.off('test', handler, this); // Removes all hander functions, called 'test' with scope this
Parameters
| name | string | Name of the event to unbind. |
| callback | pc.callbacks.HandleEvent | Function to be unbound. |
| scope | object | Scope that was used as the this when the event is fired. |
Returns
pc.EventHandlerSelf for chaining.
on(name, callback, [scope])
Attach an event handler to an event.
obj.on('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
Parameters
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.
once(name, callback, [scope])
Attach an event handler to an event. This handler will be removed after being fired once.
obj.once('test', function (a, b) {
console.log(a + b);
});
obj.fire('test', 1, 2); // prints 3 to the console
obj.fire('test', 1, 2); // not going to get handled
Parameters
| name | string | Name of the event to bind the callback to. |
| callback | pc.callbacks.HandleEvent | Function that is called when event is fired. Note the callback is limited to 8 arguments. |
| scope | object | Object to use as 'this' when the event is fired, defaults to current this. |
Returns
pc.EventHandlerSelf for chaining.