• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Why did they block real(string)?

D

Dawn

Guest
I used real(string) to check if the string value contains convertable-to-number string or just plain string. Now it shows "unable to convert string [value] to number" error... which really messes up my script. Is there a workaround for this?

I specifically used it for this
Code:
///@param str
var str=argument0;

if !is_string(str) return false

if string_upper(string_copy(str,1,3))=="INF" return false

if real(str)==0 {
    if str=="0" return true
    return false
}
return true
EDIT: Well, I fixed it with str!="" and string_digits(str) == string_length(str) but I still don't understand why it had to be done.
 
Last edited by a moderator:

TsukaYuriko

☄️
Forum Staff
Moderator
real has thrown errors when you fed it a string for as long as I can remember.

Code:
show_message(real("Threatuna bomb!"));
ERROR in
action number 1
of Create Event
for object object0:

Error in function real().
Verified in GM 8.1. This is not a new behavior by any means. The error message thrown may be worded differently, but that's about it.

The reason why it happens, as well as a workaround for it, are defined in the manual entry of real:
https://docs2.yoyogames.com/source/_build/3_scripting/4_gml_reference/strings/real.html said:
This function can be used to turn a given string into a real number. When using this function, numbers, minus signs, decimal points and exponential parts in the string are taken into account, while other characters (such as letters) will cause an error to be thrown. If you know, or suspect, that a string may have other characters then you can use string_digits() to remove all non-numeric characters, before using this function to turn the resulting string into a real number.
 
D

Dawn

Guest
@YellowAfterlife
Awesome post. Thank you.

@TsukaYuriko
Not sure about GM8 but it always returned 0 for my scripts as long as I remember as I've been using it in some scripts till this very update, except for the case the string that started with "inf", which returned infinite.
 

Hyomoto

Member
I think YAL's post makes a pretty clear case for why this is a bit annoying. It would be more helpful if real simply returned undefined when it can not be converted properly.

It would still crash if you used the value in some places, but if you are in a position where you need to validate a number the GML equivalent is quite a bit slower than real throwing out undefined. Though, to be fair, it's probably just adding that same code under the hood.
 
I used real(string) to check if the string value contains convertable-to-number string or just plain string. Now it shows "unable to convert string [value] to number" error... which really messes up my script. Is there a workaround for this?

I specifically used it for this
Code:
///@param str
var str=argument0;

if !is_string(str) return false

if string_upper(string_copy(str,1,3))=="INF" return false

if real(str)==0 {
    if str=="0" return true
    return false
}
return true
EDIT: Well, I fixed it with str!="" and string_digits(str) == string_length(str) but I still don't understand why it had to be done.
I can confirm that the changes in the recent update caused my code to give errors where there were none before.

i was reading in values from an ini file and trimming them using string_format() then converting them back to a real. my errors were caused by having spaces before the first digit in string.

i also used string_digit() to solve the issue. It means the real() function was not.throwing errors previously when it should have been if I understand correctly.
 

Hyomoto

Member
I can confirm that the changes in the recent update caused my code to give errors where there were none before.

i was reading in values from an ini file and trimming them using string_format() then converting them back to a real. my errors were caused by having spaces before the first digit in string.

i also used string_digit() to solve the issue. It means the real() function was not.throwing errors previously when it should have been if I understand correctly.
I don't know exactly when it changed, but it used to be possible to do "if string == string( real( string ) )" so I can confirm it was not always like this.
 
Top