• 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!

\n (line break) is not being save to ini file

Hello, I have a small issue where if a string contains \n and that string is saved to an ini file, the loaded later, the string wont contain the \n portion.

Example:
Code:
description_string = "Hello Test \n This is a test";

ini_open("test");
    ini_write_string("section", "key", description_string);
ini_close();
If you view the ini file, the variable is stored like this, missing the \n part:
"Hello Test This is a test"



My current work around is this:
Code:
description_string[0] = "Hello Test";
description_string[1] = "This is a test";
description_string_full = (description_string[0] + "\n" + description_string[1]);
With this method I can save and load those variables, and the "\n" portion is kept in code.
 
M

MirthCastle

Guest
Maybe you should try a regular text file?...

Your workaround is normal if you require the two lines be strictly separate, or you want to change up the second line.

There is a built in solution if you don't care about being able to change up the second line:

When you use draw_text_ext(x, y, string, sep, w); it will automatically wrap any text within the area specified, so you don't have to set the line break manually.

This way you just save the whole line. Then let the draw_text_ext do the wrapping for you.

"Hello World!----This is a Test!"

The dashes are extra spaces so that you don't end up with "This" on the hello world line.

You would set the w to whatever "Hello World! " is.

I hope that helps!
 
Last edited by a moderator:

chamaeleon

Member
@Bladestorm Games

If it is not a requirement that the content in the ini file be easily readable by human eyes

Code:
description_string = "Hello Test \n This is a test";
ini_open("test");
ini_write_string("section", "key", base64_encode(description_string));
ini_close();
Code:
ini_open("test");
description_string = base64_decode(ini_read_string("section", "key", ""));
ini_close();
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
Default INI functions do not support multi-line keys/values, quotes inside values, and other things outside common non-standardised implementations.

I once wrote an extension that addresses these issues by implementing it's own INI reader+writer in GML.

If it's raw data, writing it as base64 or otherwise changing value to fit requirements is the other option.
 
Maybe you should try a regular text file?...

Your workaround is normal if you require the two lines be strictly separate, or you want to change up the second line.

There is a built in solution if you don't care about being able to change up the second line:

When you use draw_text_ext(x, y, string, sep, w); it will automatically wrap any text within the area specified, so you don't have to set the line break manually.

This way you just save the whole line. Then let the draw_text_ext do the wrapping for you.

"Hello World!----This is a Test!"

The dashes are extra spaces so that you don't end up with "This" on the hello world line.

You would set the w to whatever "Hello World! " is.

I hope that helps!
This option would work using "draw_text_ext()" but the problem with using it in my main project is that sometimes "description_string" can be different, example:
Code:
var choice = choose("Option Apple", "Option B", "Option LongWordHere");
description_string = (choice + " \n was selected")
Which since the string length is different it would be more difficult to find each "break point" with draw_text_ext();


\n means "replace this with a newline character."
\\n means "\n"
The problem with having \\n is it would just draw "\n" instead of actually line-breaking, but yes, haveing \\n does get stored.


@Bladestorm Games

If it is not a requirement that the content in the ini file be easily readable by human eyes

Code:
description_string = "Hello Test \n This is a test";
ini_open("test");
ini_write_string("section", "key", base64_encode(description_string));
ini_close();
Code:
ini_open("test");
description_string = base64_decode(ini_read_string("section", "key", ""));
ini_close();
This is probably the method I'll be using, although I was hoping to keep saved information readable, but I can live with a little bit being encoded

Default INI functions do not support multi-line keys/values, quotes inside values, and other things outside common non-standardised implementations.

I once wrote an extension that addresses these issues by implementing it's own INI reader+writer in GML.

If it's raw data, writing it as base64 or otherwise changing value to fit requirements is the other option.
Nice article on your extension! I may consider using it since it has some neat functions that go with it.

Thanks for all the responses guys!
 
Last edited:

chamaeleon

Member
The problem with having \\n is it would just draw "\n" instead of actually line-breaking, but yes, haveing \\n does get stored.
For readability you can use string_replace_all() to go back and forth between "\n" and "\\n" which is probably what @Tsa05 had in mind.
Code:
description_string = "Hello Test \n This is a test";
ini_open("test");
ini_write_string("section", "key", string_replace_all(description_string, "\n", "\\n"));
ini_close();
Code:
ini_open("test");
description_string = string_replace_all(ini_read_string("section", "key", ""), "\\n", "\n");
ini_close();
The base64 version is "safer" in the sense that it won't care much about anything about the string content, but perhaps the above works for your needs too.
 
For readability you can use string_replace_all() to go back and forth between "\n" and "\\n" which is probably what @Tsa05 had in mind.
Code:
description_string = "Hello Test \n This is a test";
ini_open("test");
ini_write_string("section", "key", string_replace_all(description_string, "\n", "\\n"));
ini_close();
Code:
ini_open("test");
description_string = string_replace_all(ini_read_string("section", "key", ""), "\\n", "\n");
ini_close();
The base64 version is "safer" in the sense that it won't care much about anything about the string content, but perhaps the above works for your needs too.
Oh that is a really neat way of doing it!
 
Top