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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
//Here is the code for a bare bones plugin that does nothing but register itself. Kiwi.Plugins.HelloWorld= { /** * The name of this plugin. */ name:'HelloWorld', /** * The version of this plugin in semver (semantic versioning) format */ version:'1.0.0', /** * The minimum version of Kiwi.js required to run this plugin in semver (semantic versioning) format */ minimumKiwiVersion: "0.7.0" }; Kiwi.PluginManager.register(Kiwi.Plugins.HelloWorld); |
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.
1 2 3 4 5 |
//Format <NameOfThePlugin>-<VersionOfThePlugin>.js //Example helloworld-1.0.0.js |
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.
1 2 |
<script src="kiwi.min.js"></script> <script src="plugins/helloworld-1.0.0.js/helloworld-1.0.0.js"></script> |
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
1 |
Kiwi.Plugins |
It is mandatory to attach all plugins to this namespace (a JS Object), as the example does.
1 |
Kiwi.Plugins.HelloWorld = ... |
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.
1 2 3 4 5 6 7 |
//This example continues on after the HelloWorld plugin has been registered. Kiwi.Plugins.HelloWorld.create = function(game) { //Inside this method you can do some of your own 'boot' stage code, //You have access to the game in which just booted also. console.log(game.stage.name + ' has just booted.'); } |
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’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
Kiwi.Plugins.HelloWorld = { name: 'HelloWorld', version: '1.0.0', minimumKiwiVersion: '0.7.0', /** * Other plugins required for this plugin to run. * @property pluginDependencies * @type Array * @public */ pluginDependencies: [ { //The name of the Plugin that we are dependant on. name:'MessagingPlugin', minimumVersion:'0.7.0' } ] Kiwi.PluginManager.register(Kiwi.Plugins.HelloWorld); }; |
The above ‘HelloWorld’ plugin is now dependant on another ‘MessagingPlugin’ in order to work.
Recent Comments