The Script class is the fundamental base class for all scripts within PlayCanvas. It provides the minimal interface required for a script to be compatible with both the Engine and the Editor.

At its core, a script is simply a collection of methods that are called at various points in the Engine's lifecycle. These methods are:

Script#initialize - Called once when the script is initialized Script#postInitialize - Called once after all scripts have been initialized Script#update - Called every frame, if the script is enabled Script#postUpdate - Called every frame, after all scripts have been updated Script#swap - Called when a script is redefined

These methods are entirely optional, but provide a useful way to manage the lifecycle of a script and perform any necessary setup and cleanup.

Below is a simple example of a script that rotates an entity every frame.

class EntityRotator extends Script {
update() {
this.entity.rotateLocal(0, 1, 0);
}
}

When this script is attached to an entity, the update will be called every frame, slowly rotating the entity around the Y-axis.

For more information on how to create scripts, see the Scripting Overview.

Hierarchy (view full)

Constructors

Properties

app: AppBase

The AppBase that the instance of this type belongs to.

entity: Entity

The Entity that the instance of this type belongs to.

Accessors

  • set enabled(value): void
  • True if the instance of this type is in running state. False when script is not running, because the Entity or any of its parents are disabled or the ScriptComponent is disabled or the Script Instance is disabled. When disabled no update methods will be called on each tick. initialize and postInitialize methods will run once when the script instance is in enabled state during app tick.

    Parameters

    • value: boolean

    Returns void

Methods

  • Fire an event, all additional arguments are passed on to the event listener.

    Parameters

    • name: string

      Name of event to fire.

    • Optionalarg1: any

      First argument that is passed to the event handler.

    • Optionalarg2: any

      Second argument that is passed to the event handler.

    • Optionalarg3: any

      Third argument that is passed to the event handler.

    • Optionalarg4: any

      Fourth argument that is passed to the event handler.

    • Optionalarg5: any

      Fifth argument that is passed to the event handler.

    • Optionalarg6: any

      Sixth argument that is passed to the event handler.

    • Optionalarg7: any

      Seventh argument that is passed to the event handler.

    • Optionalarg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

    Self for chaining.

    obj.fire('test', 'This is the message');
    
  • Test if there are any handlers bound to an event name.

    Parameters

    • name: string

      The name of the event to test.

    Returns boolean

    True if the object has handlers bound to the specified event name.

    obj.on('test', function () { }); // bind an event to 'test'
    obj.hasEvent('test'); // returns true
    obj.hasEvent('hello'); // returns false
  • 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.

    Parameters

    • Optionalname: string

      Name of the event to unbind.

    • Optionalcallback: HandleEventCallback

      Function to be unbound.

    • Optionalscope: object

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

    Returns EventHandler

    Self for chaining.

    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
  • Attach an event handler to an event.

    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.

    • Optionalscope: object = ...

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

    Returns EventHandle

    Can be used for removing event in the future.

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console
    const evt = obj.on('test', function (a, b) {
    console.log(a + b);
    });
    // some time later
    evt.off();
  • Attach an event handler to an event. This handler will be removed after being fired once.

    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.

    • Optionalscope: object = ...

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

    Returns EventHandle

    • can be used for removing event in the future.
    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

Events

EVENT_ATTR: string = 'attr'

Fired when script attributes have changed. This event is available in two forms. They are as follows:

  1. attr - Fired for any attribute change. The handler is passed the name of the attribute that changed, the value of the attribute before the change and the value of the attribute after the change.
  2. attr:[name] - Fired for a specific attribute change. The handler is passed the value of the attribute before the change and the value of the attribute after the change.
export class PlayerController extends Script {
initialize() {
this.on('attr', (name, newValue, oldValue) => {
console.log(`Attribute '${name}' changed from '${oldValue}' to '${newValue}'`);
});
}
};
export class PlayerController extends Script {
initialize() {
this.on('attr:speed', (newValue, oldValue) => {
console.log(`Attribute 'speed' changed from '${oldValue}' to '${newValue}'`);
});
}
};
EVENT_DESTROY: string = 'destroy'

Fired when a script instance is destroyed and removed from component.

export class PlayerController extends Script {
initialize() {
this.on('destroy', () => {
// no longer part of the entity
// this is a good place to clean up allocated resources used by the script
});
}
};
EVENT_DISABLE: string = 'disable'

Fired when a script instance becomes disabled.

export class PlayerController extends Script {
initialize() {
this.on('disable', () => {
// Script Instance is now disabled
});
}
};
EVENT_ENABLE: string = 'enable'

Fired when a script instance becomes enabled.

export class PlayerController extends Script {
initialize() {
this.on('enable', () => {
// Script Instance is now enabled
});
}
};
EVENT_ERROR: string = 'error'

Fired when a script instance had an exception. The script instance will be automatically disabled. The handler is passed an Error object containing the details of the exception and the name of the method that threw the exception.

export class PlayerController extends Script {
initialize() {
this.on('error', (err, method) => {
// caught an exception
console.log(err.stack);
});
}
};
EVENT_STATE: string = 'state'

Fired when a script instance changes state to enabled or disabled. The handler is passed a boolean parameter that states whether the script instance is now enabled or disabled.

export class PlayerController extends Script {
initialize() {
this.on('state', (enabled) => {
console.log(`Script Instance is now ${enabled ? 'enabled' : 'disabled'}`);
});
}
};