GDevelop
GDevelop copied to clipboard
New Lines/NewLine() Expression are treated differently if stored in variables loaded from external text files.
Describe the bug
Bluzima on the forums is trying to load data from a text file into their game, then test if there is a NewLine in it to do an action. They are using the expression StrFind(VariableString(Loadedtextvarhere),"*" + NewLine()), which should test fine if the text file has any instance of "whateverprecedingcharactersyouwanthere * (newlinehere) whateverelseyouwanthere"
However, if loaded from a text file and stored into memory, while it will display correctly if you load that string variable into a text object, it will not evaluate correctly. The same is true if you try to use the TextObject.String() expression to test against, instead of a variable.
What does work is if you manually populate a string variable with the text I pasted above. It will evaluate fine.
To Reproduce
Steps to reproduce the behavior:
- Create a text file with whatever characters you want, followed by a new line (return/enter key).
- Save the text file somewhere you can easily build the Expression-based path to in GDevelop.
- In a new GDevelop project, load that text file into a variable using the "Load text from _ into scene variable _"
- Set up a new event that uses the "Compare two values" condition, and tests StrFind(VariableString(YourVariable),"yourcharacter" + NewLine()) is greater than -1.
- Set up an action to do anything. The action will not occur.
There are a few permutations to this, but things Bluzima and I have now tested:
- String Find against a string variable loaded from a text file (above scenario) - Doesn't work
- Modify the string of a text object with the variable from 1 above, then String Find against the TextObject.String() expression. - Doesn't work.
- Create a string variable (via the variable editor or events) with the text copy+pasted from the text file, then String Find against that scene variable - DOES WORK
- Modify the string of a text object with the variable from 3 above, then String Find against the TextObject.String() expression. - DOES WORK.
Everything in red in the screenshot is done with the variable that was created by reading from the text file. Everything in blue is done with the variable that was manually created.

Here's the example project. You will need to extract it to your desktop for the file path to work. NewLineTests.zip
Other details
- Include any OS/browser version/smartphone that you're using - Win 10 64
- Which version of GDevelop are you using? The desktop app or the web-app? - 5.0.140
- Add any other context about the problem here. - Forum thread here: https://forum.gdevelop.io/t/issue-searching-for-newline-character-with-strfind/41814/15
Thanks for taking the time to report this bug!
TL;DR: CRLF vs LF.
I'll think about what is the best way to fix this but here is the issue:
The text file you provided test.txt opens like:
t
test
With a filesystem that uses CRLF terminators (Windows), the file will contain t\r\n\r\ntest whereas a filesystem that uses LF terminators (Mac, Linux) will save a string that is t\n\ntest.
And the condition searches for the string t\n
See https://stackoverflow.com/questions/1761051/difference-between-n-and-r
Just to loop back around on this from the forum post (Since I apparently completely missed your response here):
I think it would make the most sense to add an expression that removes the \r from any string you pass to it (or replaces it with NewLine()), since it wouldn't impact any strings that don't have the \r.
I just want to make sure that writing a file without CR on Windows wouldn't bring bugs: @Silver-Streak, I suppose you're on Windows. Could you open this file and let me know if it opens the same as the test.txt that you provided in the test project?
@AlexandreSi Looks good to me.
