• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Windows Text file, line break removal

D

Deklein

Guest
So I've been working with some included text files, along with additional text files I create in-game.
I've run into a bit of an issue, however, and I'm unsure how to resolve it.

I have one text file in particular that basically contains a list of items, one item on each line, similar to the block below:
Code:
Item1
Item2
Item3
...
Item62
Anyways, I have some code to parse the file and store each line into an array.

Code for reference:
Code:
var num = 0;
while (!file_text_eof(file))
    array[num++] = file_text_readln(file);
Now this code has been working great; I've been able to pull each item from the file no problem. However, I ran into an issue when creating a new string making use of the item I pulled.
Code:
//for this example, "array[2]" is Item2
newString = array[2] + " plus more stuff";
When displaying the variable newString, I expect the following:
"Item2 plus more stuff"

However, I instead get
"Item2
plus more stuff"

It seems to me that when I store each line and therefore each item, the line break after each item is also stored. When I go to use this information, it also puts in a line break!

I don't want it to do this but haven't found anything yet on how to stop this from happening.
I feel like I'm missing something obvious.
 
A

Aura

Guest
You can use string_replace() on the string before it is added to the array.

Code:
var str = file_text_readln(file);
str = string_replace(str, "#", "");
You can then add str to the appropriate array slot.
 

jo-thijs

Member
@Aura, that' not hopw # works though.
GameMaker stores # in strings as an actual # and it stores \# as actual 2 characters.
The draw functions just format these things to show up as a new line and a single #.

@Deklein, hi and welcome to the GMC!
The best way to deal with this is to use this code instead:
Code:
var num = 0;
while (!file_text_eof(file)) {
    array[num++] = file_text_read_string(file);
    file_text_readln(file);
}
 
Top