API Reference

Class List

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 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

systemScriptComponentSystem

The ComponentSystem that created this Component.

entityEntity

The 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(nameOrType, [args])

Create a script instance and attach to an entity script component.

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

Parameters

nameOrTypestring, typeof(ScriptType)

The name or type of ScriptType.

argsobject

Object with arguments for a script.

args.enabledboolean

If script instance is enabled after creation. Defaults to true.

args.attributesobject

Object with values for attributes (if any), where key is name of an attribute.

args.preloadingboolean

If script instance is created during preload. If true, script and attributes must be initialized manually. Defaults to false.

args.indnumber

The index where to insert the script instance at. Defaults to -1, which means append it at the end.

Returns

ScriptType

Returns 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

nameOrTypestring, typeof(ScriptType)

The name or type of ScriptType.

Returns

boolean

If it was successfully destroyed.

get(nameOrType)

Get a script instance (if attached).

var controller = entity.script.get('playerController');

Parameters

nameOrTypestring, typeof(ScriptType)

The name or type of ScriptType.

Returns

ScriptType, null

If 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

nameOrTypestring, typeof(ScriptType)

The name or type of ScriptType.

Returns

boolean

If 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

nameOrTypestring, typeof(ScriptType)

The name or type of ScriptType.

indnumber

New position index.

Returns

boolean

If 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

scriptInstanceScriptType

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

scriptInstanceScriptType

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

namestring

The name of the Script Type.

scriptInstanceScriptType

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

namestring

The name of the Script Type.

scriptInstanceScriptType

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

scriptInstanceScriptType

The instance of the ScriptType that raised the exception.

errError

Native JS Error object with details of an error.

methodstring

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

namestring

The name of the Script Type.

scriptInstanceScriptType

The instance of the ScriptType that has been moved.

indnumber

New position index.

indOldnumber

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

enabledboolean

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

scriptInstanceScriptType

The instance of the ScriptType that has been moved.

indnumber

New position index.

indOldnumber

Old position index.

Inherited

Properties

booleanenabled

Enables or disables the component.

Entityentity

The Entity that this Component is attached to.

ComponentSystemsystem

The ComponentSystem used to create this Component.

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

nameobject

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

EventHandler

Self 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

namestring

The name of the event to test.

Returns

boolean

True 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

namestring

Name of the event to unbind.

callbackcallbacks.HandleEvent

Function to be unbound.

scopeobject

Scope that was used as the this when the event is fired.

Returns

EventHandler

Self 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

namestring

Name of the event to bind the callback to.

callbackcallbacks.HandleEvent

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandler

Self 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

namestring

Name of the event to bind the callback to.

callbackcallbacks.HandleEvent

Function that is called when event is fired. Note the callback is limited to 8 arguments.

scopeobject

Object to use as 'this' when the event is fired, defaults to current this.

Returns

EventHandler

Self for chaining.