When you create a new script, and open it in the script editor, you get something that looks like this:
The script lets you define a few functions that will be called when the scene is playing, and some parameters.
The ctx object
The context is an object, unique per Script, that you can use to store your script data during the script life time. The context is created upon setup() and cleared on cleanup() and is passed into all of the script functions. It has a few pre-defined properties:
|entity||Entity||The entity that the script is attached to.|
|entityData||Object||Data object, shared between all scripts on the Entity.|
|activeCameraEntity||Entity||The currently active camera entity.|
|domElement||HTMLCanvasElement||The WebGL canvas element.|
|playTime||number||The elapsed time since scene start.|
|viewportHeight||number||Height of the canvas.|
|viewportWidth||number||Width of the canvas.|
|worldData||Object||Data object, shared between all scripts in the World.|
Data objects and scoping
The ctx object unique to each script, and properties we define on it will only be accessible by that script. Some of its properties are shared between scripts. entityData is shared by all scripts on the entity and worldData is shared by all scripts. They are all initially empty, and can be used to store any kind of data
For example, if we’d like to define a property called acceleration, we could make it available on three levels:
The global goo object
The goo object provides access to classes in the Goo Engine API.
Parameters and “args”
All parameters that are declared in the parameters array can be accessed via the args during runtime. The parameters are also displayed the Script component panel in Create. The above script will generate the following script component panel:
Below you can read more about what the custom parameters lets you do.
Parameters need to be defined on a specific format. It is mentioned in the comments of an empty script too, but here’s a walkthrough of the structure.
- key [string] - The property key in the args object that should be used for this parameter.
- type [string] - Parameter type (see available types further down).
- default - Default value for the parameter.
- name [string] - The name that shows up in the script component panel.
- control [string enum] - Type of control in the script component panel. Will be discussed later.
- description [string] - Tooltip for the script component panel.
- options [array] - Used with the select control type.
- min [number] - Used with int or float types.
- max [number] - Used with int or float types.
- decimal [number] - Number of fractional digits for float values.
- step [number] - Step (increment) amount for float values.
- precision [number] - Number of significant digits for float values.
- exponential [boolean] - Used with slider control type.
The type property must be set to one of a few predefined strings, each corresponding to a type of parameter.
- int - Integer number variable (e.g. 5).
- float - Number variable (e.g. 3.14).
- string - String (e.g. “HelloGoo”).
- boolean - boolean (true or false).
- vec2 - An array of 2 numbers.
- vec3 - An array of 3 numbers.
- vec4 - An array of 4 numbers.
- texture, sound, entity, camera, animation, json - Direct references to different types of objects, controlled by drag-and-drop areas in the script panel.
All types in action, including a sample script:
Note that the URLs you enter should start with // and not http:// or https://.