Jump to content

Thread problem with BABYLON.SceneLoader.ImportMesh


saldukoo
 Share

Recommended Posts

Hello. Having a problem with BABYLON.SceneLoader.ImportMesh.

I think it is thread related. But i dont know how to fix it. Here's my code:

function Player() {
    this.mesh = null;
} 

...

player = new Player();
BABYLON.SceneLoader.ImportMesh("", "/assets/game/imports/", "player.babylon", scene, function (newMeshes, particleSystems, skeletons) {
     player.mesh = newMeshes[0];
});

Here i am declaring the player class and then using it inside the createScene() -function. This works just fine in the render loop.

Now i want to move the import statements to the player class. Consider the following adjustment:

function Player() {
    this.mesh = null;
    this.addTo = function(scene) {
       BABYLON.SceneLoader.ImportMesh("", "/assets/game/imports/", "player.babylon", scene, function (newMeshes, particleSystems, skeletons) {
           this.mesh = newMeshes[0];
       });
    };
} 

...

player = new Player();
player.addTo(scene);

This instead, doesn't work. The render loop thinks player.mesh is null regardless of time. How should i fix this thing?

Link to comment
Share on other sites

Hi,
I believe you are missing bind for your function to correctly set this inside the function.

this.addTo = function(scene) {
       BABYLON.SceneLoader.ImportMesh("", "/assets/game/imports/", "player.babylon", scene, function (newMeshes, particleSystems, skeletons) {
       this.mesh = newMeshes[0];
    }.bind(this));
}.bind(this);

Cheers.

Link to comment
Share on other sites

Hi,

JavaScript has no threads, It has asynchronicity. which is much nicer :)

When you add the player to the scene, the mesh is not yet set, since it first needs to be loaded and parsed. You can only add the player AFTER the mesh was assigned. To do this, simply move your addTo call inside the success callback:

function Player() {
    this.mesh = null;
    var that = this;
    this.addTo = function(scene) {
       BABYLON.SceneLoader.ImportMesh("", "/assets/game/imports/", "player.babylon", scene, function (newMeshes, particleSystems, skeletons) {
           this.mesh = newMeshes[0];
           that.addTo(scene)
       });
    };
} 

...

player = new Player();

This is a classic callback/async JS question. 

Link to comment
Share on other sites

oh! wait, That was wrong :)

function Player() {
    this.mesh = null;
    var that = this;
    
    this.addTo = function(scene) {
       BABYLON.SceneLoader.ImportMesh("", "/assets/game/imports/", "player.babylon", scene, function (newMeshes, particleSystems, skeletons) {
            that.mesh = newMeshes[0];
        });
    };
} 

...

player = new Player();
player.addTo(scene);

I just realized what you were trying to do :)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...