Class: ScriptType

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
  • postInitialize
  • update
  • postUpdate
  • swap
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.

Constructor

new ScriptType(args)

Parameters:
Name Type Description
args Object The input arguments object
Properties
Name Type Description
app Object The pc.Application that is running the script
entity Object The pc.Entity that the script is attached to
Properties:
Name Type Description
app pc.Application The pc.Application that the instance of this type belongs to.
entity pc.Entity The pc.Entity that the instance of this type belongs to.
enabled Boolean 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.
Source:

Members

(private, static, readonly) __name :String

Name of a Script Type.
Type:
  • String
Source:

(static, readonly) attributes :pc.ScriptAttributes

The interface to define attributes for Script Types. Refer to pc.ScriptAttributes
Type:
Source:
Example
var PlayerController = pc.createScript('playerController');

PlayerController.attributes.add('speed', {
    type: 'number',
    title: 'Speed',
    placeholder: 'km/h',
    default: 22.2
});

Methods

(static) extend(methods)

Shorthand function to extend Script Type prototype with list of methods.
Parameters:
Name Type Description
methods Object Object with methods, where key - is name of method, and value - is function.
Source:
Example
var PlayerController = pc.createScript('playerController');

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

Events

attr:[name]

Fired when a specific script attribute has been changed
Parameters:
Name Type Description
value Object New value
valueOld Object Old value
Source:
Example
PlayerController.prototype.initialize = function() {
    this.on('attr:speed', function(value, valueOld) {
        console.log('speed been changed from ' + valueOld + ' to ' + value);
    });
};

attr

Fired when any script attribute has been changed
Parameters:
Name Type Description
name String Name of attribute
value Object New value
valueOld Object Old value
Source:
Example
PlayerController.prototype.initialize = function() {
    this.on('attr', function(name, value, valueOld) {
        console.log(name + ' been changed from ' + valueOld + ' to ' + value);
    });
};

destroy

Fired when a script instance is destroyed and removed from component
Source:
Example
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
Source:
Example
PlayerController.prototype.initialize = function() {
    this.on('disable', function() {
        // Script Instance is now disabled
    });
};

enable

Fired when a script instance becomes enabled
Source:
Example
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.
Parameters:
Name Type Description
err Error Native JavaScript Error object with details of error
method String The method of the script instance that the exception originated from.
Source:
Example
PlayerController.prototype.initialize = function() {
    this.on('error', function(err, method) {
        // caught an exception
        console.log(err.stack);
    });
};

state

Fired when a script instance changes state to enabled or disabled
Parameters:
Name Type Description
enabled Boolean True if now enabled, False if disabled
Source:
Example
PlayerController.prototype.initialize = function() {
    this.on('state', function(enabled) {
        console.log('Script Instance is now ' + (enabled ? 'enabled' : 'disabled'));
    });
};