ScriptComponent
Extends: 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 and attach to an entity script component. |
destroy | Destroy the script instance that is attached to an entity. |
get | Get a script instance (if attached). |
has | Detect if script is attached to an entity. |
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. |
enable | Fired when Component becomes enabled. |
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. |
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)
Create a new ScriptComponent instance.
Parameters
system | ScriptComponentSystem | The ComponentSystem that created this Component. |
entity | Entity | The Entity that this Component is attached to. |
Properties
An array of all script instances attached to an entity. This array is read-only and should not be modified by developer.
Methods
create(nameOrType, [args])
Create a script instance and attach to an entity script component.
entity.script.create('playerController', {
attributes: {
speed: 4
}
});
Parameters
nameOrType | string, typeof(ScriptType) | The name or type of ScriptType. |
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
ScriptType, nullReturns an instance of a 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 ScriptType cannot be found by name in the ScriptRegistry.
destroy(nameOrType)
Destroy the script instance that is attached to an entity.
entity.script.destroy('playerController');
Parameters
nameOrType | string, typeof(ScriptType) | The name or type of ScriptType. |
Returns
booleanIf it was successfully destroyed.
get(nameOrType)
Get a script instance (if attached).
const controller = entity.script.get('playerController');
Parameters
nameOrType | string, typeof(ScriptType) | The name or type of ScriptType. |
Returns
ScriptType, nullIf script is attached, the instance is returned. Otherwise null is returned.
has(nameOrType)
Detect if script is attached to an entity.
if (entity.script.has('playerController')) {
// entity has script
}
Parameters
nameOrType | string, typeof(ScriptType) | The name or type of ScriptType. |
Returns
booleanIf script is attached to an entity.
move(nameOrType, ind)
Move script instance to different position to alter update order of scripts within entity.
entity.script.move('playerController', 0);
Parameters
nameOrType | string, typeof(ScriptType) | The name or type of ScriptType. |
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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | ScriptType | The instance of the 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 | string | 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
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.
const 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 handler functions, called 'test' with scope this
Parameters
name | string | Name of the event to unbind. |
callback | HandleEventCallback | Function to be unbound. |
scope | object | Scope that was used as the this when the event is fired. |
Returns
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 | HandleEventCallback | 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
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 | HandleEventCallback | 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
EventHandlerSelf for chaining.