Writing Kiwi.js Plugins

Creating a Plugin is a very simple process. However depending on what you want the Plugin to do, it may require a deep knowledge of how the core Kiwi.js library works.

Introduction

Minimum Plugin Requirements

When it comes to writing a plugin, there is only a very small amount of code that is necessary to package and this code is standard across all Plugins, no matter how complex. Below is a list of requirements for every Plugin.

  • Unique Name for your Plugin.
  • Version number for the Plugin, following the semantic versioning system.
  • The minimum version of Kiwi required to run the Plugin.
  • The Plugin must register itself with the core Kiwi.js library.

Below is an example of a Plugin which does nothing but registers itself to become a valid Plugin.

Packaging

When packaging a Plugin, we recommend that you follow the same format as we do, which is outlined below. This format helps to avoid caching problems and lets you to easily see the version when browsing directories.

Including and Testing

To test to see if a Plugin successfully registers then what you need to do is create a new HTML file and either inside the HEAD or BODY tags include the Kiwi Library and then your Plugin. If you then load the HTML page you should see a log message that confirms that the plugin has been registered, with the name and version of the Plugin.

Include the Kiwi Library First!

Make sure that you always include the Kiwi.JS Library before your Plugin, otherwise the registration process will fail because ‘Kiwi.PluginManager’ will not exist.

Key Points:

It is not necessary to create a game or to write any additional scripts for the plugin to be registered; it happens automatically as Plugins are registered once per page and attach themselves to the core Kiwi.js library. Warning: Don’t get confused between registering a Plugin and using one

You can get an array of available plugins at any time from the Kiwi.PluginManager.availablePlugins property. It will return an array of objects, each of which contains the name and version number of a registered plugin.

There are several key elements in the basic plugin script that are essential. First, the namespace

It is mandatory to attach all plugins to this namespace (a JS Object), as the example does.

The format that encapsulates the plugin is also important. Although there are other ways of doing this, the recommended method is to create a standard javascript object literal. After creating the object the plugin needs to be registered to the PluginManager using the register function.

Optional Features

When creating a Plugin there are some optional features that you can access to gain advantages of the Kiwi.js Library.

For many Plugins, you will not need the following optional features. As they are not necessary to create every type of Plugin. For instance if you’re just packing up custom  GameObjects or Components as Plugins you don’t need to worry about the Game boot process, as developers using your plugin will just create instances of the objects or components as needed. If you’re creating a service though, you may need to do things just before the game starts, such as checking to see if an ad-server is available in a banner ad plugin.

The Create Method

When you are writing Plugins, sometimes you will want delay the execute of your code until a game has been booted OR alternatively you may want to get the Game that is using that Plugin.

In either case, this is where you can attach a ‘create’ method to the Object Literal which you registered with PluginManager.

The create method is executed when a Game that has been told to use this Plugin reaches the ‘boot’ stage of the Game creation process.

The Boot Stage is the point in the Game where all the Managers (Input, Time, Stage, e.t.c) are just starting up because the DOM which the game requires, has finished loading.

Dependencies

Some Plugins are dependent on others being available before they can be used. If you are creating one such Plugin then before you register the Plugin you can specify its ‘pluginDependencies’.

The above ‘HelloWorld’ plugin is now dependant on another ‘MessagingPlugin’ in order to work.

Share the joy

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">