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

Discussion in 'GameMaker Studio 2 Community Tech Support' started by Dawn, Apr 18, 2019.

  1. Dawn

    Dawn Member

    Joined:
    Sep 5, 2016
    Posts:
    58
    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: Apr 18, 2019
  2. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    2,477
  3. TsukaYuriko

    TsukaYuriko Q&A Spawn Camper Forum Staff Moderator

    Joined:
    Apr 21, 2016
    Posts:
    1,844
    real has thrown errors when you fed it a string for as long as I can remember.

    Code:
    show_message(real("Threatuna bomb!"));
    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:
     
  4. Dawn

    Dawn Member

    Joined:
    Sep 5, 2016
    Posts:
    58
    @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.
     
  5. Hyomoto

    Hyomoto Member

    Joined:
    Jul 7, 2016
    Posts:
    1,080
    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.
     
  6. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,352
    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.
     
    Pfap likes this.
  7. Hyomoto

    Hyomoto Member

    Joined:
    Jul 7, 2016
    Posts:
    1,080
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice