6 Replies Latest reply on Oct 5, 2007 11:26 AM by clbeech

    change string to variable (sort of)

    trigger2160
      Hi. I am making a game in flash which requires lots of similar functions, such as

      function getSomeNumber():Number{
      return number;
      }

      I was wondering if i could generalize this by doing the following. take a parameter of string defining the name of the variable. i then want to change this string to the name of the variable so that i can output the value of the variable. a bit like this

      function getSomeNumber(nameOfVar:String):Number{
      //somehow use string to find out variable name and return value of variable
      return unknown;
      }

      hope this makes sense.

      trigger2160
        • 1. Re: change string to variable (sort of)
          AScracker
          FIrst you need to convert the String to Number
          Here the Code is
          var str:String = "123";
          getSomeNumber(str);
          function getSomeNumber(nameOfVar:String):Number {
          // here you need to convert the string to number bcoz ur return type is Number
          var num:Number = Number(nameOfVar);
          return num;
          }

          ~~~SK
          • 2. Re: change string to variable (sort of)
            trigger2160 Level 1
            Im sorry but thats not what i am after. i do not want the string changed to a number. i want it changed to a variable name.

            var someThing:Number = 20;

            I have a variable as above. i want the string changed to the name of the variable (someThing) so that the variable can output its number. Hope that is clear.
            • 3. Re: change string to variable (sort of)
              clbeech Level 3
              OK, you should be able to do this, if the variable resides in the root timeline, else point to the mc containing the var:
              • 4. Re: change string to variable (sort of)
                kglad Adobe Community Professional & MVP
                actually cl, you're creating two variable references with that code:

                this[nameOfVar] and getSomeNumber().

                it's probably clearer if you do something like:

                • 5. Re: change string to variable (sort of)
                  trigger2160 Level 1
                  clbeech's code was what i was looking 4. Not sure y I need all that extra code kglad but thanks for the help anyway.
                  • 6. Re: change string to variable (sort of)
                    clbeech Level 3
                    @kglad: yeah, I see what you're saying there kg, but I think he's just wanting a return from a previously created list of variable references. I'm not sure why he wouldn't call them directly, but I think it has something to do with the user input from his system being in the form of a String, so he wasn't able to access the value of the var named similarly ... but I follow you, and I guess I should have even simplified the return as ...

                    return this[nameOfVar];

                    ... rather than setting up the extra reference in the function, but your method is the way to go for constructing a new variable from an input String and value, and I love the wildcard use here, didn't know that would work, cool.

                    OH, but hey (@trigger also) if the variable already exists, one should be able to reference it directly by calling ...

                    this['something'];

                    ... and if you've got a function which is gathering the input you could say ...

                    value = this[input];

                    ... if 'input' is in the form of a string, and it should return the value of the variable, without having to call to a function, right? that's what you're saying by 'just use it directly', I got it now :)