Loading and appending model to scene from class?

Hey. I use classes to make things organized (for example Life > Enemy > Orc > Green Orc)

And obviously to avoid putting all code in main index html file which is hardly readable

I have this file life.js:

class Life {
constructor() {
    this.health = 100;

    //executes
    alert("lol");
    // doesnt execute
    BABYLON.SceneLoader.Append("./", "box.babylon", scene, function (scene) {
    });
    
}

}

you can tell by coments what executes what doesnt. if this code is placed in index.html then it indeed adds box (my model) to the scene, but it doesnt when its not.

(my index html is just basic code from babylon homepage)

anyone knows how to make it work? Or do I have to add all objects to the scene from main index.html file?

If I understand you correctly, it might be the case to pass a scene reference to the object instance.

class Life {
    constructor() {
        this.health = 100;
    }
    
    append(scene) {
        BABYLON.SceneLoader.Append("./", "box.babylon", scene, (scene) => {});
    }
}

And in the scene builder:

let life = new Life();
life.append(this.scene);

Hope it helps.

1 Like

yeah that did the job thanks :slight_smile:

1 Like