Components and Component Execution Order
The component class must inherit from a class. Example:
The life cycle of a component is completely controlled by the node. Unlike ordinary class objects, components cannot be created by constructors:
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
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:
import { Component } from 'cc';
@ccclass("MyComponent")
class MyComponent extends Component {
constructor () {
console.log(this.node.name); // Error: The component is not attached to the node
}
public printNodeName () {
console.log(this.node.name);
}
}
Component execution order
// Game.ts
import { _decorator, Component, Node } from 'cc';
const { ccclass, property } = _decorator;
import { GameData } from './GameData';
import { Menu }from './Menu';
@ccclass("Game")
export class Game extends Component {
private configuration = Configuration;
private gameData = GameData;
private menu = Menu;
onLoad () {
this.gameData.init();
this.menu.init();
}
}
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
.
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:
// Configuration.ts
static updateConfig (deltaTime: number) {
}
Then call these methods in the update of the Game.ts
script:
// Game.ts
update (deltaTime: number) {
this.configuration.updateConfig(deltaTime);
this.gameData.updateData(deltaTime);
this.menu.updateMenu(deltaTime);
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.
When CompA
is above CompB
on the Inspector panel, the output may be this way:
CompA onLoad!
CompB onLoad!
CompA start!
CompB start!
CompA 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:
CompB onLoad!
CompA onLoad!
CompB start!
CompA start!
CompB update!
CompA update!
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:
// Configuration.ts
import { _decorator, Component, Node } from 'cc';
const { ccclass, executionOrder } = _decorator;
@ccclass("Configuration")
@executionOrder(-1)
export class Configuration extends Component {
onLoad () {
console.log('Configuration onLoad!');
}
}
By setting it as above, Configuration.ts
‘s onLoad
will be executed before Menu.ts
‘s onLoad method.