Overview of the Construct 2 SDK

Third party developers can write their own plugins and behaviors for Construct 2 in javascript. Plugins have two parts: the editor side (defining the plugin settings, actions and conditions, etc) and the runtime side. The editor side is interpreted by Google's V8 javascript engine built in to the HTML5 exporter. The runtime side runs in the browser. Note the implications: you cannot use browser features in the edittime, and you should not use browser-specific features in the runtime.

Javascript is not Java! Java is an application programming language developed by Sun (which, confusingly, can also run in browsers via a plugin). Javascript is the native programming language for web pages in browsers. Make sure you're clear on the difference.

You do not need any special tools to develop plugins or behaviors. All you need is a text editor and a little javascript knowledge. A good text editor with syntax highlighting for javascript is Notepad++, which is favoured by Scirra developers.

This guide will not teach you javascript. Generally, we ask that you do not post questions about the javascript language itself to the Scirra forums. There are many other better places to ask on the web. Questions about the SDK are always welcome, though. Some useful resources for javascript are:

Mozilla's Javascript Guide
A complete guide to javascript. This might be a good starting point if you are new to programming.

StackOverflow
An excellent Q&A website. Also a good place to search to see if your question has already been asked and answered.

Javascript Garden
Guide to the unusual parts of javascript. Very useful if you have experience with a different programming language but are new to javascript.

Mozilla Developer Network (MDN)
An excellent reference for HTML, javascript, and more. A very useful place to look up features for the browser side of the plugin.

Overview of plugins and behaviors

Before developing a plugin or behavior, you should be familiar with the usage of Construct 2, or the terminology and functionality will be difficult to understand. The Construct 2 beginner's guide is a good place to start.

As you probably know, plugins define new objects in the editor, and behaviors add functionality other objects. Plugins and behaviors are surprisingly similar. Behaviors are essentially also plugins, but with the aim of affecting another object. The SDKs for both are very similar, so this guide will simply describe how plugins are made, and note where behaviors are different.

Plugin scripts

Plugin and behavior scripts are located at

<install path>\exporters\html5\plugins
<install path>\exporters\html5\behaviors

Each plugin has its own folder. Plugins consist of four files:

common.js - this is prepended to both edittime.js and runtime.js in case you have code common to both.
edittime.js - defines the plugin for the editor, including all its actions, conditions and expressions.
runtime.js - defines the plugin functionality in the browser.
PluginIcon.ico - the editor loads this icon to represent the plugin.

A template for both a plugin and behavior can be downloaded here. You can copy these to a folder in the above directories to provide a skeleton starting point for your plugin or behavior.

Plugins work identically in the 32-bit and 64-bit versions of Construct 2.

All the "built-in" plugins and behaviors are also written this way. It would be useful to read their scripts - also in the above directories - to see how their features are implemented. You can learn a lot from this.

All the scripts for the entire javascript runtime are also located in <install path>\exporters\html5. They are perfectly readable and commented (not minified). This is a great way to learn even more, but may make for some heavy reading. You may find these three scripts particularly relevant, though:

common_prelude.js - prepended to both edittime scripts and runtime scripts.
edittime_prelude.js - prepended only to edittime scripts.
preview_prelude.js - prepended only to runtime scripts.

Remember that javascript has no way of limiting access to objects. This means you can alter any part of the runtime at any time. You should assume this is a bad idea (with undefined consequences) unless the object properties have been explicitly documented with valid ways of accessing the property. The runtime is complex, and any undocumented changes you make may break projects in subtle ways or have other unintended consequences. Keep the reference handy!

Good luck!

Developing your own plugins and behaviors is relatively straightforward and can be fun! The following guide pages describe plugin development in more detail. If you have any questions about the SDK feel free to ask on the forum, but please remember we ask that you look to other resources for questions on the javascript language itself.

Tags: