Modules

The engine and editor expose their APIs to developers through modules, which exist as ECMAScript modules.

Note: starting from 3.0, engine functions cannot be accessed through the global variable, prefixed with cc!

Engine Module

Functionality

Module 'cc' provide access to engine functionalities. The content of the 'cc' module is dynamic, and its content is related to the setting of Feature crop in Project Settings.

Engine logging

An example is shown below:

  1. import { log } from 'cc';
  2. log('Hello world!');

Constants at build time

The engine module 'cc/env' exposes some constants at build time. These constants represent the execution environment, debug level, or platform identification.

As these constants are declared with const, it provides a good opportunity for code optimization.

Execution environment

Name (all types are boolean)Description
BUILDIs it running in the built environment.
PREVIEWIs it running in the preview environment.
EDITORIs it running in the editor environment.

Debug level

Name (all types are boolean)Description
DEBUGWhether it is in debug mode. It is false only when the debug option is not checked when building, and it is true in all other cases
DEVEquivalent to DEBUG/EDITOR/PREVIEW

Platform identification

The constants listed in the following table represent whether it is running on a particular platform or class of platforms, and all types of these constants are boolean.

NamePlatformMINIGAME mini gameRUNTIME_BASED based on Cocos RuntimeSUPPORT_JIT JIT is supported
HTML5Web
NATIVENative platforms
ALIPAYAlipay Mini Game✔️✔️
BAIDUBaidu Mini Game✔️✔️
BYTEDANCEBytedance Mini Game✔️✔️
WECHATWeChat Mini Game✔️✔️
XIAOMIXiaomi Quick Game✔️✔️
COCOSPLAYCocos Play✔️✔️
HUAWEIHuawei Quick Game✔️✔️
OPPOOPPO Mini Game✔️✔️
VIVOvivo Mini Game✔️✔️

Logging in debug mode

An example is shown below:

  1. import { log } from 'cc';
  2. import { DEV } from 'cc/env';
  3. if (DEV) {
  4. log('I am in development mode!');
  5. }

Editor Modules

The editor modules are all under the 'cce:' protocol (cce stands for “CocosCreatorEditor”).

All editor modules are only available in the editor environment. For example, the editor module cannot be accessed in the environment after previewing and building, on the contrary, it can be accessed in the Scene panel.