1

I've been trying to add data into my array for sometime now and it doesn't work. I have the following code:

function OBJMesh(file)
{
    this.modelVertex = [];
    this.modelColor = [];
    var that = this;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function ()
    {

        if(rawFile.readyState == 4)
        {
            if(rawFile.status === 200 || rawFile.status === 0)
            {
                var allText = rawFile.responseText;
                var lines = allText.split("\n");


                for(var i = 0; i < lines.length; i ++)
                {
                    var lineData = lines[i];
                    var lineString = lineData.split(" ");

                    if(lineString[0] === "v")
                    {

                        var x = parseFloat(lineString[1]);
                        var y = parseFloat(lineString[2]);
                        var z = parseFloat(lineString[3]);

                        /*
                        this.modelVertex.push(x);
                        this.modelVertex.push(y);
                        this.modelVertex.push(z);

                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(0.0);
                        this.modelColor.push(1.0);
                        */

                        that.modelVertex.push(10.0);

                        //document.getElementById("textSection").innerHTML = "testing";
                    }

                }
            }
        }
    }

    rawFile.send();

}

OBJMesh.prototype.getModelVertex = function ()
{
    return this.modelVertex;
};

OBJMesh.prototype.getModelColor = function ()
{
    return this.modelColor;
};

If I comment out the this.modelVertex.push(10.0); it gets pass the error and prints out "testing". But if I uncomment it, it gets stuck there and won't print anything out. Why is it doing this? how can I solve it so it actually pushes the given data to the this.modelVertex array?

Many Thanks

Edit: I have edited my code after dystroy told me what to do and it do work when I try to print the values in the OBJMesh constructor (shown above), but when I try to do this by creating the object in my main function (shown below) it doesn't print anything.

var cubeModel;

function main()
{
    cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj");

    document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
}

1 Answer 1

5

this isn't your new instance of OBJMesh in the callback but the XMLHttpRequest.

Start by referencing the desired object just before defining the callback :

var that = this;
rawFile.onreadystatechange = function ()

then use it :

that.modelVertex.push(10.0);

Answer to the question in your edit :

Your constructor contains an asynchronous request. Which means your array isn't available immediately but later.

A solution would be to pass a callback to the constructor :

function OBJMesh(file, doAfterInit) {
    this.modelVertex = [];
    this.modelColor = [];
    var that = this;
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, true);
    rawFile.onreadystatechange = function () {
        if(rawFile.readyState == 4)
        {
            if(rawFile.status === 200 || rawFile.status === 0)
            {
                var allText = rawFile.responseText;
                var lines = allText.split("\n");
               for(var i = 0; i < lines.length; i ++)
                {
                    var lineData = lines[i];
                    var lineString = lineData.split(" ");
                    if(lineString[0] === "v"){
                        that.modelVertex.push(10.0);
                        if (doAfterInit) doAfterInit();
                    }
                }
            }
        }
    }
    rawFile.send();
}

...


cubeModel = new OBJMesh("file:///Users/Danny/Desktop/3DHTMLGame%202/cube.obj", function() {
    document.getElementById("textSection").innerHTML = cubeModel.getModelVertex();
});

But having a class here doesn't look like a smart idea.

Sign up to request clarification or add additional context in comments.

5 Comments

@danronmoon If you mean inside the callback, yes, it's the XmlHttpRequest.
does that mean my 'OBJMesh.prototype.getModelVertex' needs to be changed to 'return that.modelVertex;' too?
@Danny no, it's fine. The problem was only in the callback on the XmlHttpRequest object.
@dystroy many thanks for your advice and answer, but the problem kind of still appears (look at my edited post(above))
Many thanks for anwering my second question. You said that creating a class here doesn't look like a smart idea, can you please tell me what would you advise me to do then? Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.