Extending Construct 2 with Javascript

Discussion and feedback on Construct 2

Post » Fri Feb 04, 2011 11:13 pm


The HTML5 exporter in Construct 2 includes Google's V8 Javascript engine. This means both the editor and runtime parts of plugins can be written in javascript. This makes it much, much easier to extend Construct - previously you had to have relatively good knowledge of C++, which is a difficult language to learn, as well as Microsoft Visual C++ (the full version of which is expensive, and the express versions are difficult to set up for the SDK). Now you can do it on any computer in any text editor (even Notepad). I use Notepad++ myself, which is basically an advanced Notepad, and has syntax highlighting for javascript.

Custom javascript plugins are a good substitute for Python scripts for advanced users, too. You can easily put together some custom functionality for your own project once you're familiar with the SDK.

Plugins also only need to be written once for both the 32-bit and 64-bit versions of Construct - the V8 engine handles the differences itself.

Location of plugins
Plugins are stored under <install path>\exporters\html5\plugins. Each plugin has its own folder, with four files:
edittime.js - defines everything the editor needs to know about the plugin.
runtime.js - defines the plugin methods that run in the browser.
common.js - prepended to both edittime.js and runtime.js, in case there's any code that is in common to both files. No plugins currently use this, it was added in anticipation of code duplication.
PluginIcon.ico - icon file loaded by the editor to represent the plugin.

To make a plugin, simply copy and paste another plugin's folder and start editing the files. (There's no template yet - sorry!)

Documentation
There is no SDK documentation right now, mainly because there's just no time to get it all written down. Also, the SDK is subject to change at any time! If you write a plugin now, be prepared to have to change it to fix it after some releases. This is due to the nature of pre-alpha software: it will change a lot, and it will break things.

The best way to learn about the Javascript SDK is to read the source code to the other plugins. There are a couple of other javascript source files that are prepended to the plugin sources that will help you learn about the features available. These are under <install path>\exporters\html5, and are:
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.
The rest of the scripts in that folder are for the browser runtime. You can have a look through them if you want your plugin to hack around with the engine. Due to the nature of Javascript your plugin can modify any part of the runtime at any time. However, it is undocumented as to which parts will definitely break if you change them, and which parts can safely be modified. If you're not sure, make a forum post asking.

Minification
It is not necessary to minify/obfuscate your scripts. Construct 2 gives the option to minify script on export.

Hope that helps get you started! Just be warned, the SDK may change at any time as functionality is added and changed.
Ashley2011-08-14 23:19:15
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Sun Feb 06, 2011 1:28 am

Had a look through the files and tried to make my own cookie plugin with limited sucess, but am a bit stuck, is there going to be any more documentation on this anytime soon? I'd love to get going making lots of plugins but am just having trouble using it with c2.
Image Image
Scirra Founder
B
125
S
37
G
25
Posts: 3,945
Reputation: 44,922

Post » Sun Feb 06, 2011 1:50 am

I'd just make a forum post if you get stuck - there probably won't be SDK docs for a while.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Tue Feb 08, 2011 9:36 am

It still confuses me :( If I wrote all the functions on a test page, would it be ok to post them here and get help turning it into a plugin?

Or could you show me the simplest plugin, say an Alert plugin that just calls alert("text");?

That should be enough to get me going, would love to write some ajax/cookie plugins etc but just am stuck atm.
Image Image
Scirra Founder
B
125
S
37
G
25
Posts: 3,945
Reputation: 44,922

Post » Tue Feb 08, 2011 2:52 pm

OK, I'll see if I can put together a quick template at some point.
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Wed Feb 09, 2011 1:16 am

must learn js now
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Wed Feb 09, 2011 1:50 am

[quote="Ashley":zatf3hra]OK, I'll see if I can put together a quick template at some point.[/quote:zatf3hra]
In the meantime, the keyboard object seems to be the best place to start. It's the most barebones of all the plugins.
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Post » Wed Feb 09, 2011 2:11 am

One thing I've been meaning to ask: is there an equivalent to the OnFrame() method from the original SDK? If there is, I can't seem to find it.
Moderator
B
98
S
37
G
19
Posts: 1,584
Reputation: 17,817

Post » Wed Feb 09, 2011 3:31 am

Not yet!
Scirra Founder
B
359
S
214
G
72
Posts: 22,952
Reputation: 178,610

Post » Wed Feb 09, 2011 6:07 am

I am preparing a "template/tutorial" plugin. It will contain one action that pops up an alert, one condition that compares a number with the number 17, and one expression that outputs the string "Pie".

The template/tutorial plugin is extensively commented, so you can understand most of the sections in the SDK. I'll post it here by tomorrow morning.

EDIT: I think I will call it the SDKTut Object, seeing as how Ash might one day decide to make his own template object with better comments, and I don't want to cause any confusion.
B
5
S
2
G
5
Posts: 221
Reputation: 2,272

Next

Return to Construct 2 General

Who is online

Users browsing this forum: No registered users and 14 guests