Class ScriptTypeEX<Attrs>

扩展脚本类

Type Parameters

  • Attrs

Hierarchy

  • ScriptType
    • ScriptTypeEX

Constructors

  • Create a new ScriptType instance.

    Type Parameters

    • Attrs

    Parameters

    • args: { app: AppBase; entity: Entity }

      The input arguments object.

      • app: AppBase

        The AppBase that is running the script.

      • entity: Entity

        The Entity that the script is attached to.

    Returns ScriptTypeEX<Attrs>

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

  • get enabled(): boolean
  • Returns boolean

  • set enabled(arg: boolean): 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

    • arg: boolean

    Returns void

  • get attributes(): ScriptAttributes
  • The interface to define attributes for Script Types. Refer to ScriptAttributes.

    Example

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

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

    Returns ScriptAttributes

  • get scriptName(): string
  • Name of a Script Type.

    Returns string

Methods

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

    Returns

    Self for chaining.

    Example

    obj.fire('test', 'This is the message');
    

    Parameters

    • name: string

      Name of event to fire.

    • Optional arg1: any

      First argument that is passed to the event handler.

    • Optional arg2: any

      Second argument that is passed to the event handler.

    • Optional arg3: any

      Third argument that is passed to the event handler.

    • Optional arg4: any

      Fourth argument that is passed to the event handler.

    • Optional arg5: any

      Fifth argument that is passed to the event handler.

    • Optional arg6: any

      Sixth argument that is passed to the event handler.

    • Optional arg7: any

      Seventh argument that is passed to the event handler.

    • Optional arg8: any

      Eighth argument that is passed to the event handler.

    Returns EventHandler

  • Test if there are any handlers bound to an event name.

    Returns

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

    Example

    obj.on('test', function () { }); // bind an event to 'test'
    obj.hasEvent('test'); // returns true
    obj.hasEvent('hello'); // returns false

    Parameters

    • name: string

      The name of the event to test.

    Returns boolean

  • Called when script is about to run for the first time.

    Returns void

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

    Returns

    Self for chaining.

    Example

    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 handler functions, called 'test' with scope this

    Parameters

    • Optional name: string

      Name of the event to unbind.

    • Optional callback: HandleEventCallback

      Function to be unbound.

    • Optional scope: object

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

    Returns EventHandler

  • Attach an event handler to an event.

    Returns

    Self for chaining.

    Example

    obj.on('test', function (a, b) {
    console.log(a + b);
    });
    obj.fire('test', 1, 2); // prints 3 to the console

    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.

    • Optional scope: object

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

    Returns EventHandler

  • 添加脚本属性监听

    Type Parameters

    • K extends string | number | symbol

    Parameters

    • propertyName: K

      脚本属性名称

    • callback: ((value: Attrs[K], preValue: Attrs[K]) => any)

      属性变化回调函数

        • (value: Attrs[K], preValue: Attrs[K]): any
        • Parameters

          • value: Attrs[K]
          • preValue: Attrs[K]

          Returns any

    • Optional scope: object

      回调函数this指向

    Returns void

  • Attach an event handler to an event. This handler will be removed after being fired once.

    Returns

    Self for chaining.

    Example

    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

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

    • Optional scope: object

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

    Returns EventHandler

  • Called after all initialize methods are executed in the same tick or enabling chain of actions.

    Returns void

  • Called for enabled (running state) scripts on each tick, after update.

    Parameters

    • dt: number

      The delta time in seconds since the last frame.

    Returns void

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

    Parameters

    • old: ScriptType

      Old instance of the scriptType to copy data to the new instance.

    Returns void

  • Called for enabled (running state) scripts on each tick.

    Parameters

    • dt: number

      The delta time in seconds since the last frame.

    Returns void

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

    Example

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

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

    Parameters

    • methods: object

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

    Returns void