5 Replies Latest reply on Oct 29, 2014 6:18 PM by eLearning Guy

    set and get variables - what am I doing wrong?

    eLearning Guy Level 1

      I have a relatively small composition and I have almost everything figured out except one major problem - I need to set a variable and then retrieve the results. I've watched the Paul Trani video on this, and read the docs, and it's just not working for me.

       

      I have a symbol on the stage with the following code:

       

      ***************************

       

      // insert code for mouse click here

      // set the selected answer for later correct/incorrect omparison

      var current_selection = sym.setVariable("CURRENT_SELECTED_EQUATION", "1");

      alert(sym.getVariable(current_selection));

       

      ****************************

       

      When the composition is previewed I get simply 'undefined' in the alert.

       

      What am I doing wrong?

        • 1. Re: set and get variables - what am I doing wrong?
          eLearning Guy Level 1

          So I figured out how to set and retrieve a locally scoped (i.e. set within the same symbol) variable, like asked above. The trick seems to be to ignore the Edge Animate JS API and just use simple JS, like so:

           

          var current_selection = '1';

          alert(current_selection);

           

          duh : (

           

          But now what I really need to do is to set and retrieve a variable that was originally set on the Stage. So here is what my code looks like:

           

          Stage's onCompositionReady event:

          var current_selection = '0';

          alert(current_selection); // nothing happens - no alert box appears when previewed in the browser

           

          A Symbol I have on the Stage, with code on the Click event:

          alert(current_selection); // returns undefined

          • 2. Re: set and get variables - what am I doing wrong?
            vivekuma Adobe Employee

            You need to use getVariable like this:

                 alert(sym.getVariable("CURRENT_SELECTED_EQUATION"));


            hth,

            Vivekuma

            • 3. Re: set and get variables - what am I doing wrong?
              eLearning Guy Level 1

              Thanks, but it doesn't seem to be working.

               

              In a blank, clean project I put the following code in the Stage's compositionReady action:

               

              var CURRENT_SELECTION = '0';

              alert(sym.getVariable("CURRENT_SELECTION"));

               

              The alert now appears but it shows 'undefined'.

              • 4. Re: Re: set and get variables - what am I doing wrong?
                vivekuma Adobe Employee

                No, it doesn't work like that, you have to use the key while setting and getting like below:

                    var current_selection = sym.setVariable("CURRENT_SELECTED_EQUATION", "1");

                    alert(sym.getVariable("CURRENT_SELECTED_EQUATION"));

                 

                hth,

                Vivekuma

                • 5. Re: set and get variables - what am I doing wrong?
                  eLearning Guy Level 1

                  I got it working. So basically the API's set/getVariable commands are required for the Stage. They are not for symbols when working with variables, so that's what was confusing me.

                   

                  Here is a working example, for others in the future:

                   

                  Stage's compositionReady action:

                   

                  sym.setVariable("CURRENT_SELECTION", "0");

                  alert(sym.getVariable("CURRENT_SELECTION")); // returns '0' as expected

                   

                  To set/get the variable from a symbol on the stage:

                   

                  alert(sym.getVariable("CURRENT_SELECTION"));

                  sym.setVariable("CURRENT_SELECTION","2");

                  alert(sym.getVariable("CURRENT_SELECTION"));