SOLVED string() adds unnecesary newline.

Hello. I'm currently experiencing either a bug, undocumented behavior, or something completely out of my comprehension.

In simple terms, I have a variable that stores the health for the player (health_var1) and I want to draw the value of it on an object that's going to be sort of the HUD/GUI, AND next to it to be the max health value.
The problem is, that health_var1 is an integer, and you cannot concatenate a string and an integer directly, so I have to use string(health_var1) to be able to.
But once I try to use this code: draw_text(health1[0],health1[1],"Health: " + string(health_var1) + "/100");, this happens.
1597865339995.png
The issue isn't string length. As soon as I use draw_text(health1[0],health1[1],"Health: 100/100");, everything works as planned.
1597865438946.png
I also tried draw_text(health1[0],health1[1],"Health: " + "100" + "/100");, to test, but it produced the desired result as well. So the issue isn't in the concatenation or string length, but in string() itself.

If there is anything required, I'll respond as swift as I can.
- Rudolf.
 

TheouAegis

Member
Make a clean project. With nothing in it. Create a variable and give it a value. Then use
draw_text(64,64,string(that variable)+"/100");
If it still exists in the clean project, then report it. If it doesn't exist, then you have issues elsewhere.
 

Roldy

Member
I think you might have a problem else where.

Add a show_debug_message and see what your string looks like:
GML:
var _tempString = string(health_var1);
var _tempString2 = "Health: " + string(health_var1) + "/100";

show_debug_message(_health_var1);
show_debug_message(_tempString + " Is this on a new line?");
show_debug_message(_tempString2);

draw_text(health1[0], health1[1], _tempString2);
 

saffeine

Member
Make a clean project. With nothing in it. Create a variable and give it a value. Then use
draw_text(64,64,string(that variable)+"/100");
If it still exists in the clean project, then report it. If it doesn't exist, then you have issues elsewhere.
posted just before i had a chance to post it, but i couldn't manage to replicate the bug at all, despite two different approaches:
Runner_wJ1gBMVvVy.png
Runner_4ITGGKaWfs.png

unfortunately, that doesn't give me anything to go off, but the issue may lie elsewhere.
( and a further note: i did try without the spaces between too, and still nothing. )

wish i had something much more hopeful to reply with, but see if it might have anything to do with the variable itself, and just to make sure, check to see if the position variables are getting mixed up somewhere.
 

curato

Member
I can't see anything in the code you posted. It has to be something in the assignement of the value for health_var1 , Things are dynamically typed so are you sure that you aren't sneaking a new line in there some where? It might help to post where you assign this value. Also, just to test you may want to just flat out assign the variable to 100 before you print it and see if the issue persist.
 
UPDATE: The issue seems to be coming from the fact that the health variable is at first stored in a file, which is read by file_text_readln, (which is where the newline is coming from), and after is put in a ds_map.

SOLUTION: The solution to this problem was to simply to put it through real(health_var1), which removed the newline, and made it a real number. When necessary, string(health_var1) will no longer come with a newline.

Huge thanks to everyone contributed in this thread!
- Rudolf
 

Roldy

Member
posted just before i had a chance to post it, but i couldn't manage to replicate the bug at all, despite two different approaches:
View attachment 33501
View attachment 33500

unfortunately, that doesn't give me anything to go off, but the issue may lie elsewhere.
( and a further note: i did try without the spaces between too, and still nothing. )

wish i had something much more hopeful to reply with, but see if it might have anything to do with the variable itself, and just to make sure, check to see if the position variables are getting mixed up somewhere.
Check the type of health_var1. It is possible you accidentally turned it into a string somewhere.

GML:
show_debug_message(typeof(health_var1));
 

Roldy

Member
UPDATE: The issue seems to be coming from the fact that the health variable is at first stored in a file, which is read by file_text_readln, (which is where the newline is coming from), and after is put in a ds_map.

SOLUTION: The solution to this problem was to simply to put it through real(health_var1), which removed the newline, and made it a real number. When necessary, string(health_var1) will no longer come with a newline.

Huge thanks to everyone contributed in this thread!
- Rudolf
That would mean somewhere else in your code you have made health_var1 not Real (probably a string). You still have a bug somewhere put the real conversion at the same place you read it from a file.
 
That would mean somewhere else in your code you have made health_var1 not Real (probably a string). You still have a bug somewhere.
The odd thing is when, for instance, in comparisons
GML:
if (health_var1 <= 0) {
    instance_deactivate(obj_player);
}
It would ignore the newline and automatically set it as Real. It would only contain integers, so anywhere where it wasn't drawn, there wouldn't be an issue.
The reason why it was a String in the first place, is because in the save file after it comes more lines. It wasn't the last line in the save file. Therefore file_text_readln returned 100\n.
 
Top