8 Replies Latest reply on Nov 17, 2006 12:39 PM by digitellinc

    _global variable lost outside of function?

    raylaur Level 1
      Vexing riddle: why are my global variables undefined outside of the functions that use them?

      Loading XML file and creating arrays from the child nodes. Like this:

      var modules:Array = this.firstChild.childNodes;
      _global.moduleContents = new Array();
      for (var i = 0; i<modules.length; i++) {
      _global.moduleContents.push(modules .attributes.contents);

      then I have another function that recursively extracts each index from _global.moduleContents array and pushes them into a _global. pageContents array. (see attached code)

      I made sure to use _global. in front of all global variables. The code workd flawlessly in itself. I am able to trace the whole array or single array indices from within the functions.

      But they are undefined outside of the functions! I checked to make sure there is no local variable of the same names - in every case I use _global before the variable name.

      The crazy part of it is I tried testing some simple functions with global variables in a new movie to understand the scope issues and they worked fine!

      function messages() {
      _global.greetings = ["hello", "hi", "how are you?"];
      function sayHi() {
      _global.greetings.push("how goes it?");
      trace("this is outside: "+_global.greetings);

      Why not in this:
        • 1. Re: _global variable lost outside of function?
          are you defining the global variables out side of your functions ?
          • 2. Re: _global variable lost outside of function?
            raylaur Level 1
            I have nested functions. All are defined in one function or other....but according to Flash documentation a global variable can be used anywhere..is that not the case? I'll try declaring global variables outside of the functions and see how that works. At this point I'll try anything.
            • 3. _global variable lost outside of function?
              raylaur Level 1
              I took _global.pageContents, the array I want to access throughout the program and declared it at the top of the page outside of all functions. I gave it a temporary value of [1,2,3]. I then added a trace inside the loadContents function that seeds it and one outside of all functions.

              Result: traced inside the function showed 1,2,3...plus the data that was pushed to it by the function. As it should be.
              traced outside the function only showed 1,2,3...doesn't show any loaded data

              so it looks like that one global variable is being treated as it they were two different variables . What am I missing or is this some kind of fluke in Actionscript or the player? I publishing as Flashplayer 8 Actionscript 2.0
              • 4. Re: _global variable lost outside of function?
                digitellinc Level 1
                odd, the only thing I can think of would be that you have a var pageContents defined some where else, where you don't use _global.pageContents, so this local variable is being used outside of your function. check to make sure you don't have a mc named pageContents this could be causing the error as well.
                • 5. Re: _global variable lost outside of function?
                  raylaur Level 1
                  thanks for you replies.

                  I just did a document search for the word pageContents and the only ones that showed up were _global. Also no mc's with that name.
                  • 6. Re: _global variable lost outside of function?
                    digitellinc Level 1
                    ouch, sorry I can't be more of a help.
                    • 7. Re: _global variable lost outside of function?
                      raylaur Level 1
                      One possible explanation I just came acrsoss is that the trace is being called before the onload has finished executing...even though he trace is located outside the function further down the page. So it's undefined. But when placed in the onLoad if (success) statement it traces correctly. A misunderstanding of how the code is being executed perhaps...thinking that because I write it in a linear way, it's executing in the way its written.
                      • 8. Re: _global variable lost outside of function?
                        digitellinc Level 1
                        Gotcha, what is happening is that you call a loading script that basically works in the background of other scripting operations, you can call multiple load actions and they will all execute at the same time, because you have your trace call after the load action and you are not waiting for that file to be loaded you are getting an 'undefined' var.

                        You may want to put something in at the end of all of your if (success) statements like

                        _global.docstructureLoaded = true;

                        then say this on your next load function;

                        _global.pageContentsLoaded =true;

                        then in your root area


                        if(_global.pageContentsLoaded==true && .... ){
                        //when all your loads are done do this ...


                        Just to make sure that you arn't calling any other functions, or moving your timeline ahead before your data gets to the right place.