Skip to main content
alessiozingoni
Participant
May 28, 2018
Answered

Get external txt file content as string variable

  • May 28, 2018
  • 1 reply
  • 771 views

Hi everyone,

I apologize if my question is trivial, but I didn't find any solution googling it. I probably don't know even the right search keywords.

My goal is to write one function that:

  • read one txt file
  • gets txt content as string 

So that this string could be manipulated in the remaining part of the code. I found this example:

var myTextLoader: URLLoader = new URLLoader();

myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e: Event): void {

    trace(e.target.data);

}

myTextLoader.load(new URLRequest("myText.txt"));

It works nicely, but only print txt content in output window. I need content to be stored in a variable and made avaible to the rest of the code.

I tried using a global variable wich assigning target.data to. But when I changed it to this:

var myTextLoader: URLLoader = new URLLoader();

var externalText: String;

myTextLoader.addEventListener(Event.COMPLETE, onLoaded);

function onLoaded(e: Event): void {

    externalText = (e.target.data);

}

myTextLoader.load(new URLRequest("myText.txt"));

trace (externalText);

the output is null. I sense I'm missing something fundamental. Could you tell me the correct workflow to get what I'm looking for?

Thanks in advance

This topic has been closed for replies.
Correct answer ASWC

Hi Lars.

you can first assign it to a global variable: myGlobalVar = e.target.data.myTxtVar

I did something similar to what you are suggesting in second code example I reported ("var myTextLoader:.... trace (externalText);"). There I created a global variable outside of onLoaded function, and assigned e.target.data to it. But global variable came out as "null", instead of store txt content. I don't understad why it's happening. Thanks for the help.


You must learn about what it means to code with asynchronous languages. The question you ask can be answered by any AS3 beginner tutorial out there so I suggest to start with that. While those forums can be used for teaching coding that's not really their main purpose.

The bottom line:

- With asynchronous languages (like AS3) the code DOES NOT wait for the operation to complete and instead move on to the next line of code right away. If you are loading something (loading takes time) then the next line of code CANNOT be about the result of the load operation (there won't be any). That's why you have a event system to use, by registering the correct event with a listener, the listener will trigger when the event is fired and only from THAT listener will you be able to get your data and move on.

1 reply

Lars Laborious
Legend
May 28, 2018

Hi.

You need to put the trace statement inside the onLoaded function, after the externalText part.

alessiozingoni
Participant
May 28, 2018

Hi Lars, thanks for reply. Placing trace statement inside the onLoaded function will make txt content appears on Output widows, but it's not my final goal. I'm trying to get txt file content as String output, in order to manipulate it outside of onLoaded function. I'd like to have a function able to do something like this this:

var stringTXTContent: String = extractContentFromTXT(fileTXTPath);

  • Aggiungi al frasario
    • Nessuna lista di parole per Inglese -> Italiano...
    • Crea una nuova lista parole...
  • Copia
Lars Laborious
Legend
May 28, 2018

You can retrieve your text variable(s) from inside your onLoaded function.

If you have a variable called myTxtVar inside your text file, you can first assign it to a global variable: myGlobalVar = e.target.data.myTxtVar; then you can manipulate it from wherever.