Home Game Development Universal entry to variables – Cocos Creator

Universal entry to variables – Cocos Creator

0
Universal entry to variables – Cocos Creator

[ad_1]

Hello everybody. There is a query. Let’s say I’ve some form of state widespread to the entire scene.

  1. Where is it higher to connect the script? (you possibly can’t appear so as to add to the scene itself)
  2. 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]

LEAVE A REPLY

Please enter your comment!
Please enter your name here