The specific APIs for instances depends on the plugin type. These interfaces are all documented in the scripting reference section of the manual, such as ISpriteInstance for Sprite instances, which provides APIs for things like animations. Note that these interfaces also inherit from IWorldInstance and IInstance (depending on the kind of plugin). For example common properties like x
and y
are part of IWorldInstance
.
Construct further generates a type definition for every object type in the project, such as InstanceType.Player
for a Sprite object named Player. This will derive from ISpriteInstance
but add type definitions for the instance variables, behaviors and effects specific to that object type, ensuring they are autocompleted and type checked correctly. When writing TypeScript code you may wish to use a specific type like InstanceType.Player
if you want to refer to a specific object type, or a more general type like ISpriteInstance
or IWorldInstance
depending on whether that code is more generally applicable.
Integrating scripts and events
Strings and numbers can be exchanged between event sheets and TypeScript code using global and local variables in event sheets. Local variables are accessible via localVars
inside scripts in event sheets, and global variables are accessible via runtime.globalVars
. There is more information in the manual section on Scripts in event sheets.
Strings and numbers can also be exchanged via instance variables via the instVars
property of IInstance. (As noted previously Construct generates type definitions for each kind of object type which will include all the available instance variables.)
See also the example project Integrating events with script for an example of some useful techniques.
Using an external editor
Construct has special features to let you use an external code editor like VS Code to write your project's code if you prefer. Note that the workflow is different to working within the Construct editor itself, and involves exporting your project's type definitions. See the guide Using an external editor.
Learn more
Here are some more resources to learn more about using TypeScript in Construct.
Examples
There are lots of example projects using TypeScript coding in Construct which you can find in the Scripting category in the Example Browser.
Debugging
You can still use the browser developer tools to debug code that you write in Construct. Note that the debugger will show the compiled JavaScript output of your TypeScript code, but that generally looks the same but just without the type annotations. For a quick-start guide on debugging, see the manual section on Debugging script.
Minifying
Minifying on export also applies to your TypeScript code written in Construct. When minifying, your TypeScript code is first compiled to JavaScript, and then the minification performed on the resulting JavaScript. Code minification is done with the industry-standard library terser. Simple minify mode should never affect how your code runs. However if you enable Advanced minify mode on export, you may need to adjust how you write your code. See the manual guide on Exporting with Advanced minification for more details.
Worker mode
Construct is capable of running its entire engine in a Web Worker, rendering via OffscreenCanvas. However by default projects using TypeScript code run in the main page ("DOM mode") to ensure compatibility with code that expects to be able to use DOM features like document
. If your TypeScript code is compatible with running in a Web Worker, you can re-enable worker mode for improved performance by changing the project property Use worker to Yes (in the Advanced section). When you do this, Construct will also adjust the provided type definitions to match a worker context, so accessing DOM-specific APIs like document
will be marked as an error. There is more information about this in the manual section JavaScript in Construct.
Reference
See the Scripting section of the manual for full documentation on using TypeScript in Construct.