Script Class

Extends Object
Module: Script

Script objects are used to control other objects present in the scene.

It can access and change every object in the program and supports some events

  • initialize
    • Called on app initialization, its called after all children elements are initialized, its safe to apply operations on other objects inside this method.
  • update
    • Called on every frame after rendering
  • dispose
    • Called when disposing the program
  • onMouseOver(intersections)
    • Called on every frame if mouse is on top of one of the script children
    • Receives an intersections array as argument.
  • onResize(x, y)
    • Called every time the window is resized
    • Receives width and height as parameters
  • onAppData(data)
    • Called when receiving data sent by the host website

Code written inside scripts have access to the following attributes:

  • scene
  • program
  • self
    • Same as this reference but global in the script scope
  • Keyboard
  • Mouse

There is also access to the following functions

  • include
    • Include a javascript file from resources, when including files the user needs to be carefull and clear manually global declarations

Methods

appData

(
  • data
)

Call onAppData if available.

Called automatically every time external data is passed to the runtime.

Parameters:

  • data Object

compileCode

(
  • code
)

Set script code.

Can be used to dinamically change the script code. However it is not recommended can lead to undefined behavior.

Parameters:

  • code String

dispose

()

Disposes the script, can be used to clear resources when the program exits.

Calls the script dispose method if it exists.

getIncludes

(
  • code
)
static

Get includes from the code.

Used to extract includes from code when loading libraries in APPEND mode.

Parameters:

  • code String

    Script code.

include

(
  • name
)

Auxiliar function to include javascript source file from resource into the script.

The imported source is evaluated and loaded in the context of the script.

Global declarations need to be cleaned using the dipose method.

Parameters:

  • name String

    Javascript resource name.

initialize

()

Initialize script Automatically called by the runtime Calls the script initialize method if it exists

removeIncludes

(
  • code
)
static

Remove includes from code.

Used to remove include statements when initializing code in APPEND mode.

Parameters:

  • code String

    Script code.

resize

()

Call resize method if available.

The resize method receives width and height as arguments.

toJSON

(
  • meta
)
Object

Create JSON for script.

Parameters:

  • meta Object

Returns:

Object:

json

update

()

Update script state.

Calls the script update method if it exists.

Properties

code

String

Javascript code attached to the script

program

Program

Pointer to the parent program Used access program resources easier

scene

Scene

Pointer to the parent scene

Attributes

APPEND

Number

Append libraries on initialization.

Libraries are appended to the script code on initialization.

DEFAULT

String

Default script code used when creating a new Script.

EVALUATE

Number

Evaluate libs during runtime.

This allows to load new libs during runtime, but its not possible to access private statements.

METHODS

Array

List of methods that a script can implement.

script

Function

Compiled function used during runtime