Hi,
I often use StrFind function to look for a string in a text, (this text I often load from a .txt file in folders).
It works fine, except sometimes, with the return (line break) character, that cannot be found (sometimes).
Is there anything specific to know about this character? Does gdevelop StrFind function works in a specific way for this character?
Thanks, Iāve never tried that, didnāt know that āNewLine()ā.
Iāll do, and will give more information if I understand why is my issue happening.
Hi again, I run some tests to understand what doesnāt work well. The problem doesnāt seem to be in the newline character, but in a star ā*ā followed by a newline ā¦ ! I donāt unerstand why, here are the description and results of my tests :
Tests:
In the 3 tests, I look for the same string that is a star followed by a new line
What change in each test is in what string we are looking for.
Test 1 : weāre looking inside a string that is a variable where a text from a .txt file has been previously loaded.
Test 2 : we donāt use a variable, weāre looking inside a string directly, this string is a copy of the text in my .txt file
Test 3 : weāre looking inside a variable that has been set to a copy of the text in my .txt file
Result of the tests :
I run 3 preview : one looking for the star alone ("*"), one looking for the newline alone, and then one looking for a star and a newline.
if I look for a star only ā*ā, or if I look for a new line only , it is found in the 3 tests.
if I look for star and new line together, itās not found in the first test.
Note : I opened debugger to look at the string loaded in test 1. I converted this string to binary, to compare with my .txt file. But it seems to be the same.
So, the problem seems to be :
looking for a star followed by a newline doesnāt work when the main text has been loaded from a text file.
I just share the result of my test, and if I did no mistake, it might be consider as a bugā¦ ?
Itās kind of a specific issue though, a few people only might encounter this problem.
I just run a new test, replacing the star by another character, ālā. The issue is the same.
So it is not about the star character.
As far as I know, the problem can be generalised as:
" Looking for a character followed by a new line doesnāt work when the main text weāre looking in has been loaded from a .txt file. "
So the issue is larger and can be encounter by more people, since you might want to look for things in a .txt file in paragraphs separated by new lines ā¦
Have you checked the characters in your file (a hex editor would be ideal)? It could be the line is terminated with the old CR + NL (Carriage Return and New Line), so you may need to search for CR (ASCII 13) instead.
Alternatively, regular expressions (regex) may be of some use.
What you did is my ātest 3ā. It works fine on my side too. What doesnāt work is what I did in test 1, which is, load a text from a file into a scene variable.
I can try that yes, but Iāve never used a hex editor before. What I did though is to compare the binary translation of the string I look for, and the actual string Iām looking in, as shown by the debugger, and the binary octets are the same.
So I do look for a string (character + NewLine) that should be here since the debugger shows it in the variable, but that is not found for a mysterious reasonā¦
Thanks for the regex advice! Iāll give a try.
Nevertheless my tests make me think the problem appears only when loading from a txt fileā¦ So the trick might be in the loading event and not in the StrFind functionā¦
This is the variable holding the text that was read in? Then maybe someone from the GDdvelop developers might be able to shine some light on tā¦
One other option may be to use a javascript event to process the input string. Not sure how or what, but that may be able to handle the input a bit better.
Yes it is. I just checked again.
The picture here shows the comparaison I made :
on left side, the condition, lookin for string ā* newlineā
on right side the actual text variable where Iām looking in
The binary translation of the text looked for and the binary translation of the variable where the search is run, shows that the strings are the same. However the StrFind doesnāt find the string.
( I donāt know if to translate in binary is a good practice, but I did that copying the strings in an online translator, in order to compare what the characters actually are)
So I guess yesā¦ I checked a few times and it seems that thereās a strange thing here.
Are you trying to search for wildcards or the actual asterisk character? Because wildcards arenāt supported in any way within expressions or variables. They are just treated as asterisks.
(Edit: Wanting to confirm this first before I do testing tomorrow)
No Iām not looking for wildcards. The asterisk ā*ā was an example. But I tried with the letter ālā and the result is the same.
When I load into a variable a txt file that contains only a character followed by a new line, the variable can be seen in the debugger, well loaded.
But looking for this variable string value, in this variable string value return - 1 (strange!?!)
Okay, Iāll test tomorrow. 99% sure you cannot use return/new lines in expressions without the format MrMen showed, and Iām using some loaded texts in my test projects that search for special characters including newlines.
Iāll see if I can reproduce it and report back, if so weāll get an issue on the GitHub
I tried a few times, trying to isolate the issue with testsā¦ I can do some more, I donāt want to make you loose time with this, but I donāt know what to test moreā¦
I did one more test, this time using NewLine() format instad of the newline character. It does the same.
My .txt file is nothing more that the letter m and a new line
I load it into a variable ātextā
if I look in this variable text for the letter m alone, it works, my condition compares 2 numbers : StrFind(VariableString(text), āmā) =/ - 1 is true
if I look for NewLine() alone, the condition StrFind(VariableString(text), NewLine()) =/ - 1 is true too
the problem occures when I look for the letter m followed by a new line. The condition StrFind(VariableString(text), āmā+NewLine()) =/ - 1 is false ā¦
Woke up a way too early and did some testing. I can reproduce this a few different ways, so Iāve opened up a Github issue with a test project:
As with any issue, there isnāt a timeline on when/if itāll be fixed, but hopefully the sample project can assist narrow it down for the devs or contributors that look at it.
I think the best is to add a new expression that would be NewLineForWindows or something similar.
Or I could create an expression that cleans a string from the \r characters.
I think something that cleans \r (or replaces it with newlines) may be the most flexible option? Especially since someone could use it on a string that doesnāt contain \r (if theyāre unsure) and it wouldnāt cause any issues, correct?