Problem with LoadVars in a Class method

Hi, this is my first post here.

I have been going totally crazy with this problem since yesterday and I hope someone here can help me with this.

I am writing a custom class called diagram and I have a method called readDiagramTextFile() which reads from a text file (path passed as a parameter to the method) and returns a variable read from the text file. I’m using LoadVars for this. Here is the code for the method:


    function readDiagramTextFile(filePath:String):String{
            var diagramLV:LoadVars = new LoadVars();
            var returnText:String;
            
            diagramLV.onLoad = function(success:Boolean){
                if(success){
                    returnText = this.variableName;
                    trace(returnText);
                } else {
                    trace("Error while loading the text.");
                }
            }
            
            diagramLV.load(filePath);
            
            trace(returnText);
            return returnText;
    }

I want this method to return the value of the variable from the text file. However it is not working as it should. The onLoad() gets executed after the “return” statement, hence the function ends up returning an empty string.

The first trace (inside the onLoad) returns the text I want but the second trace (above the return) gives “undefined”.

Is there any way to make sure the load is complete before I return the value? Is this a problem with scope? It’s driving me crazy and I have already spent almost 10 hours trying various things getting this to work! :scream:

Any help will be greatly appreciated!