Plugin Script

property

In the Assets, select any of the script, you can see such a settings interface in the Properties, we can set whether the script is "Import As Plugin" here.

This option is only related to the script, specific aspects of the impact, beginners only need a simple understanding

Type: Normal Script Plugin Script
Declare components Support Not Support
Modular Support, you can refer to other Normal Script by require, can not require Plugin Script Not Support, also can not require Normal Script
Variable scope The local variables defined within each script are not exposed to global At runtime, local variables within the script that are not in any function are exposed as global variables
use strict Forced, undefined variables can not be assigned Need to manually declare, otherwise once the undefined variable is assigned it will become a global variable
When the script is imported into the editor The ES2015 feature in the script is compiled first, and then into a unified modular analysis process Do not do anything
During the project building phase All Normal Scripts will be packaged into a single script file, and in the release mode will be further compressed Not packaged, and in the release mode will be compressed
SourceMap Support Not Support

Script loading order

The script loading order is as follows:

  1. Cocos2d engine
  2. Plugin Script (If there are multiple, they are loaded in alphabetical order according to the path in the project)
  3. Normal Script (Only one file after packing, the internal scripts will be initialized in the order of require dependencies)

Target platform compatibility

After the project is published, the plugin will be loaded directly by the target platform, so please check the plugin compatibility with the target platform, or the plugin may not run.

Precautions

Global variable

Since all Plugin Scripts are guaranteed to be loaded before Normal Scripts, you can use this feature to declare some special global variables in addition to being used to load the plugin.

You can add such a script in the project, and set the "Import As Plugin":

/* globals.js */

// Defines the default value for new components
window.DEFAULT_IP = "192.168.1.1";

// Define the component switch
window.ENABLE_NET_DEBUGGER = true;

// Define engine API abbreviations (only for constructors)
window.V2 = cc.Vec2;

Then you can access them directly in any Normal Script:

/* network.js */

cc.Class({
    extends: cc.Component,
    properties: {
        ip: {
            default: DEFAULT_IP
        }
    }
});
/* network_debugger.js */

if (ENABLE_NET_DEBUGGER) {
    // This component takes effect when ENABLE_NET_DEBUGGER
    cc.Class({
        extends: cc.Component,
        properties: {
            location: {
                default: new V2(100, 200)
            }
        },
        update: function () {
            ...
        },
    });
}
else {
    // Otherwise this component does nothing
    cc.Class({
        extends: cc.Component,
        start: function () {
            // Remove the component after it starts
            this.destroy();
        }
    });
}

In this case, the variables in globals.js has been used since scripts such as network.js and network_debugger.js are loaded. So if the globals.js is not a plugin script, each script that might use those global variables should declare require("globals"); at the top, to ensure that globals.js is loaded first.

But if a global variable itself is to be initialize in the component's onLoad, it is recommended to use window.foo = bar to declare the global variables directly in the onLoad of a Normal Scirpt, do not need to use the Plugin Scirpt, see Access by using global variable.

Please note that when the game is running away from the editor, the plugin script will run directly on the global scope. Local variables within the script that are not in any function will be exposed as global variables. Be careful to cause global variables to pollute.

You should use global variable with care and understanding, we do not recommend using it, you had better keep them readonly if really need to do that.
Please avoid name duplication with exists global variables in the system.
You can freely encapsulate or extend the Cocos2d engine in Plugin Scripts, but this will increase team communication costs, making scripts difficult to reuse.


Continue on to read about JavaScript Primer or back to Scripting.