saldukoo Posted March 7, 2016 Share Posted March 7, 2016 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? Quote Link to comment Share on other sites More sharing options...
wladimiiir Posted March 7, 2016 Share Posted March 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
RaananW Posted March 7, 2016 Share Posted March 7, 2016 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. Quote Link to comment Share on other sites More sharing options...
saldukoo Posted March 7, 2016 Author Share Posted March 7, 2016 Hi RaananW. I tried that but unfortunately didn't work. The ImportMesh refuses to work unless it's direcly inside createScene function. This is weird. Maybe I am setting up my class wrong? Is there an example somewhere? Quote Link to comment Share on other sites More sharing options...
RaananW Posted March 7, 2016 Share Posted March 7, 2016 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 saldukoo 1 Quote Link to comment Share on other sites More sharing options...
saldukoo Posted March 7, 2016 Author Share Posted March 7, 2016 Yes! That works, thank you. I hate callbacks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.