Highlighted

Get external txt file content as string variable

New Here ,
May 28, 2018

Copy link to clipboard

Copied

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

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.

TOPICS
Development

Views

316

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Get external txt file content as string variable

New Here ,
May 28, 2018

Copy link to clipboard

Copied

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

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.

TOPICS
Development

Views

317

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 28, 2018 0
Enthusiast ,
May 28, 2018

Copy link to clipboard

Copied

Hi.

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2018 0
New Here ,
May 28, 2018

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2018 0
Enthusiast ,
May 28, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2018 0
New Here ,
May 28, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2018 0
Enthusiast ,
May 28, 2018

Copy link to clipboard

Copied

It's all about when you assign your data. You assigned e.target.data to your global variable before onLoaded was fired. That's why you should put your trace statement (and assign your data to global variables) inside your onLoaded function.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 28, 2018 0
Advocate ,
May 29, 2018

Copy link to clipboard

Copied

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.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 29, 2018 0
New Here ,
May 29, 2018

Copy link to clipboard

Copied

Thank you. I did not know of asynchronous programming notion.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 29, 2018 0