Extending Construct 2 with Javascript

0 favourites
From the Asset Store
Casino? money? who knows? but the target is the same!
  • 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.

  • 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.

  • I'd just make a forum post if you get stuck - there probably won't be SDK docs for a while.

  • 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.

  • OK, I'll see if I can put together a quick template at some point.

  • must learn js now

  • OK, I'll see if I can put together a quick template at some point.

    In the meantime, the keyboard object seems to be the best place to start. It's the most barebones of all the plugins.

  • 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.

  • Not yet!

  • 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.

  • Alright guys. I put all I currently know into this puppy. It's a trimmed down keyboard object with a few example ACE entries in it and a lot of comments. It is likely very buggy, but it should familiarize you with everything. You can use it as a starting point if you like, but realize that I'm just feeling my way around blindly. Now that I'm fairly familiar with everything, I'm going to try and implement simplex noise as a plugin.

    The two parts you want to really familiarize yourself with mainly are the ACE definition macros in the edittime file and how those relate to the ACE definitions in the runtime file. That should get you well on your way.

    http://dl.dropbox.com/u/1032313/SDKTut.zip

  • thanks, that helped a lot.

    PS

    a tip for everybody, don't use notepad it'll just confuse you more, use notepad++

  • Notepad++ is better then classic Notepad, but I must recommend the PSPad because this software is better than anyone else source editor for me.

    PSPad is a freeware programmer's editor for Microsoft Windows operating systems, useful for people who:

    • work with various programming environments
    • like highlighted syntax in their source code
    • need a small tool with simple controls and the capabilities of a mighty code editor
    • are looking for a tool that handles plain text
    • want to save time - PSPad offers rich text formating functions
    • need tool what offer user extension capabilities
    • want to save money and still have the functionality of professional products because PSPad is free for commercial and government purposes too

    List of changes, history and bug corrections is available here

    If you have a problem or question, you can visit the PSPad discussion forum. After registration you can receive messages for chosen topics via e-mail.

    PSPad beta versions are published in the discussion forum. You can download them without any restrictions, test new program functions and have bugs corrected before the full release is available.

    The latest build published in the forum is 4.5.5 (2405)

    PSPad is available in eight languages:

    Cesky | Slovensky | English | Deutsch | Fran�ais | Italiano | Russian | Espa�ol

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I like Visual Studio because of intellisense with JS, is there any free editor out there like notepad++ but for JS with intellisense style features?

  • Arsonide: good tutorial but the expressions are wrong: the return value from expressions is ignored. You need to write PieExp like this:

    exps["PieExp"] = function(ret) // all expressions take a 'ret' parameter
    	{
    		// Call set_string, set_int or set_float on the ret object to return a value
    		// The value returned from this function is ignored!
    		// If your expression has parameters, they follow on after 'ret', e.g.:
    		// function(ret, x, y)
    		ret.set_string("Pie");
    	};[/code:2zaldcb6]
Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)