12 Replies Latest reply on Jul 14, 2006 3:07 PM by Rothrock

    Loading variables from txt file

    Afarchy Level 1
      Hi, I have Flash 8 Professional and was wondering how I can load a variable from an external txt file using Actionscript. I already have an Array called infotext that I need to hold various strings to display through my Flash Movie. How do I go about setting infotext[1], infotext[2], etc to equal the text from a txt file?
        • 1. Re: Loading variables from txt file
          Rothrock Level 5
          Make a text file like the following

          myVar1=Some value&
          &myVar2=4&
          &myVar3=Yet more values

          Save it as a UTF-8 and then use the LoadVars class to read it in. Check out the help files for the LoadVars class and read all the methods and events. You will find sample code to do what you need. However the data loaded won't be in a nice array and you won't be able to load it directly as an array. You will have to parse it and convert it.

          If your data won't have any commas (or some other delimiter) in it you could do something like:

          myVar=Some Text,4,Some Other Text

          You would still use the LoadVars class to load the file. But then you could break the whole thing into and array:

          myArray=myVar.split(",");

          Finally. If you have a lot of info and it is fairly structured you might want to look into using the XML class. But we won't go there 'til your say so.
          • 2. Re: Loading variables from txt file
            Generic Nickname
            More specific question here: Is sendAndLoad() supposed to be able to overwrite the variable values in the referenced text file? It is not clear in any of the documentation i have perused whether this is the case. i am able to get the variables loaded from my file on my server, but it doesn't seem to overwrite when i send. i'm wondering if it's due to the permissions on my server (though i've given the file full read and write permissions). If variables can't be overwritten via this method what methods are there? Am i going to have to rely on PHP and mySQL to overwrite variables as i pass them back and forth between Flash?
            • 3. Re: Loading variables from txt file
              Rothrock Level 5
              Flash has no ability to overwrite files. I haven't used sendAndLoad() but my understanding is that it is for working with some back end like coldfusion, php, etc. So the system at the other end will need to deal with updating a file.
              • 4. Loading variables from txt file
                Afarchy Level 1
                I'm having some difficulties doing what you suggested. I have about 24 blocks of text I need to display and some of them have a few commas, parenthesis, \n etc. I'm also not sure how I to split it into the array, and this is important because I have already structured my flash document around that array.

                What I'm really trying to do is have an easy way to convert the text in my flash document into different languages by simply changing the text from another file. That way, I can have other people around the globe change my text without having to have flash.
                • 5. Re: Loading variables from txt file
                  Rothrock Level 5
                  Check out the help file for String.split() to see how it works. That solution is only going to work for a simple text file where everything can be delimited with some character. So this probably won't work for your current situation.

                  What you will need to do is when then external file loads have some code in the LoadVars.onLoad event handler that goes through all the variables and puts them into the array. Something like this:

                  myLoadVars.onLoad=function(success){
                  if(success){
                  myArray=new Array();
                  for(i=0;i<24;i++){
                  myArray[ i ] = this["myVar"+i]
                  }
                  }

                  This assumes that you have given the variables inside of your text file a nice naming like myVar0, myVar1, etc. If you don't do it that way it will get a lot more complex.

                  In the end, I really would recommend working up an XML version of this. It is a little more complex, but for something that is designed to have 24 different chunks for several different languages it seems like a good match.
                  • 6. Re: Loading variables from txt file
                    Afarchy Level 1
                    Quick question before I continue: can I split the text with a semicolon?
                    • 7. Re: Loading variables from txt file
                      Rothrock Level 5
                      Have you read the help entry for String.split()?

                      But the answer is yes, you can use just about anything. It may be a good solution, just remember that if the semicolon appears anywhere in the actual text it will cause problems.
                      • 8. Loading variables from txt file
                        Afarchy Level 1
                        I put this on frame 2 and it gives me undefined for all myArray except 25:
                        • 9. Loading variables from txt file
                          Afarchy Level 1
                          ^^^^^^
                          • 10. Re: Loading variables from txt file
                            Rothrock Level 5
                            Should work fine. Are you sure you have all the capitalization correct? When you go to the debug–>List Variable menu, what do you see?

                            What happens if you trace(this["mytext"+3] or some such value like that.

                            One last thing I noticed. It is probably a good idea to do the load AFTER the onLoad function is defined. I don't know if it is the cause of your problem, but it could make the difference.
                            • 11. Re: Loading variables from txt file
                              Afarchy Level 1
                              Doing trace(this["mytext"+3]); gave me undefined as well.

                              In the debugger, myLoadVars was correctly defined with all of the text, but myArray was all undefined except myArray[25].

                              I moved the load and it didn't seem to change anything. =(
                              Thanks for the continued help.
                              • 12. Re: Loading variables from txt file
                                Rothrock Level 5
                                Don't know what to tell you. It works just fine on my machine. You have a text file that looks like this?

                                mytext0=something&
                                &mytext1=something else&
                                &mytext2=somethin further

                                And that is saved as UTF-8? If it is reading into the player and you see it in the debugger that should all be okay.

                                Other than that I can only think there is something else going on. Try it with a brand new file that only has this code in it. What happens then?