How to get objects generated by scripts on babylon.js Editor v4.0

Hi, @julien-moreau

As you know, I try the new wonderful editor and have a question. Is it possible for .ts files to get objects
generated by scripts?

For example, “ground” in the picture is located preview and graph window.

I can get the objects by the following script.

import { Mesh, FreeCamera,Scene } from "@babylonjs/core";
import { fromScene} from "../tools";

export default class example extends Mesh{

    @fromScene("ground")
    private _ground :Mesh;

    public onStart(): void {
      console.log(this._ground.name);
       
    }
}

Then, console shows “ground”.

On the other hands, I can not get the object “ball” generated by class example1.

import { Mesh, FreeCamera,Scene } from "@babylonjs/core";
export default class example1 extends Mesh{
    public onStart(): void {
       const ball = Mesh.CreateSphere("ball", 10, 1);
    }
}
import { Mesh, FreeCamera,Scene } from "@babylonjs/core";
import { fromScene} from "../tools";
export default class example2 extends Mesh{
    @fromScene("ball")
    private _ball : Mesh;
    public onStart(): void {
       console.log(this._ball.name);
    }
}

Then, the error below shows in browser.

ball.ts:57 Uncaught TypeError: Cannot read property 'name' of null
    at Mesh../src/scenes/scene/ball.ts.ball.onStart (ball.ts:57)
    at Observer.callback (tools.ts:33)
    at Observable../node_modules/@babylonjs/core/Misc/observable.js.Observable.notifyObservers (observable.js:286)
    at Scene../node_modules/@babylonjs/core/scene.js.Scene.render (scene.js:3366)
    at index.ts:32
    at Engine../node_modules/@babylonjs/core/Engines/engine.js.Engine._renderFrame (engine.js:830)
    at Engine../node_modules/@babylonjs/core/Engines/engine.js.Engine._renderLoop (engine.js:845)

Is it a way to get such objects in scripts?

Thanks in advance

Hey @Limes2018!

Unfortunately this will not be possible because the @fromScene decorator is computed before all .onStart functions are called. This ensures that all decorated properties are availalbe in the .onStart functions of your scripts.

Anyway what I can add is an “onInitialize” function that is called immediately. This would be typically where you can create the ball and then all .onStart functions of other scripts will have access to the ball. What do you think ?

Hi, @julien-moreau

Anyway what I can add is an “onInitialize” function that is called immediately. This would be typically where you can create the ball and then all .onStart functions of other scripts will have access to the ball.

That’s awesome idea!

Hi, @julien-moreau
Sorry for late reply. I confirmed a new function “onInitialize()” method like this at beta7.

//sourceA.ts
    public onInitialize(): void {
         const ball = Mesh.CreateSphere("ball", 10, 1);
         ball.name="testball";
    }

//sourceB.ts
    @fromScene("testball")
    private _testball:Mesh;

    public onStart(): void {
        console.log(this._testball.name);
    }

console.log of sourceB.ts show the name of _testball as “testball”.

Great thanks!!

1 Like