Components and component execution order

All classes inherited from Component are called Component Classes. The objects in a Component Class are called Components. Components are implement according to the Cocos Creator Entity Component (EC) system.

The component class must inherit from a cc class. Example:

  1. import { Component } from "cc";
  2. @ccclass("MyComponent")
  3. class MyComponent extends Component {
  4. }

Component creation and destruction

The life cycle of a component is completely controlled by the node. Unlike ordinary class objects, components cannot be created by constructors:

  1. const component = new MyComponent(); // Error: The component cannot be created by the constructor

In contrast, components must be created by nodes and added to nodes as follows

  1. const myComponent = node.addComponent(MyComponent);

When the component is no longer needed, call the node.removeComponent(myComponent) method to remove the specified component and destroy it. Example:

  1. import { Component } from "cc";
  2. @ccclass("MyComponent")
  3. class MyComponent extends Component {
  4. constructor () {
  5. console.log(this.node.name); // Error: The component is not attached to the node
  6. }
  7. public printNodeName () {
  8. console.log(this.node.name);
  9. }
  10. }
  1. const myComponent = node.addComponent(MyComponent);
  2. myComponent.printNodeName(); // Correct
  3. node.removeComponent(myComponent);
  4. myComponent.printNodeName(); // Error: The component is not attached to the node

Component execution order

Use a unified control script to initialize other scripts

Generally, developers will have a Game.ts script as the overall control script. If there are three components Configuration.ts, GameData.ts, and Menu.ts, then their initialization process would look like the following example:

  1. // Game.ts
  2. import { _decorator, Component, Node } from "cc";
  3. const { ccclass, property } = _decorator;
  4. import { Configuration } from './Configuration';
  5. import { GameData } from './GameData';
  6. import { Menu }from './Menu';
  7. @ccclass("Game")
  8. export class Game extends Component {
  9. private configuration = Configuration;
  10. private gameData = GameData;
  11. private menu = Menu;
  12. onLoad () {
  13. this.configuration.init();
  14. this.gameData.init();
  15. this.menu.init();
  16. }
  17. }

Among them, the init method needs to be implemented in Configuration.ts, GameData.ts and Menu.ts, and the initialization logic is put in it. In this way, it is guaranteed the initialization sequence of Configuration, GameData and Menu.

Use custom methods to control the update sequence in Update

Similarly, it is necessary to ensure the update order of each frame of the above three scripts, we can also replace the update scattered in each script with our own defined method:

  1. // Configuration.ts
  2. static updateConfig (deltaTime: number) {
  3. }

Then call these methods in the update of the Game.ts script:

  1. // Game.ts
  2. update (deltaTime: number) {
  3. this.configuration.updateConfig(deltaTime);
  4. this.gameData.updateData(deltaTime);
  5. this.menu.updateMenu(deltaTime);
  6. }

Control the execution order of components on the same node

The execution order of component scripts on the same node can be controlled by the order of the components in the Inspector panel. The components arranged above will be executed before the components arranged below. We can adjust the arrangement order and execution order of the components through the Move Up and Move Down menus in the gear button at the upper right corner of the component.

If there are two components: CompA and CompB, their contents may be similar to this example:

  1. // CompA.ts
  2. import { _decorator, Component, Node } from "cc";
  3. const { ccclass, property } = _decorator;
  4. @ccclass("CompA")
  5. export class CompA extends Component {
  6. onLoad () {
  7. console.log('CompA onLoad!');
  8. }
  9. start () {
  10. console.log('CompA start!');
  11. }
  12. update (deltaTime: number) {
  13. console.log('CompA update!');
  14. }
  15. }
  16. // CompB.ts
  17. import { _decorator, Component, Node } from "cc";
  18. const { ccclass, property } = _decorator;
  19. @ccclass("CompB")
  20. export class CompB extends Component {
  21. onLoad () {
  22. console.log('CompB onLoad!');
  23. }
  24. start () {
  25. console.log('CompB start!');
  26. }
  27. update (deltaTime: number) {
  28. console.log('CompB update!');
  29. }
  30. }

When CompA is above CompB on the Inspector panel, the output may be this way:

  1. CompA onLoad!
  2. CompB onLoad!
  3. CompA start!
  4. CompB start!
  5. CompA update!
  6. CompB update!

After moving CompA under CompB in Inspector by Move Down in the upper right corner of the CompA component settings menu, the output may be this way:

  1. CompB onLoad!
  2. CompA onLoad!
  3. CompB start!
  4. CompA start!
  5. CompB update!
  6. CompA update!

Set component execution priority

If the above method still cannot provide the required control granularity, developers can also directly set the executionOrder of the component. executionOrder affects the execution priority of the component’s life cycle callback. The smaller the executionOrder, the earlier the component will be executed relative to other components. The executionOrder defaults to 0, so if it is set to a negative number, it will execute before other default components. Example:

  1. // Configuration.ts
  2. import { _decorator, Component, Node } from "cc";
  3. const { ccclass, executionOrder } = _decorator;
  4. @ccclass("Configuration")
  5. @executionOrder(-1)
  6. export class Configuration extends Component {
  7. onLoad () {
  8. console.log('Configuration onLoad!');
  9. }
  10. }
  1. // Menu.ts
  2. import { _decorator, Component, Node } from "cc";
  3. const { ccclass, executionOrder } = _decorator;
  4. @ccclass("Menu")
  5. @executionOrder(1)
  6. export class Menu extends Component {
  7. onLoad () {
  8. console.log('Menu onLoad!');
  9. }
  10. }

By setting it as above, Configuration.ts‘s onLoad will be executed before Menu.ts‘s onLoad method.

Note: executionOrder is only valid for onLoad, onEnable, start, update and lateUpdate, but not valid for onDisable and onDestroy.