[Solved] How to read whole text file not just one line ?

yoplalala

  • *
  • Posts: 1632
Hi !

I've read this thread http://community.stencyl.com/index.php/topic,24729.0.html and found
the code var s:String = nme.Assets.getText("assets/data/dict.txt");
The problem is that it reads only the first line of the file. How can you read all the file ?

Thanks

« Last Edit: January 29, 2015, 08:48:21 am by yoplalala »

yoplalala

  • *
  • Posts: 1632
In fact the var s contains all the text. How do I know that ? When I draw it on screen all the line appears  on the same line (line break is omitted in display).
But how do you split the lines ? i've tried split  s using separator \n , but it doesn't do the trick :'( .

Justin

  • *
  • Posts: 4645
Custom Code Event:
Code: [Select]
public static var newlinePattern:EReg = ~/[\r\n]+/g;

public static function getFileLines(filename:String):Array<Dynamic>
{
return newlinePattern.split(openfl.Assets.getText("assets/data/"+ filename));
}

lines is a LIST attribute.
someText.txt is a text file in the extras folder.

[set lines to [code : getFileLines("someText.txt") ] ]

Try that.
For Live Support: Join our discord server and ping me @justin.
I'm most often available between 10am and 10pm Japan time. (GMT+9)

Bombini

  • Posts: 1398
Hi guys,

thanks for the input an the journal thread but maybe i am too stupid ;)
Could you help/show me how to setup the custom block (to include the code and load the text from the file)?

Will exchange what ever you need from Space Pirates ;)


« Last Edit: January 28, 2015, 04:39:08 am by Bombini »

yoplalala

  • *
  • Posts: 1632
Thanks it works great Justin

I finally managed to split the Lines by using the splitter "code:String.fromCharCode(13) + String.fromCharCode(10)"
But I will take your method :)

For Bombini


Bombini

  • Posts: 1398
Ok thanks a lot!
Wow this will make my life much easier!

What i still dont understand (and sorry this is not my strenght):
  • How do i keep the lines from the text file (three lines in textfile, but game draws all lines on one (see below)?
  • How i get/draw only specific lines from the textfile (basically to use one textfile for the whole game)?
  • How do i get rid of the brackets while drawing?


yoplalala

  • *
  • Posts: 1632
It's a list now. So if want only the first line, "draw the get item 0 from the List" . I don't know if I'm clear.

I think it'll answer your 3 questions :)

Bombini

  • Posts: 1398
Oh boy ;)
Shame on  me!
I will post the working system soon in the SP thread!
Thanks guys

yoplalala

  • *
  • Posts: 1632
lol . Bumped the thread to put this link http://community.stencyl.com/index.php/topic,35620.0.html  in fact there's an extension