GM:S 1.4 keyboard_string adds characters to the string, while the font does not support them.

Discussion in 'Programming' started by Edwin, Jul 11, 2019.

  1. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417
    Hello. My Font Range is default. When I add the keyboard_string current character to the string, and if font doesn't support these characters will show up (not as spaces). I noticed it by using string_length. Is this a bug, why does it happens and how do I get rid of these characters?

    My GM:S is 1.4, not 2. Sadly it's too expensive, but I'm currently saving up the money for it.
     
  2. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    739
    keyboard_string is not tied to a specific font. Why should it be? And even if it was the case, which one if you have multiple fonts? In some cases you may not even need to draw it. It's definitely not a bug, it's supposed to work like this.

    If you have a font that can display only a specific subset of characters and having other invisible characters in your string is a problem, you may want to discard everything else from keyboard_string yourself. This can be done in many ways but depends on your specific case if you want it to be efficient.
     
    Edwin likes this.
  3. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    739
    One thing I can add to the above (sorry for the double post) is that people tend to forget that keyboard_string is not read-only, you can set the variable yourself. This means you could store your string in a custom variable, check in the step event if keyboard_string holds something new, sanitize its value (that is, removing the non drawable characters), add its value to your custom variable, and set keyboard_string to empty.

    By doing this you avoid sanitizing the whole string every keypress (or worse, every step). Unless of course you need the backspace functionality, but there are ways around that.
     
  4. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417
    I made a small script that will simply reset keyboard_string, well it is very slow I guess.
    Code:
    ///keyboard_discard_map(map)
    
    if (keyboard_string == "") { exit; };
    
    var map, mlen, key, klen, i;
    map = string(argument0);
    mlen = string_length(map);
    
    for (i = 0; i <= mlen; i ++) {
        key[i] = string_copy(map, i, 1);
    };
    
    klen = array_height_1d(key);
    
    for (i = 0; i < klen; i ++) {
        if (keyboard_string == key[i]) {
            keyboard_string = "";
        };
    };
    
    But this is very hard cuz I will need to import there a map with all existed symbols.
     
  5. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    739
    Are you trying to implement my suggestion, where map is supposed to be the set of all available characters?
    The only part of that code that makes sense to me is the first line. It probably doesn't even compile since array_height_1d doesn't exist.

    Keep in mind that while improbable, I don't think you can exclude the possibility of two keys being pressed almost at the same time resulting in keyboard string holding more than a single character at any point in time.
     
    Last edited: Jul 12, 2019
  6. Edwin

    Edwin Member

    Joined:
    Jul 15, 2018
    Posts:
    417
    I meant array_length_1d, it's a typo. For example argument is "1234567890" so the script will block these keys.
     
  7. Catan

    Catan Member

    Joined:
    Jun 20, 2016
    Posts:
    739
    You don't really need to convert the string map to an array, it can definitely be done using string_char_at or string_pos directly on the string. Something like:

    Code:
    var _map = "12345678"; //in your case, it could be argument0
    var _sanitized_string = ""; //will store the valid keyboard_string characters
    
    //loop through keyboard string char by char, in case it holds more than one
    for(var _i = 1; _i <= string_length(keyboard_string); _i++) {
        var _char = string_char_at(keyboard_string, _i); //get the char
        if(string_pos(_char, _map) > 0) { _sanitized_string += _char; } //if the char is present in the allowed map, add it to the result
    }
    
    keyboard_string = ""; //finished parsing, reset keyboard_string
    
    return _sanitized_string; //return only the allowed characters from keyboard_string
    
    You do need to adapt it a bit, but that's the idea. You could even turn this into a generic "string sanitizer" script and pass both the map and keyboard_string as parameters.

    Not that "map" in my case represents allowed characters, not sure if you are trying to do the inverse by passing a blacklist instead.
     
    Edwin likes 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