3 Replies Latest reply on Apr 4, 2014 7:13 AM by Xoco

    When is a variable global?

    NextStepTV Level 1

      We have a problem setting a global variable.

       

      I have a Stage with a single Symbol. The Stage contains this action on compositionReady:

      var varfoo = 'varbar';

      sym.symfoo = 'symbar';

      alert('After compositionReady, varfoo is '+varfoo+'.');

      alert('After compositionReady, symfoo is '+sym.getComposition().getStage().symfoo+'.');

       

      These alerts display the values of both variables just fine at the start of the animation.

      However...

      Inside the Symbol, the end of its Timeline contains this action:

      alert('End of Symbol Timeline');

      alert('varfoo is '+varfoo+'.');

      alert('symfoo is '+sym.getComposition().getStage().symfoo+'.');

       

      Only the first alert appears, which indicates that neither variable is global.

      Why are they not global? I thought that the Stage.compositionReady event handler was the place to store functions and variables that are global to all symbols.

        • 1. Re: When is a variable global?
          mlprouty Level 1

          I put globals at the top of ...edgeAction.js after var Composition. 

           

          var Composition = Edge.Composition, Symbol = Edge.Symbol; // aliases for commonly used Edge classes

             var gVid;  

             var htmlText = '';

             //Edge symbol: 'stage'

             (function(symbolName) {

          1 person found this helpful
          • 2. Re: When is a variable global?
            elainecc Adobe Employee

            compositionReady is a block of code that is run when the composition is loaded, but it has its own scope.  So if you declare something as a variable in compositionReady, it's only available within that block and is not accessible to any other event handler in the Stage symbol.  There are two ways around this:

             

            1. Declare a variable, as mlprouty suggests, either in Stage scope or Composition scope.  (mlprouty's example is in composition scope, and if you declare something after the "function(symbolName) { " line, it would be accessible anywhere in the stage, but not accessible by any other symbols.)
            2. Declare your variable in compositionReady and attach it to the Stage symbol by using getVariable() and setVariable().

             

            Hope that helps,

             

            -Elaine

            1 person found this helpful
            • 3. Re: When is a variable global?
              Xoco Level 3

              And what is your recommendation to declare global functions (not global variables) that are accessible from any event of any element (except symbols)? the first way in Stage scope or Composition scope too?

               

               

              Thanks.