Terminator Posted August 2, 2016 Share Posted August 2, 2016 let's say I am using this code: var board; BABYLON.SceneLoader.ImportMesh("board","/models/","models.babylon",scene,function(newMeshes){ board = newMeshes[0]; }); board.position.x += 400; board.position.x won't work since importMesh is using a callback function which is something I am aware off, but is there an alternative solution? what I want is to import a mesh from a file, then save this mesh to a variable then clone this mesh but I don't want that to happen inside the callback function for example: var originalMesh = getMeshFromFile(filename_here); for (var i= 0; i< 100; ++i) { var newClone = originalMesh.clone("index: " + index); newClone.position.y =+ i*2; newClone.material = ... } Quote Link to comment Share on other sites More sharing options...
Nabroski Posted August 2, 2016 Share Posted August 2, 2016 http://www.babylonjs-playground.com/#FYFXA#0 Good Luck Quote Link to comment Share on other sites More sharing options...
Terminator Posted August 2, 2016 Author Share Posted August 2, 2016 7 minutes ago, Nabroski said: http://www.babylonjs-playground.com/#FYFXA#0 Good Luck @Nabroski thanks for your reply, but I think using "scene.meshes" won't solve the issue in case the callback didn't finish executing.. unless I am missing something? Quote Link to comment Share on other sites More sharing options...
Nabroski Posted August 2, 2016 Share Posted August 2, 2016 try before buy Quote Link to comment Share on other sites More sharing options...
Terminator Posted August 2, 2016 Author Share Posted August 2, 2016 I just tried it, but it is just showing me the "ground1" mesh (which is the mesh that I defined using createGround()), however the "board" mesh isn't appearing Quote Link to comment Share on other sites More sharing options...
RaananW Posted August 2, 2016 Share Posted August 2, 2016 Hi people, Javascript is async (and thank god for that!). If you load an object, it will take some time until it is rendered in the scene. you will only be able to change its position after the callback was executed. changing the position before the object was loaded will not work (as you already noticed). you will have to work with callbacks, as this is how the language works . you can still have a global reference to the object, and when it is not null do something with it. but then again, what's wrong with doing it after the callback was executed? webdva 1 Quote Link to comment Share on other sites More sharing options...
Nabroski Posted August 2, 2016 Share Posted August 2, 2016 Yes RaananW is right i forgot about it sorry you have to wait before you overloading and then display the log files Keep on cloning! http://www.babylonjs-playground.com/#2CHATH#0 Quote Link to comment Share on other sites More sharing options...
Terminator Posted August 2, 2016 Author Share Posted August 2, 2016 @RaananW For sure there's nothing wrong to wait for the callback is executed, the problem is just that I don't want to execute the code inside the callback function. here is what I am thinking: 1- Import the mesh 2- get a reference about the imported mesh and clone it multiple times 3- manipulate the cloned meshes inside: scene.registerBeforeRender(function(){ }); // registerBeforeRender() so for me it is just about being able to use the imported mesh outside the callback function scope, (I think the solution is to just define a global variable for the mesh and just keep looping until it is no longer null as you mentioned?) Quote Link to comment Share on other sites More sharing options...
Terminator Posted August 2, 2016 Author Share Posted August 2, 2016 @Nabroski I was writing a reply when you posted your link, I will check it Quote Link to comment Share on other sites More sharing options...
RaananW Posted August 2, 2016 Share Posted August 2, 2016 once you start understanding the way callbacks are executed, you don't really need global variables. You could register the before render loop inside the callback. For example - you could create an external function that will be called after the callback was executed: http://www.babylonjs-playground.com/#1BUQD5 You could also create a global variable and register the "beforeRender" after the callback was executed - http://www.babylonjs-playground.com/#1BUQD5#1 . So many ways of achieving it without inspecting constantly if mesh is not null. Terminator, webdva and Nabroski 3 Quote Link to comment Share on other sites More sharing options...
Nabroski Posted August 3, 2016 Share Posted August 3, 2016 @RaananW Thank you for this! I'm not so good programmer, but mostly it's enoughhttp://www.babylonjs-playground.com/#2CHATH#4 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.