• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM keyboard_string adds characters to the string, while the font does not support them.

E

Edwin

Guest
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.
 
H

Homunculus

Guest
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.
 
H

Homunculus

Guest
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.
 
E

Edwin

Guest
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.
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.
 
H

Homunculus

Guest
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 by a moderator:
E

Edwin

Guest
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.
I meant array_length_1d, it's a typo. For example argument is "1234567890" so the script will block these keys.
 
H

Homunculus

Guest
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.
 
Top