Windows Reading strings from .csv file - can't create line breaks/newlines

Hello,

I am reading lines of dialogue into my game via importing from a .csv file into a ds_grid. The game has been interpreting my line breaks literally instead of doing a paragraph break. In other words, it is reading '\n' as literal characters instead of as an escape character. When I tried to use actual paragraph breaks in the .csv file the game wouldn't compile. Any easy workarounds for this? Thank you in advance for the help!


t7nj8ldabsy61.png
 

chamaeleon

Member
\n written as text in a text file (that is not program source code) is not a newline. It is \ and n, 2 separate characters. In source code it is (for a given language where this is the case of course). A literal newline in the text file is a newline.

If you don't want a literal newline in your file (because parsing it would be hard or impossible in such a case), you use some other convention, like \n (or anything else, it doesn't matter). You replace the characters with an actual newline, after reading in all the content, using string functions.
 
\n written as text in a text file (that is not program source code) is not a newline. It is \ and n, 2 separate characters. In source code it is (for a given language where this is the case of course). A literal newline in the text file is a newline.

If you don't want a literal newline in your file (because parsing it would be hard or impossible in such a case), you use some other convention, like \n (or anything else, it doesn't matter). You replace the characters with an actual newline, after reading in all the content, using string functions.
Thank you, I appreciate this context. Is there an example of how to do these string functions somewhere? I'm unfamiliar with them.
 

chamaeleon

Member
Thank you, I appreciate this context. Is there an example of how to do these string functions somewhere? I'm unfamiliar with them.
Every function in the manual probably has one or more examples.

In your case, replace the characters \n with a newline. In the substring to find and replace \\ represents the character \, and is followed by an n (which is not part of the escape sequence preceding it). \n in the replacement string for the substring is a string containing a single character, the newline.
GML:
var s = "some string\nanother string";
s = string_replace_all(s, "\\n", "\n");
Caveat, this does not take into account any way to escape the escape character. If your text file contains \\n because you wanted your text to actually display what you are currently incorrectly displaying you'd have to step through and replace sequences character by character instead, otherwise you get a text that ends with a \ on one line before the newline that was put in place. It's up to you to either code this, or ignore this because you don't intend to write such text, or choose a different character sequence than \n to represent newline.
 

FrostyCat

Redemption Seeker
You can account for standard string escape sequences by prepending and appending a double quote, then feeding it through json_parse. Examples:
GML:
var jsonstr = @'"Alice said to Bob, \"Hello World!\""';
show_debug_message(json_parse(jsonstr)); //Alice said to Bob, "Hello World!"
GML:
theText = json_parse(@'"' + csvGrid[# 0, 0] + @'"');
 
Every function in the manual probably has one or more examples.

In your case, replace the characters \n with a newline. In the substring to find and replace \\ represents the character \, and is followed by an n (which is not part of the escape sequence preceding it). \n in the replacement string for the substring is a string containing a single character, the newline.
GML:
var s = "some string\nanother string";
s = string_replace_all(s, "\\n", "\n");
Caveat, this does not take into account any way to escape the escape character. If your text file contains \\n because you wanted your text to actually display what you are currently incorrectly displaying you'd have to step through and replace sequences character by character instead, otherwise you get a text that ends with a \ on one line before the newline that was put in place. It's up to you to either code this, or ignore this because you don't intend to write such text, or choose a different character sequence than \n to represent newline.
Thank you very much!
 
You can account for standard string escape sequences by prepending and appending a double quote, then feeding it through json_parse. Examples:
GML:
var jsonstr = @'"Alice said to Bob, \"Hello World!\""';
show_debug_message(json_parse(jsonstr)); //Alice said to Bob, "Hello World!"
GML:
theText = json_parse(@'"' + csvGrid[# 0, 0] + @'"');
This owns, I'll try this first. Thank you much for your help.
 
You can account for standard string escape sequences by prepending and appending a double quote, then feeding it through json_parse. Examples:
GML:
var jsonstr = @'"Alice said to Bob, \"Hello World!\""';
show_debug_message(json_parse(jsonstr)); //Alice said to Bob, "Hello World!"
GML:
theText = json_parse(@'"' + csvGrid[# 0, 0] + @'"');
Sorry for the noob question, what does the @ symbol do here?
 

FrostyCat

Redemption Seeker
Sorry for the noob question, what does the @ symbol do here?
That is the string literal syntax:
The Manual entry on strings said:
You can also create verbatim string literals by preceding the whole string with the @ character:
GML:
var test = @"
Line breaks
over multiple
lines
";
The above code will render the string over multiple lines as if there was a line break escape character included. A verbatim string literal is similar to previous GameMaker version string literals but they also use double or single quotes and must be prefixed by an @ symbol, they can be broken over multiple lines in the code file and they DO NOT support escaped characters i.e. @"Hello\World" will not try to escape the W on World and will be stored verbatim.
For comparison, these do the same thing:
GML:
theText = json_parse(@'"' + csvGrid[# 0, 0] + @'"');
GML:
theText = json_parse("\"" + csvGrid[# 0, 0] + "\"");
 
Top