18 Replies Latest reply on Jan 22, 2009 1:26 PM by clbeech

    Using a variable to change a variable

    Llama_Boy Level 1
      Hey Everyone,

      Here's the situation. I'm creating a board game which has 3 variables, p1Move, p2Move and activePlayer.
      p1Move and p2Move store the p1 and p2 location on a game board. activePlayer stored who's turn it is at the moment.

      I am loading a value in from a text file which changes the value of move. This all works fine when i designate a player directly:

      p1Move = [insert variable name],
      but i want to designate dynamically based on who's turn it is.

      Something like this:

      _root["p"+activePlayer+"Move"] += Number(this.varAmount);

      so that flash changes the value of p1Move (or p2Move depending on who's turn it is) to varAmount. varAmount is the variable being read from the textfile.

      Hopefully that all makes sense. Basically i want flash to change the variable associated with the active player only.

      Any help would be greatly appreciated, LB
        • 1. Re: Using a variable to change a variable
          kglad Adobe Community Professional & MVP
          what's wrong with your code?
          • 2. Re: Using a variable to change a variable
            Llama_Boy Level 1
            It simply doesn't work. If i use:

            _root["p"+actovePlayer+"Cash"] += Number(this.varAmount);

            it returns NaN.

            If i use:

            _root["p"+actovePlayer+"Cash"] = Number(this.varAmount);

            it works fine but i need it to add to the existing value rather than just take it


            Doesn't seem to make any sense because i'm pretty sure the code is correct

            • 3. Using a variable to change a variable
              clbeech Level 3
              sounds like previous to this call the variable is not present on the main timeline. in other words _root["p"+activePlayer+"Cash"] has not been declared - one cannot 'add' to a value that doesn't exist - thus the reason why the second statement functions. so at some point previous to this statement, you need to 'set up' the variable equal to at least zero - or you could create a condition at this point to handle the contingency, as in:

              if(_root["p"+activePlayer+"Cash"] == undefined) {
              _root["p"+activePlayer+"Cash"] = varAmount;
              }else{
              _root["p"+activePlayer+"Cash"] += varAmount;
              }
              • 4. Re: Using a variable to change a variable
                Llama_Boy Level 1
                Thanks for the replies

                The variable does exist so i'm not sure wht it isn't recognised. It's declared in a global variable on the first frame and is immediately given a value of 200.
                Could it be that flash doesn't interpret the code as an existing variable and instead is attempting to create a new one which doesn't currently have a value?

                If so, is it possible to tell flash that i am pointing to a specific variable?

                Sorry if that sounds kinda vague.
                • 5. Re: Using a variable to change a variable
                  clbeech Level 3
                  np - can you show your code that is declaring the variable.
                  • 6. Re: Using a variable to change a variable
                    Llama_Boy Level 1
                    _global.p1Cash = 200;

                    There's 6 of these in total, all written in the same way with only the number being different. e.g. p1Cash, p2Cash, p3Cash............

                    Thanks a lot for the help clbeech
                    • 7. Re: Using a variable to change a variable
                      clbeech Level 3
                      np man - remove the _global keywords. that might do it.
                      • 8. Using a variable to change a variable
                        Llama_Boy Level 1
                        Maybe getting a little closer to the problem now.
                        I'm calling the function in a later frame so declaring p1Cash without the _global means the function doesn't know what it is. I've got round this by keepng the global variable, but when i run the function in the later frame i use:
                        p1Cash = p1Cash
                        Then the local variable takes the value of the global but the function can still use it. Seems to work this way
                        • 9. Re: Using a variable to change a variable
                          clbeech Level 3
                          nope - when a variable is declared on the main timeline it is persistent, and available when called by the correct path ie. _root. by 'targeting' the var using the syntax you've shown, the _global is not necessary, in fact probably the cause of the issue. because (quote from docs):

                          "Note: When setting the value of a global variable, you must use the fully qualified name of the variable, e.g. _global.variableName. Failure to do so will create a local variable of the same name that obscures the global variable you are attempting to set."

                          so in other words - you will not be able to target the variable using a dynamic structure as you are wanting to do - unless you remove those keywords.
                          • 10. Re: Using a variable to change a variable
                            Llama_Boy Level 1
                            Seems strange then that it still doesn't work. I have removed the _global from the variable but still no joy. I also tried keeping the _global in and using:

                            _root["_global.p"+activePlayer+"Cash"] += Number(this.varAmount);

                            but that didn't work either. Compiles fine but doesn't make any change to the variable.
                            • 11. Re: Using a variable to change a variable
                              clbeech Level 3
                              where is the variable 'activePlayer' located? is that stored on the main timeline as well? if so you will also need to 'target' that variable in a similar manner in order to retrieve it, probably:

                              _root["p"+_root.activePlayer+"Cash"] += varAmount;
                              • 12. Re: Using a variable to change a variable
                                Llama_Boy Level 1
                                Yeah it's on the main timeline.

                                I tried using the _root["p"+_root.activePlayer+"Cash"] but still had no joy.

                                Placing the p1Cash = p1Cash on the same frame as the function call seems to work fine, but i still have to repeat that line for each of the 6 players. I also have to use that code for credits and movement so in some cases, i have to repeat that line 18 times!

                                Think i might be goin a little mad looking at this now



                                • 13. Re: Using a variable to change a variable
                                  clbeech Level 3
                                  ok - two things to check into:

                                  1) are there any 'left over' instances of using the _global keyword remaining in the file (did you remove 'all' of them)

                                  2) where is the var: varAmount located? and how is declared and adjusted.

                                  from the frame you are working on this in try tracing:

                                  trace(_root["p"+_root.activePlayer+"Cash"]);

                                  ...what do you get as a return.

                                  • 14. Re: Using a variable to change a variable
                                    Llama_Boy Level 1
                                    sorry for the delay in replying.

                                    Ok so, i have removed all of the global variables but the problem persists.
                                    The varAmount variable is stored in a text file which gets read in. This works fine and i have tried tracing the variables from the text file several times.
                                    When i use:

                                    trace(_root["p"+_root.activePlayer+"Cash"]);

                                    it returns undefined. However, i've found that using just:

                                    trace(_root["p"+activePlayer+"Cash"]);

                                    it works, but only some of the time. It either returns p1Cash or undefined. There doesn't seem to be any reasoning behind whether it works or not
                                    • 15. Re: Using a variable to change a variable
                                      Llama_Boy Level 1
                                      This works though i know it's terrible coding:

                                      _global.p1Move = 1;
                                      _global.p1Cash = 200;
                                      _global.p1Cred = 0;
                                      p1Cash = p1Cash;
                                      p1Cred = p1Cred;
                                      p1Move = p1Move;

                                      I have all of that in the first frame and all of the code works for the rest of the game. I tried removing the _global but nothing works without it. It does mean however that i have to repeat that code for 6 players! Not exactly pretty :(
                                      • 16. Re: Using a variable to change a variable
                                        clbeech Level 3
                                        well as the docs say above, if you 're-declare' those variables that it will 'replace' the _global instance, so i still don't think i'd go that route.

                                        now above you say the the 'activePlayer' var works 'some' of the time - but that the var is sent from a text file. I think that you might try to load the variable from the file under a different name, and the declare the named var in the root, for instance in the text file use something like:

                                        playerActive=1;

                                        then in the root timeline where you load use something like:

                                        loadVariables('file.txt');
                                        activePlayer = playerActive;

                                        however it's likely that this wont be consistent either because the file has to 'load' before the variable is instantiated (available) so I'd suggest using the LoadVars class to monitor the load and respond when it becomes available. something like:

                                        var lv = new LoadVars();
                                        lv.onLoad = function() {
                                        activePlayer = this.playerActive;
                                        }
                                        lv.load('file.txt');
                                        • 17. Re: Using a variable to change a variable
                                          Llama_Boy Level 1
                                          Thanks for the quick reply

                                          I think we got crossed wires. The loadvars is fine, there's never been any trouble with that, and the activePlayer is stored on the main timeline, not taken from the textfile. The only problem really is the duplication of variables. The _global variables are all repeated as in my last post.
                                          This works fine but is messy. If i remove the _global, it doesnt work anymore and putting in the _root into the code to target the variables doesn't seem to fix it
                                          • 18. Re: Using a variable to change a variable
                                            clbeech Level 3
                                            ok - well at this point I'm not sure what to tell ya :)

                                            can you post your file to your server for download so that we can take a look.