1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 input numbers on touch screen

Discussion in 'Programming' started by shmurl, Dec 3, 2019.

  1. shmurl

    shmurl Member

    Jun 28, 2016
    Trying to make my game compatible for both windows and touch screen devices.
    For typing on keyboard I'm using this
    if speed == 0
    var t_str;
    t_str = string_digits(keyboard_string);
    global.number = t_str;
    It works ok
    except when there's no input and it tries to execute this code
    var target = noone;
    with (obj_boy) if (image_index == global.number-1)
      target = id;
    it shows an error message "unable to convert string "" to float"

    But I'm not sure if that error message should be a different post?

    The main question I have is how to make buttons on the screen to input numbers (don't need or want letters)
    At first, I had it where mouse release on obj_1 would change global.number to 1 (and the same for the other numbers) which works fine for single digit numbers, but not for numbers with multiple digits.
    Then I tried global.number = virtual_key_add(0, 0, 48, 48, vk_numpad1); in mouse release. But it's not working, I don't really understand how it works.
    I don't want to use the keyboard_virtual_show code b/c I only want the numbers (and I also want the numbers objects to appear on window & html5 exports).

    As always, thanks in advance for your help!
  2. TailBit

    TailBit Member

    Oct 16, 2019
    Wouldn't it be better to just manipulate all the strings out of keyboard_string directly:
    keyboard_string = string_digits(keyboard_string);
    so if he type "4d3t" and only "43" shows on screen, if he then press backspace and it just remove the "t" and not the most behind letter shown "3" then it would feel odd

    well, it would make sense if global.number was .. a number, that way you don't have to worry about that error:
    global.number = real(t_str);
    now .. obj_number, in the create event you set the number to 0:
    num = 0;
    when creating them then you set the number to what it should be:
    with(instance_create_layer(x,y,"instance_layer",obj_number)) num = 1;
    with(instance_create_layer(x+32,y, "instance_layer",obj_number)) num = 2;
    and so on, or create most of them with a loop
    for(var n=0;n<9;n++) with(instance_create_layer(x + 32*(n mod 3) , y - 32*(n div 3), "instance_layer",obj_number )) num = n+1;
    // the 0 and undo will have to be placed afterwards
    so when pressed:
    global.number = global.number * 10 + num;
    you could also have an undo button which does:
    global.number = global.number div 10;
    Last edited: Dec 6, 2019

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