I making a loader of .obj models for WebGL on javascript, and the problblem i'm having appears to be something with the XMLHttpRequest method, let me explain;
var textureArray=[];
loadTexture=function(name){
var request = new XMLHttpRequest();
request.open("GET", name+'.mtl');
request.onreadystatechange = function () {
if (request.readyState == 4) {
//do the stuff to put the textures in textureArray
}
}
request.send();
}
And let's say the method that uses it is this:
loadObjModel=function(name){
loadTexture(name);
//Check if the texture loaded
}
The thing is, when i say "Check if the texture loaded", the variable "textureArray" it's empty, but if i check immediately after i "do the stuff to put the texture in textureArray" (inside the onreadystatechange function) it does show the textures loaded correctly. I found what the problem is, it takes time to read the file and loop through the lines, so at the moment i "Check if the texture loaded" it hasn't finished, but when i do it at the end of "do the stuff to put the texture in textureArray" sows it loaded correctly because it had to wait to load everything and then show what the textureArray had.
The big problem here is that, when i try to use the textures i loaded at "Check if the texture loaded", i can't...because they aren't there, and i have seen webGL loaders douing it in this exact way and not having that problem. Wat could i be douing wrong?, thanks for the thoughts :)