Use built-in editor services

The editor includes several JavaScript services that provide helpful functions for interacting with project data, the engine, and the operating system. Most of the standard features built in to the editor rely on these services to carry out the editing tasks requested by the user in the UI. You can also take advantage of these same services in your custom plug-in.

Standard services

You'll find all the editor's standard services in the JavaScript modules under editor/core/js/services. For example, a few that you may find particularly useful:

There are dozens more. We're working on putting together a complete reference to all of the functions exposed by these services. In the meantime, the best way to find out what functions you can use in your plug-in is to browse the source code files.

To use a service

In order to use a service in your plug-in's JavaScript code, you need to "include" the service's JavaScript module. You can do this either with in a call to the require function, or in the define call that you use to define a module of your own.

For example, in the following custom action, we use a call to require. The first parameter to require includes the host service module -- the JavaScript file in which it is defined. The second parameter is a function that is immediately executed. Inside this function, the host service is accessible by a name that you assign it in the function's parameters. This is a handy way of making JavaScript resources available within a small, defined block of code such as the ones you might find in a plug-in action extension:

extensions = {
    actions = [
        {
            name = "launch-website"
            type = "js"
            script = """
                require(['services/host-service'], function (hostService) {
                    hostService.openUrl("http://www.stingrayengine.com");
                });
            """
        }
    ]
}

If you are defining your own JavaScript module -- for example, in a .js file that accompanies a custom panel -- you can include the service's module in your module's define call in exactly the same way as the require call above. The service then becomes available by name anywhere within the scope of your module. So you can call it within any functions you create in that module, as in the example below.

define(['services/host-service'], function (hostService) {
    "use strict";

    var exports = {}

    exports.openMyFavoriteUrl = function() {
        hostService.openUrl("http://www.stingrayengine.com")
    }

    return exports;
});

Note that most of the functions provided by editor services are asynchronous. If you need to use the return value from a function call, you'll need to use the then() method to make sure that your next code gets evaluated after the first function has completed. For more, see Tips for developing plug-ins.