API Reference

Class List

ScriptType

Represents the type of a script. It is returned by pc.createScript. Also referred to as Script Type.
The type is to be extended using its JavaScript prototype. There is a list of methods that will be executed by the engine on instances of this type, such as:

initialize and postInitialize - are called if defined when script is about to run for the first time - postInitialize will run after all initialize methods are executed in the same tick or enabling chain of actions.
update and postUpdate - methods are called if defined for enabled (running state) scripts on each tick.
swap - This method will be called when a ScriptType that already exists in the registry gets redefined. If the new ScriptType has a `swap` method in its prototype, then it will be executed to perform hot-reload at runtime.

Summary

Static Properties

attributesThe interface to define attributes for Script Types.[read only]

Static Methods

extendShorthand function to extend Script Type prototype with list of methods.

Properties

appThe pc.Application that the instance of this type belongs to.
enabledTrue if the instance of this type is in running state.
entityThe pc.Entity that the instance of this type belongs to.

Events

attr:[name]Fired when a specific script attribute has been changed
attrFired when any script attribute has been changed
destroyFired when a script instance is destroyed and removed from component
disableFired when a script instance becomes disabled
enableFired when a script instance becomes enabled
errorFired when a script instance had an exception.
stateFired when a script instance changes state to enabled or disabled

Details

Static Properties

attributesThe interface to define attributes for Script Types. Refer to pc.ScriptAttributes[read only]

Static Methods

extend(methods)

Shorthand function to extend Script Type prototype with list of methods.

var PlayerController = pc.createScript('playerController');

PlayerController.extend({
    initialize: function() {
        // called once on initialize
    },
    update: function(dt) {
        // called each tick
    }
})

Parameters

methodsObjectObject with methods, where key - is name of method, and value - is function.

Constructor

ScriptType(args)

Parameters

argsObjectThe input arguments object
args.appObjectThe pc.Application that is running the script
args.entityObjectThe pc.Entity that the script is attached to

Properties

pc.Applicationapp

The pc.Application that the instance of this type belongs to.

Booleanenabled

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 Script Component 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.

pc.Entityentity

The pc.Entity that the instance of this type belongs to.

Events

attr:[name]

Fired when a specific script attribute has been changed

PlayerController.prototype.initialize = function() {
    this.on('attr:speed', function(value, valueOld) {
        console.log('speed been changed from ' + valueOld + ' to ' + value);
    });
};

Parameters

valueObjectNew value
valueOldObjectOld value

attr

Fired when any script attribute has been changed

PlayerController.prototype.initialize = function() {
    this.on('attr', function(name, value, valueOld) {
        console.log(name + ' been changed from ' + valueOld + ' to ' + value);
    });
};

Parameters

nameStringName of attribute
valueObjectNew value
valueOldObjectOld value

destroy

Fired when a script instance is destroyed and removed from component

PlayerController.prototype.initialize = function() {
    this.on('destroy', function() {
        // no more part of an entity
        // good place to cleanup entity from destroyed script
    });
};

disable

Fired when a script instance becomes disabled

PlayerController.prototype.initialize = function() {
    this.on('disable', function() {
        // Script Instance is now disabled
    });
};

enable

Fired when a script instance becomes enabled

PlayerController.prototype.initialize = function() {
    this.on('enable', function() {
        // Script Instance is now enabled
    });
};

error

Fired when a script instance had an exception. The script instance will be automatically disabled.

PlayerController.prototype.initialize = function() {
    this.on('error', function(err, method) {
        // caught an exception
        console.log(err.stack);
    });
};

Parameters

errErrorNative JavaScript Error object with details of error
methodStringThe method of the script instance that the exception originated from.

state

Fired when a script instance changes state to enabled or disabled

PlayerController.prototype.initialize = function() {
    this.on('state', function(enabled) {
        console.log('Script Instance is now ' + (enabled ? 'enabled' : 'disabled'));
    });
};

Parameters

enabledBooleanTrue if now enabled, False if disabled