[ad_1]
Hello everybody. There is a query. Let’s say I’ve some form of state widespread to the entire scene.
- Where is it higher to connect the script? (you possibly can’t appear so as to add to the scene itself)
- How do I get to this state from one other script (besides globalThis)
So mainly what you need is a part that act like a singleton ?
I feel you possibly can obtain it like this :
import { _decorator, Component } from "cc";
const { ccclass, executionOrder } = _decorator;
@ccclass("SingletonComponent")
@executionOrder(-1)
export class SingletonComponent extends Component {
personal static _instance: SingletonComponent;
public static get occasion(): SingletonComponent {
return this._instance;
}
protected onLoad(): void {
if (SingletonComponent._instance == null) {
SingletonComponent._instance = this;
} else {
throw "Do not connect a number of cases of SingletonComponent on the scene.";
}
}
}
As for the place to connect it, when you want entry to it from a number of scenes, i might recommend a presistent node : Loading and Switching Scenes · Cocos Creator
If you don’t make use of the actual fact it’s a part although, a basic singleton that don’t extends something could be greatest.
[ad_2]