Runtime script interface
Accessing the runtime script interface
The runtime script interface is typically accessed with the name
runtime. Note however this is not in a global variable: it is only passed in specific places.
All scripts in event sheets have the runtime interface passed to them as a parameter named
runtime. For more information see Scripts in event sheets.
In script files, the runtime interface is only passed to the
runOnStartup() method. Outside of that, it is your responsibility to pass it along wherever else it is needed. For more information see Script files.
The following events can be listened for using the
- Fires every tick, just before running the layout's event sheet. Use the
runtime.dt property to access delta-time and step the game's logic by that amount of time.
- Fired once only when the first layout in the project starts.
"beforeprojectstart" fires before
"beforelayoutstart" on the first layout, which in turn is before On start of layout triggers.
"afterprojectstart" fires after
"afterlayoutstart" on the first layout, which in turn is after On start of layout triggers. In both events, all instances on the first layout are created and available to modify.
- Fired when keys are pressed and release on the keyboard. These pass copies of a KeyboardEvent.
- Fired when mouse input is received. These pass copies of a MouseEvent.
- Fired when mouse wheel input is received. This passes a copy of a WheelEvent.
- Fired when pointer input is received. This covers mouse, pen and touch input. These pass copies of a PointerEvent. Construct also adds a
lastButtons property for
"mouse" type pointers with the previous
buttons state, which makes it easier to detect if different mouse buttons have been pressed or released in this event.
- Fired when device orientation or motion sensor input is received. These pass copies of a DeviceOrientationEvent or DeviceMotionEvent respectively.
- Fired when the savegame system saves or loads the state of the game. The
saveData property of the event object is extra JSON data to save in the "save" event, and the corresponding last saved data in the "load" event. This allows custom data stored in scripts to be integrated with the savegame system. Note this is serialized to JSON so things like object references and complex types cannot be saved directly.
- Fired whenever any new instance is created. The event object has an
instance property referring to the IInstance (or derivative) that was created.
- Fired whenever any instance is destroyed. After this event, all references to the instance are now invalid, so any remaining references to the instance should be removed or cleared to
null in this event. Accessing an instance after it is destroyed will throw exceptions or return invalid data. The event object has an
instance property referring to the IInstance (or derivative) that was destroyed. It also has an
isEndingLayout property to indicate if the object is being destroyed because it's the end of a layout, or destroyed for other reasons.
- addEventListener(eventName, callback)
- removeEventListener(eventName, callback)
- Add or remove a callback function for an event. See Runtime events above for the available events.
- Return the value of delta-time, i.e. the time since the last frame, in seconds.
- Return the in-game time in seconds, which is affected by the time scale. This is equivalent to the time system expression.
- An object with a property for each object class in the project. For example if the project has an object named Sprite, then
runtime.objects.Sprite will refer to the IObjectClass interface for Sprite.
- Get an instance (an IInstance or derivative) by its UID (Unique ID), a unique number assigned to each instance and accessible via its
- An object with a property for each global variable on an event sheet in the project. For example if the project has a global variable on an event sheet named Score, then
runtime.globalVars.Score provides access to the global variable from script.
- A shorthand reference to the Mouse script interface. This is only set if the Mouse plugin is added to the project.
- A shorthand reference to the Keyboard script interface. This is only set if the Keyboard plugin is added to the project.
- A shorthand reference to the Touch script interface. This is only set if the Touch plugin is added to the project.
- An ILayout interface representing the current layout.
- Get an ILayout interface for a layout in the project, by a case-insensitive string of its name or its zero-based index in the project.
- Return an array of ILayout interfaces representing all the layouts in the project, in the sequence they appear in the Project Bar.
- End the current layout and switch to a new layout given by a case-insensitive string of its name, or its zero-based index in the project (which is the order layouts appear in the Project Bar with all folders expanded). Note the layout switch does not take place until the end of the current tick.
- An IAssetManager interface providing access to project assets like sound and music files or other project files, as well as audio decoding utilities.
- An IStorage interface providing access to storage from scripts. Storage is shared with the Local Storage plugin.
- callFunction(name, ...params)
- Call a function in an event sheet, by a case-insensitive string of its name. Each parameter added after the name is passed to the function. There must be at least as many parameters provided as the function uses, although any additional parameters will be ignored. If the function has a return value, it will be returned from this method, else it returns
- This can only be called from a script in an event sheet function with a return type other than None. It is essentially equivalent to the Set return value action. However the fact this method can be called from script can make it easier to return a value from script from an event sheet function. For example an event sheet function could contain a single script action with the code
runtime.setReturnValue(getMyValue()), which means anywhere the event sheet function is called it returns the value of calling
- A string of the project name, as specified in Project Properties.
- A string of the project version, as specified in Project Properties.
- Return a random number in the range [0, 1). This is similar to
Math.random(), but can produce a deterministic sequence of values if the Advanced Random object overrides the system random.
- Return the in-game wall-clock time in seconds, which is not affected by the time scale. This is equivalent to the wallclocktime system expression.
- sortZOrder(iterable, callback)
- Sort the relative Z order of all the IWorldInstances given by iterable, using a custom sort function as the callback which receives two IWorldInstance to compare as arguments. An example using a myZOrder instance variable for sorting a Sprite object's instances is given below.
(a, b) => a.instVars.myZOrder - b.instVars.myZOrder);
- invokeDownload(url, filename)
- Invoke a download of the resource at the given url, downloading with the given filename. Locally-generated content can be downloaded with this method using either a data URI or blob URL for url.
- A read-only boolean indicating if the runtime is currently running in the context of a Web Worker. This is controlled by the Use worker project property. In worker mode, a more limited set of browser APIs is available. See Functions and classes available to Web Workers.
- async alert(message)
- Show an alert message prompt using the alert() method. This is provided as a runtime method since it forwards the call to the DOM in worker mode. Note that unlike the standard browser
alert() method, this is an async method - in worker mode it returns a promise that is resolved when the alert is closed, and execution in the worker will continue while the alert is showing. In DOM mode, the alert is blocking and will stop all execution while the alert is showing (but it still returns a promise that resolves when the alert is closed).
Construct 3 Manual
You are here:
Search this manual:
This manual entry was last updated on 9 Jun, 2021 at 20:23