API Reference

Class List

pc.script

Functions for creating user scripts for the script component

Summary

Static Methods

attributeCreates a script attribute for the current script.
createCreate a script resource object.
createLoadingScreenHandles the creation of the loading screen of the application.

Properties

legacyIf True, then engine will use legacy scripting system, defaults to true (subject to change)

Details

Static Methods

attribute(name, type, defaultValue, options)

Creates a script attribute for the current script. The script attribute can be accessed inside the script instance like so 'this.attributeName' or outside a script instance like so 'entity.script.attributeName'. Script attributes can be edited from the Attribute Editor of the PlayCanvas Editor like normal Components.

pc.script.attribute('speed', 'number', 5);
pc.script.attribute('message', 'string', "My message");
pc.script.attribute('enemyPosition', 'vector', [1,0,0]);
pc.script.attribute('spellType', 'enumeration', 0, {
    enumerations: [{
       name: "Fire",
       value: 0
    }, {
       name: "Ice",
       value: 1
    }]
 });
pc.script.attribute('enemy', 'entity');
pc.script.attribute('enemySpeed', 'curve');
pc.script.attribute('enemyPosition', 'curve', null, {
    curves: ['x', 'y', 'z']
});
pc.script.attribute('color', 'colorcurve', null, {
    type: 'rgba'
});

pc.script.create('scriptable', function (app) {
 var Scriptable = function (entity) {
     // store entity
     this.entity = entity;
 };

 return Scriptable;
}

Parameters

nameStringThe name of the attribute
typeStringThe type of the attribute. Can be one of the following: 'number', 'string', 'boolean', 'asset', 'entity', 'rgb', 'rgba', 'vector', 'enumeration', 'curve', 'colorcurve'
defaultValueObjectThe default value of the attribute
optionsObjectOptional parameters for the attribute. Valid values are:
  • {Number} min: The minimum value of the attribute
  • {Number} max: The maximum value of the attribute
  • {Number} step: The step that will be used when changing the attribute value in the PlayCanvas Editor
  • {Number} decimalPrecision: A number that specifies the number of decimal digits allowed for the value
  • {Object[]} enumerations: An array of name, value pairs from which the user can select one if the attribute type is an enumeration
  • {String[]} curves: (For 'curve' attributes only) An array of strings that define the names of each curve in the curve editor.
  • {Array} color: (For 'curve' attributes only) If true then the curve attribute will be a color curve.

create(name, callback)

Create a script resource object. A script file should contain a single call to pc.script.create and the callback should return a script object which will be instanciated when attached to Entities.

pc.script.create( function (app) {
 var Scriptable = function (entity) {
     // store entity
     this.entity = entity;

     // use app
     app.components.model.addComponent(entity, {...});
 };

 return Scriptable;
}

Parameters

nameStringThe name of the script object.
callbackfunctionThe callback function which is passed an {pc.Application} object, which is used to access Entities and Components, and should return the Type of the script resource to be instanced for each Entity.

createLoadingScreen(callback)

Handles the creation of the loading screen of the application. A script can subscribe to the events of a pc.Application to show a loading screen, progress bar etc. In order for this to work you need to set the project's loading screen script to the script that calls this method.

pc.script.createLoadingScreen(function (app) {
    var showSplashScreen = function () { // }
    var hideSplashScreen = function () { // }
    var showProgress = function (progress) { // }
    app.on("preload:start", showSplashScreen);
    app.on("preload:progress", showProgress);
    app.on("start", hideSplashScreen);
});

Parameters

callbackfunctionA function which can set up and tear down a customised loading screen.

Properties

Booleanlegacy

If True, then engine will use legacy scripting system, defaults to true (subject to change)